Plot titles in subplots for various initial conditions
이전 댓글 표시
Hello. I have this code below and i need to put the four different values of y0 in each subplot title. How do i do this ? Is it possible to be done in one of the for loops ?
clc, clear all, close all
%--- Solving the diff. eqn. y'(t) = y(t) - c*y^2(t) with c = 0.5 using
%--- Euler's method and ode45 and comparing in same graph. I have four
%--- initial values for y0
tmax = 100;
y0 = [0.1 0.5 1.0 2.0];
f = @(t,y) y - 0.5*y.^2;
h = 0.1;
t = [0:tmax];
n = length(t)-1;
y = zeros(1,n);
for i = 1:length(y0)
[T,Y] = ode45(f,[0 tmax],y0(i));
subplot(2,2,i), plot(T,Y,'o'), hold on
for j = 1:n
y(1) = y0(i);
y(1,j+1) = y(j) + h*f(':',y(j));
end
subplot(2,2,i), title('y'' = y - 0.5y^2, y_0 = 0.1'), plot(t,y),legend('Exact','Euler','Location','Best'), xlabel('t'), ylabel('y')
end
채택된 답변
추가 답변 (1개)
To include the different values of y0 in each subplot title, you can modify the title within the loop to dynamically include the current y0 value. You can achieve this by using the num2str function to convert the numerical value to a string and then concatenate it into the title.
clc, clear all, close all
%--- Solving the diff. eqn. y'(t) = y(t) - c*y^2(t) with c = 0.5 using
%--- Euler's method and ode45 and comparing in same graph. I have four
%--- initial values for y0
tmax = 100;
y0 = [0.1 0.5 1.0 2.0];
f = @(t,y) y - 0.5*y.^2;
h = 0.1;
t = [0:tmax];
n = length(t)-1;
y = zeros(1,n);
for i = 1:length(y0)
[T,Y] = ode45(f,[0 tmax],y0(i));
subplot(2,2,i), plot(T,Y,'o'), hold on
for j = 1:n
y(1) = y0(i);
y(1,j+1) = y(j) + h*f(':',y(j));
end
% Update the title to include the current y0 value
subplot(2,2,i), title(['y'' = y - 0.5y^2, y_0 = ', num2str(y0(i))]), ...
plot(t,y), legend('Exact','Euler','Location','Best'), xlabel('t'), ylabel('y')
end
카테고리
도움말 센터 및 File Exchange에서 Title에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



