How to combine two graph from two function in another script?
    조회 수: 4 (최근 30일)
  
       이전 댓글 표시
    
Hello, I have 2 function in another script. I want to combine the graph into 1 graph. Is it possible and how to combine it?
댓글 수: 0
채택된 답변
  Jon
      
 2023년 6월 27일
        Example
Run script 1
% Script 1
% define first curve
t1 = linspace(0,5);
y1 = t1 + 2*t1.^2;
Run script 2
% Script 2
% define second curve
t2 = linspace(0,5);
y2 = -t2 -3*t2.^2;
t1,y1,t2,y2 will be in the workspace. Now plot both curves either from the command line or in another script
plot(t1,y1,t2,y2)
댓글 수: 5
  Torsten
      
      
 2023년 6월 28일
				tstart = 0;
tend = 100;
dt = 0.01;
T = (tstart:dt:tend).';
Y0 = [10 0 0 0 0];
f = @myode;
YRK4 = fRK4(f,T,Y0);
M1RK4 = YRK4(:,1);
M2RK4 = YRK4(:,2);
M3RK4 = YRK4(:,3);
ORK4  = YRK4(:,4);
PRK4  = YRK4(:,5);
YEuler = fEuler(f,T,Y0);
M1Euler = YEuler(:,1);
M2Euler = YEuler(:,2);
M3Euler = YEuler(:,3);
OEuler  = YEuler(:,4);
PEuler  = YEuler(:,5);
plot (T,M1Euler,'r',T,M1RK4,'b');
legend('Euler', 'RK4');
xlabel('time (days)');
ylabel ('M1 (gr/ml)');
title('M1');
function Y = fEuler(f,T,Y0)
  N = numel(T);
  n = numel(Y0);
  Y = zeros(N,n);
  Y(1,:) = Y0;
  for i = 2:N
    t = T(i-1);
    y = Y(i-1,:);
    h = T(i) - T(i-1);
    Y(i,:) = y + h*f(t,y);
  end
end
function Y = fRK4(f,T,Y0)
  N = numel(T);
  n = numel(Y0);
  Y = zeros(N,n);
  Y(1,:) = Y0;
  for i = 2:N
    t = T(i-1);
    y = Y(i-1,:);
    h  = T(i) - T(i-1);
    k0 = f(t,y);
    k1 = f(t+0.5*h,y+k0*0.5*h);
    k2 = f(t+0.5*h,y+k1*0.5*h);
    k3 = f(t+h,y+k2*h);
    Y(i,:) = y + h/6*(k0+2*k1+2*k2+k3);
  end
end
function CM1 = myode (~,MM)
  M1 = MM(1);
  M2 = MM(2);
  M3 = MM(3);
  O  = MM(4);
  P  = MM(5);
  delta=50;
  gamma=75;
  K1= 10^-4;
  K2=5*10^-4;
  K3=10^-3;
  Ko=0.1;
  n=3;
  Oa=10;
  Pa=100;
  mu_1=10^-3;
  mu_2=10^-3;
  mu_3=10^-3;
  mu_o=10^-4;
  mu_p= 10^-5;
  CM1= zeros(1,5);
  CM1(1) = (delta*M1*(1-(M1/gamma))-2*K1*M1*M1-M1*(K2*M2)-((Oa-n)*K3*M1*M3)-((Pa-Oa)*Ko*M1*O)-(mu_1*M1));
  CM1(2) = (K1*M1*M1)-(K2*M1*M2)-(mu_2*M2);
  CM1(3) = (K2*M1*M2)-(K3*M1*M3)-(mu_3*M3);
  CM1(4) = (K3*M1*M3)-(Ko*M1*O)-(mu_o*O);
  CM1(5) = (Ko*M1*O)-(mu_p*P);
end
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





