How to add multiple graphs onto one graph
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!!!!!
Accepted Answer
More Answers (0)
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!