How do I set a loop for ODE?
4 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
更多回答(1 个)
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!