Overplotting 6 graphs by changing into 3 graphs in MATLAB

  • Thread starter Thread starter wel
  • Start date Start date
  • Tags Tags
    Graphs Matlab
Click For Summary
SUMMARY

This discussion focuses on combining six separate MATLAB plots into three overlaid plots using two sets of M-files that contain differential equations and plotting functions. The first set of M-files utilizes the ODE solver to generate three plots for original equations, while the second set produces three plots for asymptotic expansions. The user seeks to overlay the plots for variables c and c_0, s and s_0, and q and q_0, using the 'hold on' command to achieve this in MATLAB.

PREREQUISITES
  • Familiarity with MATLAB programming and M-files
  • Understanding of ordinary differential equations (ODEs)
  • Knowledge of MATLAB's plotting functions and commands
  • Basic grasp of asymptotic analysis in differential equations
NEXT STEPS
  • Learn how to use MATLAB's 'hold on' command for overlaying plots
  • Explore MATLAB's ODE solvers, specifically ode23s and its options
  • Investigate MATLAB's subplot functionality for organizing multiple plots
  • Study techniques for visualizing and interpreting differential equations in MATLAB
USEFUL FOR

Researchers, engineers, and students working with MATLAB who need to visualize complex data from differential equations, particularly those interested in combining multiple datasets into cohesive plots.

wel
Gold Member
Messages
36
Reaction score
0
Two M-files one contains differential equations and another one to run and plot the graphs. since i am using ode solver, it has two files to run.

THERE ARE 4 MATLAB CODES WITH 4 SEPARATE MATLAB M-FLIES BUT 2 M-FILES TO PRODUCE 3 PLOTS AND ANOTHER 2 M-FILES TO PRODUCE ANOTHER 3 GRAPHS. BUT I WANT TO OVERPLOTS THESE 6 PLOTS INTO 3 PLOTS ONLY.

HERE ARE THE FIRST two M-files,
ONE contains differential equations and another one CONTAINS plotting the graphs.
since i am using ode solver, it has two files.

Code:
% Creating recent original 3 ode without scaling any parameters and c.
    
      function xprime= ns(t,x)
    
      I=1200; % light intensity
        
      %values of parameters
        k_f= 6.7*10.^7;
        k_d= 6.03*10.^8; 
        k_n=2.92*10.^9; 
        k_p=4.94*10.^9;
        alpha =1.14437*10.^-3;
    
        %Unknown parameters
        lambda_b= 0.0087;
        lambda_r =835; 
        gamma =2.74; 
        
        %Pool Values
        P_C= 3 * 10.^(11);
        P_Q= 2.87 * 10.^(10); 
        
     % initial conditions
      c=x(1);
      s=x(2);
      q=x(3);
    
      %Non-linear differential equations.
      % dc/dt= alpha*I + c(- k_f - k_d - k_n * s - k_p*(1-q))
      % ds/dt = lambda_b * c* P_C *(1-s)- lambda_r *(1-q)*s
      % dq/dt = (1-q)* k_p * c *(P_C / P_Q)- gamma * q
    
     xprime = zeros(3,1);    % a column vector
    
    xprime(1)= alpha*I + c*(- k_f - k_d - k_n * s - k_p*(1-q));
    xprime(2)= lambda_b *(1-s)*c* P_C  - lambda_r *(1-q)*s;
    xprime(3)=(1-q)*k_p* c*(P_C / P_Q)- gamma * q;
      
    % TO RUN the recent original odes with t= 0.2 *10^-9
    
      format bank
      close all; 
      clear all; 
      clc; 
    epison= 10.^-9;
      %time interval
      ti=0; 
      tf=0.2*epison; 
      tspan=[ti tf]; 
    
      x0=[0.25 0.02 0.98]; %initial conditions
    
      %time interval of [0 2] with initial condition vector [0.25 0.02 0.98] at time 0.
      options= odeset('RelTol',1e-9, 'AbsTol',[1e-9 1e-9 1e-9]);
      [t,x]= ode23s(@ns,tspan,x0,options); 
    dt = t(2:end)-t(1:end-1);  % number of time step size it is using
      
    %Plotting the graphs:
    plot(t(2:end), t(2:end)-t(1:end-1));   % plotting the time step size.
     title('Time steps for 3 recent original odes (c,s,q), time =0.2*10^-9 ');
    ylabel('t'), xlabel('t_n'); 
    
      figure 
      subplot(3,1,1), plot(t,x(:,1),'r'),grid on; 
      title('3 recent original odes, time =0.2*10^-9 '),ylabel('c'); 
    
      subplot(3,1,2), plot(t,x(:,2),'b'),grid on; 
      ylabel('s'); 
    
      subplot(3,1,3), plot(t,x(:,3),'g'),grid on; 
      ylabel('q');xlabel('Time')
