How to set iteration results as columns in matrix?

조회 수: 4 (최근 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})

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numerical 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