Input signal and arguments in Ode45. Not enough imput arguments
4 views (last 30 days)
Show older comments
Alvaro Arratibel
on 9 Mar 2021
Answered: Bjorn Gustavsson
on 9 Mar 2021
Hi everybody!
I have an issue when using Ode45. I want to resolve a second order diferential equation where some parameters are fixed and one is a sinusoidal signal.
First of all I get the data from the main:
a=1;
b=1;
c=1;
x_in has the values of a sinusoidal signal
[t,x_sp]=ode45(@Dinamica,[0 20],[0 0]);
The function:
function dx_sp=Dinamica(t,x_sp,a,b,c,x_in)
u=x_sp(1)
v=x_sp(2)
dx_sp=(x_in*a*b^2)-(2*c*b*v)-(u*b^2)
end
¿How should I introuduce those values into my function?
Matlab says: not enough imput arguments.
Thanks!
0 Comments
Accepted Answer
Bjorn Gustavsson
on 9 Mar 2021
The function dinamica expects 6 input arguments. However, the function, f that ode45 takes as input is handled as a function of two input arguments, so inside ode45 that function is called like this:
dydt = f(t,y);
Then inside your dinamica it crashes because it is expecting the 6 inputs. You have to change the call to something like this:
[t,x_sp]=ode45(@(t,x) Dinamica(t,x,a,b,c,x_in),[0 20],[0 0]);
However, inside dinamica you have a multiplication with the entire x_in array, that is every value of something you claim to be something varying harmonically. That will not work. You have to estimate the value of x_in at the time t. To do that you can do something like this:
function dx_sp=Dinamica(t,x_sp,a,b,c,t_in,x_in)
u=x_sp(1);
v=x_sp(2);
x_in_at_t = interp1(t_in,x_in,t,'pchip');
dx_sp=(x_in_at_t*a*b^2)-(2*c*b*v)-(u*b^2);
end
and change the call to:
[t,x_sp]=ode45(@(t,x) Dinamica(t,x,a,b,c,t_in,x_in),[0 20],[0 0]);
HTH
0 Comments
More Answers (1)
Walter Roberson
on 9 Mar 2021
1 Comment
Walter Roberson
on 9 Mar 2021
However:
x_in has the values of a sinusoidal signal
That implies that it is non-scalar.
dx_sp=(x_in*a*b^2)-(2*c*b*v)-(u*b^2)
All of x_in is used there, so dx_sp would be non-scalar.
[t,x_sp]=ode45(@Dinamica,[0 20],[0 0]);
The boundary conditions are [0 0], a vector of length 2. Your function has to return a column vector the same length, so length 2. Therefore your code would fail unless x_in is a column vector of length 2.
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!