using a figure in a for loop for different figures
2 views (last 30 days)
Show older comments
When I try to plot different figures in a for loop, the last figure is plotted for all three figures. I tried interchanging the loops but the results are the same. How can i achieve this?
Also, I have a second question. I would like the title to read title('$$\omega = omega$$', 'interpreter', 'latex') but this doesn't yield \omega = 5, 6.1, and 5.9 for the three figures. I also tried title('$$\omega = $$' omega, 'interpreter', 'latex') but mixing doesn't work at all.
% constants similar to three plots
delta = 0.1; % F0/k deflection of mass under a force F0
% static deflection
omegan = 6; % natural frequency
x0 = 0.1; % initial displacement
dotx0 = 0.5; % initial velocity
t = (0:0.001:5); % time vector
for omega = [5, 6.1, 5.9]
x = x0*cos(omegan*t) + dotx0/omegan*sin(omegan*t) + delta*...
(cos(omega*t) - cos(omegan*t))/(1 - (omega/omegan)^2);
for i = [1, 2, 3]
figure(i)
plot(t, x)
xlabel('time, t')
ylabel('displacement, x(t)')
%title('$$\omega = $$' omega, 'interpreter', 'latex')
end
end
0 Comments
Accepted Answer
Orion
on 8 Oct 2014
Edited: Orion
on 8 Oct 2014
I don't get why you use a for loop on i. It seems to me that the result you want is more like :
% constants similar to three plots
delta = 0.1; % F0/k deflection of mass under a force F0
% static deflection
omegan = 6; % natural frequency
x0 = 0.1; % initial displacement
dotx0 = 0.5; % initial velocity
t = (0:0.001:5); % time vector
i = 0; % counter for figure
for omega = [5, 6.1, 5.9]
x = x0*cos(omegan*t) + dotx0/omegan*sin(omegan*t) + delta*...
(cos(omega*t) - cos(omegan*t))/(1 - (omega/omegan)^2);
i = i+1; % increment the counter and so the figure to plot in
figure(i)
plot(t, x)
xlabel('time, t')
ylabel('displacement, x(t)')
title(sprintf('$$\\omega = %.3f$$',omega), 'interpreter', 'latex')
end
and just above is also a solution to your latex problem in the title.
and if this is what you wanted to do, a better way to code will be :
% constants similar to three plots
delta = 0.1; % F0/k deflection of mass under a force F0
% static deflection
omegan = 6; % natural frequency
x0 = 0.1; % initial displacement
dotx0 = 0.5; % initial velocity
t = (0:0.001:5); % time vector
omegavalues = [5, 6.1, 5.9];
for i = 1:length(omegavalues);
omega = omegavalues(i);
x = x0*cos(omegan*t) + dotx0/omegan*sin(omegan*t) + delta*...
(cos(omega*t) - cos(omegan*t))/(1 - (omega/omegan)^2);
figure(i)
plot(t, x)
xlabel('time, t')
ylabel('displacement, x(t)')
title(sprintf('$$\\omega = %.3f$$',omega), 'interpreter', 'latex')
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Annotations 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!