Another separate two M-files , ONE contains differential equations and another contains plotting the graphs. since i am using ode solver, it has two files.

Code:
  % 3 Asymptotic expansion t=0.2*10^-9 which gives tau =0.2
    
    
      function xpr= no(t,x)
        epison= 10.^-9; 
      %values of parameters
        k_f= 6.7*10.^7;
        k_d= 6.03*10.^8; 
        k_n=2.92*10.^9; 
        k_p=4.94*10.^9;
        
        %Unknown parameters
        lambda_b= 0.0087;
        
        % scale parameters
        K_F= k_f *   epison;
        K_D= k_d *   epison; 
        K_N= k_n *   epison; 
        K_P= k_p *   epison;
        LAMBDA_B= lambda_b*  epison;
        
        %Pool Values
        P_C= 3 * 10.^(11);
        P_Q= 2.87 * 10.^(10); 
        
     % initial conditions
      c_0=x(1);
      s_0=x(2);
      q_0=x(3);
    
      %Non-linear differential equations.
      % dc_0/dtau=  c_0*(- K_F - K_D - K_N * s_0 - K_P*(1-q_0))
      % ds_0/dtau = Lambda_B * c* P_C *(1-s_0)
      % dq_0/dtau = (1-q_0)* K_P * c_0 *(P_C / P_Q)
      
      
      % dc_0/dt=  c_0*(- K_F - K_D - K_N * s_0 - K_P*(1-q_0))
      % ds_0/dt = Lambda_B * c_0* P_C *(1-s_0)
      % dq_0/dt = (1-q_0)* K_P * c_0 *(P_C / P_Q)
    
    xpr= zeros(3,1);
    
    xpr(1)=c_0*(- K_F - K_D - K_N * s_0 - K_P*(1-q_0));
    xpr(2)= LAMBDA_B * c_0*P_C *(1-s_0);
    xpr(3)= (K_P * c_0*P_C*(1-q_0)) / P_Q;
    
    % TO RUN 3 asymptotic expansion for c_0,s_0 and q_0
    
      format bank
      close all; 
      clear all; 
      clc; 
    epison= 10.^-9;
    
      %time interval
      ti=0; 
      tf=0.2*epison; 
      tspan=[ti tf]; 
      
      x0=[0.25 0.02 0.98]; %initial conditions
    
      %time interval of [0 2] with initial condition vector [0.25 0.02 0.98] at time 0.
      options= odeset('RelTol',1e-9, 'AbsTol',[1e-9 1e-9 1e-9]);
      [t,x]= ode23s(@no,tspan,x0,options); 
    dt = t(2:end)-t(1:end-1); % number of time step size it is using
     
    %Plotting the graphs:
      plot(t(2:end), t(2:end)-t(1:end-1));   % plotting the time step size.
       title('Time steps for 3 asymptotic expansions (c_0,s_0, q_0) at tau=0.2');
      ylabel('t'), xlabel('t_n'); 
      
      figure 
      subplot(3,1,1), plot(t,x(:,1),'r'),grid on; 
      title('3 asymptotic expansion when t=0.2*epsion, tau= t/epison, so tau=0.2'),ylabel('c_0'); 
    
      subplot(3,1,2), plot(t,x(:,2),'b'),grid on; 
      ylabel('s_0'); 
    
      subplot(3,1,3), plot(t,x(:,3),'g'),grid on; 
      ylabel('q_0');xlabel('Time')
There are 6 plots from these two separate m-files which contains 3 each graphs and there are different. Now i want to overplot 3 plots by combining 6 plots together into 3 plots. I mean one overplot containing c and c_0, another for s and s_0 and last one for q and q_0 and 3 in total.
Time intervals of these 3 plots can be between 0 to 0.2*10-9.
please help me. i will be very much grateful and thankful for your help.
 
Last edited:
Physics news on Phys.org
the command 'hold on' will allow you to add signals to figures that are already created

plot(t,x)
hold on
plot(t,y)

this will plot both y and x on the same graph.
you can the hold command in conjunction with assigning set figures to certain plots, and you can achieve your goal.
 

Similar threads

Replies
1
Views
3K
Replies
1
Views
4K
Replies
1
Views
4K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
4
Views
2K
Replies
2
Views
2K
Replies
1
Views
1K
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K