How to input a force vector in ode45 function ?

2 visualizaciones (últimos 30 días)
BAPON
BAPON el 17 de Mayo de 2023
Comentada: Walter Roberson el 17 de Mayo de 2023
clc;
t=0:1:18;
R=[5 8 4 3 6 -7 8 -2 7 -3 8 1 6 4 8 -5 7 6 -4];
%% Calculate response without noise
y0=[0;0];
[t, y]=ode45(testode2,t,y0,R);
figure(1)
plot(y(:,1),y(:,2))
figure(2)
plot(t,y(:,1))
function f=testode2(t,y,R)
f=zeros(2,1);
a=-30; b=1000;
f(1)= y(2);
f(2)= R-a*(1-y(1)^2)*y(2)-b*y(1);
end
I am not able to put 'R' vector in function , it's showing 'NOT ENOUGH INPUT ARGUMENT'. So please help me ,what should I do ?
Thanks in advance

Respuestas (1)

Walter Roberson
Walter Roberson el 17 de Mayo de 2023
[t, y]=ode45(@(t,y)testode2(t,y,R),t,y0);
  1 comentario
Walter Roberson
Walter Roberson el 17 de Mayo de 2023
R=[5 8 4 3 6 -7 8 -2 7 -3 8 1 6 4 8 -5 7 6 -4];
That is a vector of length 19
f(2)= R-a*(1-y(1)^2)*y(2)-b*y(1);
You use the whole vector R there, so the right hand side is a vector of length 19, but f(2) designates a scalar output location. That is a problem. You have a problem.
If your R values are intended to represent "impulses" that are being given instantaneously at each of 0:18 then you will need to only run the ode45 for one second at a time, then adjust the boundary conditions, and then start the next cycle.
If your R values are intended to represent forces that are intended to be constant for the entire second, then you have a discontinuity in the derivatives every second, and the mathematics of ode45 would require that you only run the ode45 for one second at a time (that is, the time the value stays constant.)
If your R values are intended to represent continuous values that you want to do linear interpolation for (that is, in the time between t=0 and t=1 it rises smoothly from 5 to 8) then you also have problems with the derivatives, same situation as above, have to stop processing each time you change.
If your R values can be interpreted as sampling of a smooth curve of forces, then mathematically you potentially use interp1(0:18, R, t, 'spline') -- but do not use interp1(0:18, R, t) as it has the wrong mathematics for ode45.

Iniciar sesión para comentar.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by