How to combine two graph from two function in another script?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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?
採用された回答
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 件のコメント
Thank you for your response @Jon . How to combine two plot from two different function? I want combine plot from euler and Runge-Kutta
Example
%function in script 1
Using euler method
%function in script 2
using Runge-Kutta method
Make script1 a function, make script2 another function, call both functions from a new script and plot the results from the functions in the new script.
I don't think Matlab has a built in Runge-Kutta ode solver, or a Euler Method solver. But, just just to illustrate, here's how you would compare MATLAB's ode45 and ode23 solutions in one plot. You can modify appropriately substituting your own Euler and Runge-Kutta solver functions, and ode function to be solved once you have written them.
Note that as the solutions are very similar in this case it will look like there is just one curve plotted unless you zoom way in
% Solve dx/dt = -x, x0 = 10
[t23,x23] = ode23(@(t,x) -x,[0,5],10);
[t45,x45] = ode45(@(t,x) -x,[0,5],10);
% plot both solutions
plot(t23,x23,t45,x45)
xlabel('time')
ylabel('x')
legend('ode23','ode45')

I already try but I get error (error using plot, my vectors must be same length) @Torsten , my code like this
%Euler function
%input parameter
delta=50;
K1= 10^-4;
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;
K2=5*10^-4;
K3=10^-3;
gamma=75;
%input initial condition
M11(1)=10;
M22(1)=0;
M33(1)=0;
O1(1)=0;
P1(1)=0;
%input for time
t(1)=0;
dt=0.01; %time interval
t=0:dt:100; %time span
%input empty array
T=zeros(length(t)+1,1); %empty array for t
M11=zeros(length(t)+1,1); %empty array for M1
M22=zeros(length(t)+1,1); %empty array for M2
M33=zeros(length(t)+1,1); %empty array for M3
O1=zeros(length(t)+1,1);
P1=zeros(length(t)+1,1);
for i = 1:length(t)
T(i+1)=T(i)+dt;
M11(i+1)=M11(i)+1./(1+exp(-T(i)));
M11(i+1) = M11(i)+(dt*(delta*M11(i+1)*(1-(M11(i+1)/gamma))-2*K1*M11(i+1)*M11(i+1)-M11(i+1)*(K2.*M22(i+1))-((Oa-n)*K3*M11(i+1)*M33(i+1))-((Pa-Oa)*Ko*M11(i+1)*O1(i+1))-(mu_1*M11(i+1))));
M22(i+1) = M22(i)+(dt*(K1*M11(i)*M11(i)-K2*M11(i)*M22(i))-(mu_2*M22(i+1)));
M33(i+1) = M33(i)+(dt*(K2*M11(i)*M22(i)-K3*M11(i)*M33(i)-mu_3*M33(i)));
O1(i+1) = O1(i)+(dt*(K3*M11(i)*M33(i)-Ko*M11(i)*O1(i)-mu_o*O1(i)));
P1(i+1) = P1(i)+(dt*(Ko*M11(i)*O1(i)-mu_p*P1(i)));
end
%RK function
tstart = 0;
tend = 100;
dt = 0.01;
T = (tstart:dt:tend).';
Y0 = [10 0 0 0 0];
f = @myode;
Y = fRK4(f,T,Y0);
M1 = Y(:,1);
M2 = Y(:,2);
M3 = Y(:,3);
O = Y(:,4);
P = Y(:,5);
plot (T,M11,'r-',T,M1,'r');
Error using plot
Vectors must be the same length.
Vectors must be the same length.
legend('Euler', 'RK4');
xlabel('time (days)');
ylabel ('M1 (gr/ml)');
title('M1');
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
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 件)
カテゴリ
ヘルプ センター および File Exchange で Structural Mechanics についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
