Capture Matrix Results from For Loop for Plotting - MATLAB
Show older comments
Hi, I am trying to capture the matrix results x as it loops through the For loop so that I can plot. Please see code below, thank you much for the help.
k = [1.750,-0.750,0;-0.750,1.250,-0.500;0,-0.500,0.500] %stiffness matrix in kip/in
m = [0.075,0,0;0,0.075,0;0,0,0.050] %mass matrix in kip
c = [0.00369,0,0;0,0.01481,0;0,0,0.01914] %damping matrix
tdelta = ((2*pi)/1.64)/pi
dt = 0.50
a_0 = 1/(dt)^2
a_1 = 1/(2*dt)
a_2 = 2 * a_0
a_3 = 1/a_2
[m_hat] = a_0*m + a_1*c %effective mass
x_ndt = zeros(3,1);
x_mdt = zeros(3,1);
F = zeros(3,1);
x = zeros(3,1);
xdot = zeros(3,1);
xddot = zeros(3,1);
for i = 1:1:5
h = i/i
if i == 1 %i = 1 is at t = 0
F(:,h) = [2;1;0.66667] %initial force vector
x(:,h) = [0;0;0] %initial displacment
xddot_0 = inv(m)*(F - c*xdot + k*x);
x_ndt = x - dt*xdot + a_3*xddot %x_ndt is x_-dt
x_mdt(:,h) = inv(m_hat)* F
xdot(:,h) = [0;0;0] %initial velocity
xddot(:,h) = inv(m)*(F - c*xdot + k*x)
else
x_ndt(:,h) = x
x(:,h) = x_mdt
F(:,h) = F - (k-a_2*m)*x - (a_0*m-a_1*c)*x_ndt
x_mdt(:,h) = inv(m_hat)* F
end
end
plot(i,x)
Answers (1)
KALYAN ACHARJYA
on 10 Nov 2019
Edited: KALYAN ACHARJYA
on 10 Nov 2019
How can this possible, you are trying to plot i vs x, where i is scalar (single value) and x is vector having length 3.
x =
-250.4839
239.6726
-57.8830
>> i
i =
To plot & get the graph, both parameters must be vectors, please ensure "i" also a vector having same length as x. Do indexing with different parameters or assign other variable name in for loop.
I hope these are sufficient hints to solve the issue.
Good Luck !
Categories
Find more on MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!