solving a differential equation with ODE45

10 views (last 30 days)
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; ]

Accepted Answer

Steven Lord
Steven Lord on 11 Nov 2020
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 Comment
Sergio Orasch
Sergio Orasch on 11 Nov 2020
Thanks! I did not have a space between the lines in the code, but now it works with your code. Mysterious

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!