system of odes

3 views (last 30 days)
yngv
yngv on 8 Apr 2011
hello i have a system of ode equations i want to solve with ode45. the equations is x1´=s*(x2-x2*x1-x1-q*(x1^2) x2´=(-x2-x1*x2+x3)/s x3´=w*(x1-x3) were s,q and w is constans i also have the conditions x1(0)=30 x2(0)=1 x3(0)=30
i've defined the system of equations in the function fxx,
function yprime=fxx(x,s,q,w) global s; global q; global w; x = zeros(3,1); yprime=[s*(x(2)-x(2)*x(1)-x(1)-q*(x(1)).^2); (-x(2)-(x(1))*x(2)+x(3))/s; w*(x(1)-x(3))]; end
and then i run the command [t,x]=ode45(@fxx,[0 10],[30 1 30]) then i get the errors ??? Error using ==> mldivide Matrix dimensions must agree.
Error in ==> fxx at 4 yprime=[s*(x(2)-x(2)*x(1)-x(1)-q*(x(1)).^2); (-x(2)-(x(1))*x(2)+x(3))/s; w*(x(1)-x(3))];
Error in ==> odearguments at 109 f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ==> ode45 at 173 [neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ... and sometimes i get that x is undefined, im sure i have added the folder to matlab

Accepted Answer

Matt Tearle
Matt Tearle on 8 Apr 2011
The problem is your argument list for fxx. ODE functions should be functions of the independent variable, then the dependent variable, then any other parameters. But you're using global anyway. So... Don't do that. Do this instead
function yprime = fxx(t,x,s,q,w)
% no globals - globals are bad
x = zeros(... etc
Then in your calling script,
s = pi;
q = 42:
w = 1234;
odefun = @(t,y) fxx(t,y,s,q,w);
[t,x] = ode45(odefun,[0,10],[30;1;30]);
  3 Comments
Matt Tearle
Matt Tearle on 11 Apr 2011
Oh, waitwaitwaitwaitwait. Didn't read your code carefully enough, sorry.
Delete the line x = zeros(3,1)! What on earth is that doing there? I thought it was preallocating your output (which is yprime, not x -- my bad for not noticing that). x is your input dependent variable. If the first thing you do is overwrite it with zeros, then, yes, not surprisingly, you'll get garbage results.
yngv
yngv on 11 Apr 2011
now it works properly, thank you very much!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!