How to plot several graphs in a loop?

3 views (last 30 days)
Jason Choi
Jason Choi on 11 Apr 2019
Answered: Quentin Derville on 3 Dec 2021
I wouldl ike to generate multiple graphs from my loop. Here is the code I have so far:
file
I am trying to graph (time vs normal acceleration) and (time vs tangential acceleration) for each a_value(i.e iteration).
I was wondering if I could get 4 different graphs by placing the respective part of the code inside either loop.
In the end I would be able to see 4 different plots generated by each iteration.
  2 Comments
Rik
Rik on 11 Apr 2019
I suspect you might find subplot helpful, but I don't fully understand what you mean.
Also, why did you post your code as an image? Just use the markup provided by the editor here. Then we can copy your code and run it.
Jason Choi
Jason Choi on 11 Apr 2019
My apologies, I am new to asking questions.
clc;
clear all;
syms a t
rho = (1 + (100 * a^2 * t^2))^(3/2)/ (2 * a);
rho_dot = diff(rho,t);
theta_dot = (10 * a) / (1 + (100 * a^2 * t^2));
theta_ddot = diff(theta_dot, t);
a_value = [0.25 0.5 0.75 1]; %wanted a values
time = 0:0.1:2; % interval time
values = zeros(3, length(time));
counter = 1;
while counter <= length(a_value) % change counter value
a = a_value(counter);
subs(rho); subs(rho_dot); subs(theta_dot); subs(theta_ddot);
for i = 1:length(time)
t = time(i);
new_rho = subs(rho); new_rho_dot = subs(rho_dot);
new_theta_ddot = subs(theta_ddot);
new_theta_dot =subs(theta_dot);
values(1, i) = new_rho * new_theta_dot; % velocity
values(2, i) = new_rho * new_theta_dot^2; % normal acceleration
values(3, i) = new_rho_dot* new_theta_dot + new_rho * new_theta_ddot; % tangential acceleration
end
% the acceleration value at t = 0.8 s
a_total = sqrt(values(2,8)^2 + values(3,8)^2);
fprintf("When the given acceleration is %d[m/s^2], the total acceleration at t = 0.8s is : %d [m/s]\n", a_value(counter), a_total)
counter = counter + 1;
end
I am inputing my values of rho and theta along with their derivitives in order to get the velocity, normal and tangentential acceleration of a particle. I input my values into the values matrix, and I want to plot these values against time
% something along the lines of
plot(time, values(2,i))
plot(time, values(3,i))
I would like to plot this in the loop so I have two plots each, for each iteration. I need to copy these plots as an image.

Sign in to comment.

Answers (1)

Quentin Derville
Quentin Derville on 3 Dec 2021
Try this code, it may be helpful
f1 = figure;
f2 = figure;
for k = 1:3
% sin kx plot
figure(f1)
plot(x,sin(k*x))
hold on
% cos kx plot
figure(f2)
plot(x,cos(k*x))
hold on
end
figure(f1)
title('Sinus')
figure(f2)
title('Cosinus')

Categories

Find more on Programming 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!