How to set iteration results as columns in matrix?
4 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
Walter Roberson
on 19 Dec 2012
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})
More Answers (0)
See Also
Categories
Find more on Numerical Integration and Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!