solving a differential equation with ODE45

1 次查看(过去 30 天)
Hello,
I have 4 equations and I would like to solve it with a ode45 solver, however it does not work. Normally I use this solver to for solving equations with 2 variables.
Thanks for help!
ERROR:
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in exponentialansatz_dgl (line 16)
xdot= [ x(3);
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in exponentialansatz (line 68)
[t,y]=ode45(@exponentialansatz_dgl,tspan,x0);
CODE
main program
tspan = [0 20];
x1 = 2;
x2 = 3;
x3 = 0;
x4 = 1;
x0 = [ x1;
x2;
x3;
x4];
[t,y]=ode45(@exponentialansatz_dgl,tspan,x0);
function
function xdot = exponentialansatz_dgl(t,x)
x1=x(1);
x2=x(2);
x3=x(3);
x4=x(4);
k1 = 1;
k2 = 2;
k3 = 1.5;
m1= 2;
m2 = 1;
xdot= [ x(3);
x(4);
k2/m1 -(k1+k2)/m1;
k2/m2 -(k2+k3)/m2; ]

采纳的回答

Steven Lord
Steven Lord 2020-11-11
Remove the space in the middle of each of the last two lines of the xdot matrix. You probably also want to end the definition of xdot with a semicolon to prevent MATLAB from displaying xdot each time ode45 calls this function.
xdot= [ x(3);
x(4);
k2/m1-(k1+k2)/m1;
k2/m2-(k2+k3)/m2; ];
  1 个评论
Sergio Orasch
Sergio Orasch 2020-11-11
Thanks! I did not have a space between the lines in the code, but now it works with your code. Mysterious

请先登录,再进行评论。

更多回答(0 个)

类别

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

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by