FOR LOOP with constant matrix
Show older comments
Dear All,
I want to create FOR LOOP by Matlab to solve this equation :
x(t+dt)=(dt*A+I)*x(t)+B*dt
in which
A =[0 1;-6 -5]
I=eye(2)
B=[0;1]
dt=0.01
initial condition
x(0)=[0;0]
x(t) = vector 2x1
Any one can give me some ideas ?
I have created some scripts in m file as per below :
ft=1; % Step Input
dt=0.01; % Time Interval (can be changed)
t_end=5; % End of Time for plotting (fix)
Nstep=round(t_end/dt);
Xo=zeros(2,1); % Initial Condition for X, at t=0
Xdot0=A*Xo+B*ft; % Intitial Condition for Xdot, at t=0
Y=C*Xdot0; % Intitial Condition for Y, at t=0
I=eye(size(A)); % Identity Matrix
for i=1:Nstep
x(i)= Xo
x(i+1)=(dt*A+I)*x(i)+B*dt;
end
However, It does not work
Thanks a lot Wildany
Answers (1)
Avoid "it does not work" in question. Better explain any details, e.g. if you get an error message (then add a complete copy) or if the results differ from your expectations (then describe both).
x is an array and in consequence x(i) is a scalar. You cannot assign the vector Xo to a scalar element of a vector.
If i is increased by 1 in each iteration, you overwrite the former value in x(i+1) .
ft = 1; % Step Input
dt = 0.01; % Time Interval (can be changed)
t_end = 5; % End of Time for plotting (fix)
Nstep = round(t_end / dt);
X = zeros(2, Nstep); % Pre-allocate the complete solution
X(:, 1) = [0;0]; % Initial Condition for X, at t=0
% Not used, so omit it:
% Xdot0=A*Xo+B*ft; % Intitial Condition for Xdot, at t=0
% Y=C*Xdot0; % Intitial Condition for Y, at t=0
I = eye(size(A)); % Identity Matrix
B = [0;1]; % Was missing ?
for i = 2:Nstep % 1st step defined already
x(:, i) = (dt*A+I) * x(:, i-1) + B*dt;
end
Categories
Find more on Creating and Concatenating Matrices 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!