How to add multiple graphs onto one graph
25 views (last 30 days)
Show older comments
Hi!
Im still quite new to MatLab so forgive me if im asking a very simple question!!
I'd like to know how to plot multiple graphs (lines) onto one graph using one main file.
I'd like to get this output!! (figure 1)
figure 1^
i'm not looking for the :
x=1;
y=2;
x2=3;
y2=4;
plot(x,y)
hold on
plot(x2,y2)
hold off
kind of answer but rather, using one main file (figure 2) to generate the relevant points forthe graph to be plotted,
i have seperate files for functions in this main file such as Autopilot, Coupler etc.
% INITIAL SEGMENT
clear all
clc
% Define Paramaters Values
global Ta Ka Tau g Vt Kr Kv Kd Gc Ki Psi Phi Yr R ints Lref
% Typical Parameters for Lateral Beam Guidance System
Ta = 2.0; % Time constant
Ka = 1.0; % Deflection gain
Tau = 0.1; % Servo time constant
g = 9.81; % Gravitational constant
Vt = 60; % Aircraft forward velocity
Kr = 1.5; % Roll rate gyro
Kv = 1.3; % Vertical gyro
Kd = 1.7; % Directional gyro
Gc = 23.0; % Coupler gain
Ki = 0.5; % Integral gain
ints = 0; % Integral term
Lref = 0; % Reference angular error
% Initial Conditions
Psi = (5*pi)/180; % Heading angle
Phi = 0; % Roll angle
Yr = 60; % Lateral displacement of aircraft
R = 6000; % Range distance
% Define Parameters for Simulation
stepsize = 0.005; % Integration step size
comminterval = 0.1; % Communication interval
EndTime = 100; % Duration of simulation
i = 0; % Initialize counter for data storage
% Initial Condition for all States and State Derivatives
x= [Psi Phi 0 0 Yr 0]'; % x = [x1 x2 x3 x4 x5 x6]
xdot = [0 0 0 0 0 0]'; % xdot = [ xdot1 xdot2 xdot3 xdot4 xdot5 xdot6]
% x1 = Heading angle, Psi (rad)
% x2 = Roll angle, Phi (rad)
% x3 = Roll rate, P (rad/s)
% x4 = Aileron deflection, delta_A (rad)
% x5 = Lateral displacement, Yr (m)
% x6 = Angular error, lamda
% END OF INITIAL SEGMENT
% DYNAMIC SEGMENT
for time = 0:stepsize:EndTime
% stores time state and state derivative data at every comminterval
if rem(time,comminterval) == 0
i=i+1; % increment counter
tout(i,1)=time; % store time
xout(i,:)=x; % store states
xdout(i,:)=xdot; % store state derivatives
Rout (i,:)=R; % store range
lamda (i,1)=xout(i,5)/R; % store lamda value [x(6)]
end
% DERIVATIVE SECTION
psic = Coupler(x,Lref); % Coupler function
xdot = Autopilot1(x,psic); % Autopilot function
% END OF DERIVATIVE SECTION
% INTEG SECTION
%we can choose the integration method from here
%x = Euler(xdot,stepsize,x); % euler integration function
x = rk4('Autopilot1',stepsize,x,psic); % 4th Order Runge-Kutta Integration
% END OF INTEG SECTION
end
% END OF DYNAMIC SEGMENT
% TERMINAL SEGMENT
%Compare Euler & Runge Kutta Integration
figure(1)
plot(tout,(xout(:,5)),'red') %red-Euler, blue-Runge Kutta
title('Lateral Displacement')
xlabel('Time (s)')
ylabel('Yr')
grid on
grid minor
hold on
% Plot graphs of state variables
figure(2)
clf
subplot (3,2,1)
plot (tout,(xout(:,1)),'blue')
title('Heading Angle')
xlabel('Time (s)')
ylabel('\psi (rad/s)')
% legend ('Euler Integration')
grid on
grid minor
subplot (3,2,2)
plot(tout,(xout(:,2)),'blue')
title('Roll Angle')
xlabel('Time (s)')
ylabel('\phi(rad)')
% legend ('Euler Integration')
grid on
grid minor
subplot (3,2,3)
plot(tout,(xout(:,3)),'blue')
title('Roll Rate')
xlabel('Time (s)')
ylabel('P(rad/s)')
% legend ('Euler Integration')
grid on
grid minor
subplot (3,2,4)
plot(tout,(xout(:,4)),'blue')
title('Aileron Deflection')
xlabel('Time (s)')
ylabel('DeltaA (rad)')
% legend ('Euler Integration')
grid on
grid minor
subplot (3,2,5)
plot(tout,(xout(:,5)),'blue')
title('Lateral Displacement')
xlabel('Time (s)')
ylabel('Yr')
% legend ('Euler Integration')
grid on
grid minor
subplot (3,2,6)
plot(tout,(xout(:,6)),'blue')
title('Angular error')
xlabel('Time (s)')
ylabel('lamda')
% legend ('Euler Integration')
grid on
grid minor
% Plot graphs of 4th Order Runge Kutta
figure(3)
clf
subplot (3,2,1)
plot (tout,(xout(:,1)),'red')
title('Heading Angle')
xlabel('Time (s)')
ylabel('\psi (rad/s)')
% legend ('4th Order Runge Kutta')
grid on
grid minor
subplot (3,2,2)
plot(tout,(xout(:,2)),'red')
title('Roll Angle')
xlabel('Time (s)')
ylabel('\phi(rad)')
% legend ('4th Order Runge Kutta')
grid on
grid minor
subplot (3,2,3)
plot(tout,(xout(:,3)),'red')
title('Roll Rate')
xlabel('Time (s)')
ylabel('P(rad/s)')
% legend ('4th Order Runge Kutta')
grid on
grid minor
subplot (3,2,4)
plot(tout,(xout(:,4)),'red')
title('Aileron Deflection')
xlabel('Time (s)')
ylabel('DeltaA (rad)')
% legend ('4th Order Runge Kutta')
grid on
grid minor
subplot (3,2,5)
plot(tout,(xout(:,5)),'red')
title('Lateral Displacement')
xlabel('Time (s)')
ylabel('Yr')
% legend ('4th Order Runge Kutta')
grid on
grid minor
subplot (3,2,6)
plot(tout,(xout(:,6)),'red')
title('Angular error')
xlabel('Time (s)')
ylabel('lamda')
% legend ('4th Order Runge Kutta')
grid on
grid minor
% Integral & Coupler Gain
figure(4)
plot(tout,(xout(:,5)),'green')
title('Lateral Displacement')
xlabel('Time (s)')
ylabel('Yr')
%Gc =30(green), 23(blue),30(red)
%Ki=0.05 (green),0(blue), 0.5(red)
grid on
grid minor
hold off
% END OF TERMINAL SEGMENT
figure 2 ^
Thanks in advance!!!!!
0 Comments
Accepted Answer
dpb
on 10 Mar 2019
Edited: dpb
on 11 Mar 2019
And what, specifically, is wrong with
...
code that generates x,y line 1
code that generates x,y line 2
code that generates x,y line 3
plot(x,y) % line 1
hold on
plot(x,y) % line 2
plot(x,y) % line 3
...
? It's what the hold function exists for if it isn't convenient to have all x,y data in one array (as often isn't if there aren't identical number of elements in each array or the x-values aren't exactly spaced on each other).
If the latter is the case, then simply compute put all the data to plot into a single X, Y array for each and call plot...
...
code that generates x,y line 1
code that generates x,y line 2
code that generates x,y line 3
X=[x1 x2 x3];
Y=[y1 y2 y3];
plot(X,Y)
Or, just use arrays in the code and store each directly into the array when compute it...
...
y(:,1)=code that generates y line 1(x(:,1))
y(:,2)=code that generates y line 1(x(:,2))
y(:,3)=code that generates y line 1(x(:,3))
plot(x,y) % all three lines
So many options to choose from... :)
More Answers (0)
See Also
Categories
Find more on Stress and Strain 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!