system of odes

6 ビュー (過去 30 日間)
yngv
yngv 2011 年 4 月 8 日
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

採用された回答

Matt Tearle
Matt Tearle 2011 年 4 月 8 日
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 件のコメント
Matt Tearle
Matt Tearle 2011 年 4 月 11 日
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 2011 年 4 月 11 日
now it works properly, thank you very much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by