solving a differential equation with ODE45

조회 수: 5 (최근 30일)
Sergio Orasch
Sergio Orasch 2020년 11월 11일
댓글: Sergio Orasch 2020년 11월 11일
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