How to set iteration results as columns in matrix?

5 ビュー (過去 30 日間)
Achiad
Achiad 2012 年 12 月 19 日
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);
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 this error:
Subscripted assignment dimension mismatch.
Error in second_main (line 14)
X1(:,i)=X(:,1);
Is there a solution for this?

採用された回答

Walter Roberson
Walter Roberson 2012 年 12 月 19 日
You should not be assuming that the number of timesteps used internally will be exactly the same each time through. For your calculations use
X1{i} = X(:,1);
X2{i} = X(:,2);
T{i} = t;
and then the plot at the end should be
plot (T{1},X1{1},T{2},X1{2},T{3},X1{3},T{4},X1{4})
  1 件のコメント
Achiad
Achiad 2012 年 12 月 19 日
finally! thank you very very much! :)

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by