How do I set a loop for ODE?

1 回表示 (過去 30 日間)
Achiad
Achiad 2012 年 12 月 18 日
Hello, I'm trying to solve an ode of a second order response for my homework in dynamics.
my function is:
%SORODE_Z%
function [dX]=SORODE_Z(t,X,z,varargin)
tau=0.5;
Xa=1;
C=(1/tau^2)*[0, Xa]';
A=[0, 1; -1/tau^2 , -2.*z./tau];
dX=A*X+C;
end
my script is:
%second_main%
clear
t0=0;
tfinal=10;
tspan=[t0 tfinal];
X0=[0,0]';
zv=input('input zv as [n1,n2...nn]:');
nn=numel(zv);
options=[];
for i=1:nn
v(i)=zv(i);
z=zv(i);
[t,X]=ode45('SORODE_Z',tspan,X0,options,z);
t1(:,(i))=t(:,1);
X1(:,(i))=X(:,1);
X2(:,(i))=X(:,2);
end
figure(1)
clf
plot (t1(:,1),X1(:,1),t1(:,1),X1(:,2),t1(:,1),X1(:,3),t1(:,1),X1(:,4))
title('Step response by second order')
xlabel('t [sec]')
ylabel('X')
axis([0,10,0,2])
legend('z=o','z=0.5','z=1','z=2')
my input is: [0,0.5,1,2]
when I run the script i get an error of:
Error using vertcat
CAT arguments dimensions are not consistent.
Error in SORODE_Z (line 6)
A=[0, 1; -1/tau^2 , -2.*z./tau];
Error in odearguments (line 88)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 114)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn,
...
Error in second_main (line 13)
[t,X]=ode45('SORODE_Z',tspan,X0,options,z);
can someone help me please?

採用された回答

Pedro Villena
Pedro Villena 2012 年 12 月 18 日
correction in your script:
[t,X] = ode45(@SORODE_Z,tspan,X0,options,z);
  2 件のコメント
Achiad
Achiad 2012 年 12 月 18 日
thank you very much! :)
Achiad
Achiad 2012 年 12 月 18 日
I changed this section to:
for i=1:nn
v(i)=zv(i); %#ok<SAGROW>
z=zv(i);
[t,X]=ode45(@SORODE_Z,tspan,X0,options,z);
X1(:,i)=X(:,1);
X2(:,i)=X(:,2);
end
now I get this error:
Subscripted assignment dimension mismatch.
Error in second_main (line 14)
X1(:,i)=X(:,1);
Is there a solution?

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

その他の回答 (1 件)

Jan
Jan 2012 年 12 月 18 日
編集済み: Jan 2012 年 12 月 18 日
As far as I can see, the error can only occur, when z is not a scalar. But in the posted code, z seems to be a scalar in every case. Therefore I speculate, that the posted code differs from the code you run, e.g. because you have different scripts with the same name or did not save before running.
Instead of guessing, using the debugger would be much smarter:
dbstop if error
Then start your program again. Now Matlab stops when the error is reached and you can inspect the values of the variables in the command windows or workspace browser. What are the sizes of tau and z then?
Btw, it is safer to use a function handle instead of a string to define the function to be integrated, see doc ode45. Using anonymous functions to define parameters is even better.
  1 件のコメント
Achiad
Achiad 2012 年 12 月 18 日
thank you :) changing from string to anonymous function corrected half the problem.

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

カテゴリ

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