Supposed to get 6 separate graphs, instead getting 3

17 views (last 30 days)
Hi! So for my assignment, I am supposed to end up with 6 graphs, 3 showing displacements, 3 showing velocities. However, for some reasons the first 3 graphs don't want to work when I try to run my code. Oddly enough, if I make one figure with 6 plots, then they all show up, but when I do it as 6 separate ones, the first 3 don't work. I need to be able to make 6 separate plots to meet the assignment instructions.
This is what my code looks like:
% Time Interval
timespan = [0:0.1:25]; % sec
% Initial Conditions
q0 = [0 0 0 0.1 0.2 0.3]; % m/s
% Solve for Velocity and Displacememnt as a Function of Time
[t,q] = ode45(@(t,q) dampersystem(t, q), timespan, q0);
% Setting Values
X1 = q(:,1);
X2 = q(:,2);
X3 = q(:,3);
V1 = q(:,4);
V2 = q(:,5);
V3 = q(:,6);
% Plotting
figure(1) % X1 (m)
plot(t,X1);
grid on;
title('X1(m) vs Time(sec)')
xlabel('Time(sec)')
ylabel('Displacement(m)')
figure(2) % X2 (m)
plot(t,X2);
grid on;
title('X2(m) vs Time(sec)')
xlabel('Time(sec)')
ylabel('Displacement(m)')
figure(3) % X3 (m)
plot(t,q(:,3));
grid on;
title('X3(m) vs Time(sec)')
xlabel('Time(sec)')
ylabel('Displacement(m)')
figure(4) % V1 (m/s)
plot(t,q(:,4));
grid on;
title('V1(m/s) vs Time(sec)')
xlabel('Time(sec)')
ylabel('Velocity(m/s)')
figure(5) % V2 (m/s)
plot(t,q(:,5)); grid on;
title('V2(m/s) vs Time(sec)')
xlabel('Time(sec)')
ylabel('Velocity(m/s)')
figure(6) % V3 (m/s)
plot(t,q(:,6)); grid on;
title('V3(m/s) vs Time(sec)')
xlabel('Time(sec)')
ylabel('Velocity(m/s)')
And here is the function that is on a separate script (by instructions):
function dqdt = dampersystem(t, q)
% Differential Equations
F1 = t;
F2 = sin(t);
F3 = 3;
%Calculates Displacement for each Mass
dqdt = zeros(6,1);
dqdt(1) = q(4);
dqdt(2) = q(5);
dqdt(3) = q(6);
% Three Equations of Motion to find Velocity for each Mass
dqdt(4) = (F1/3) -(4/3)*q(4) -(30/3)*q(1) +(15/3)*q(2) +(4/3)*q(6);
dqdt(5) = F2 -30*q(2) + 15*q(1) + 15*q(3);
dqdt(6) = (F3/5) -(6/5)*q(6) -(30/5)*q(3) +(4/5)*q(4) +(15/5)*q(2);
end

Answers (2)

Star Strider
Star Strider on 5 Dec 2020
I don’t understand what ‘don’t work’ means.
They all show up and appear to be appropriate when I run your posted code, both as separete figures and (when I plot them in one figure using subplot) as appropriate subplots. (I am running R2020b, although I doubt the version is relevant.)
Can you be more specific as to what the problem is?
  6 Comments
Star Strider
Star Strider on 5 Dec 2020
Hannah Pike —
So it works now and the problem is solved?
Did you have the clc call (or something similar) after the first 3 figures were created? The existing plots and variables would have overwriten any with similar names that were created before them.
Image Analyst
Image Analyst on 5 Dec 2020
I think you meant "close all" since clc just clears the command window and does not destroy plots (though I'm not sure what clc does if you're using the "live editor" - I'm just talking about regular m-file scripts).

Sign in to comment.


Image Analyst
Image Analyst on 5 Dec 2020
Instead of figure(n), try subplot(2,3,n). For example for the various n do this:
subplot(2, 3, 1);
subplot(2, 3, 2);
subplot(2, 3, 3);
subplot(2, 3, 4);
subplot(2, 3, 5);
subplot(2, 3, 6);
instead of figure(1), figure(2), etc.
  1 Comment
Hannah Pike
Hannah Pike on 5 Dec 2020
This worked well! The issue is that when I try and not do it as a subplot, and I try and make each graph it's own figure, only the last 3 display.

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots 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!