Input signal and arguments in Ode45. Not enough imput arguments

4 次查看(过去 30 天)
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!

采纳的回答

Bjorn Gustavsson
Bjorn Gustavsson 2021-3-9
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

更多回答(1 个)

Walter Roberson
Walter Roberson 2021-3-9
  1 个评论
Walter Roberson
Walter Roberson 2021-3-9
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by