Plot titles in subplots for various initial conditions
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (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
Categorías
Más información sobre Title en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



