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

 Réponse acceptée

Matt Tearle
Matt Tearle le 8 Avr 2011

1 vote

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 commentaires

yngv
yngv le 10 Avr 2011
after trying your code, x is a vector [30,1,30........30,1,30] which is incorrect, but atleast i dont get any errors :)
maybe the yprime=[...] is wrong? maybe i should use 3 diffrent variables instead of x(1), x(2) and x(3)?
Matt Tearle
Matt Tearle le 11 Avr 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 le 11 Avr 2011
now it works properly, thank you very much!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by