Plot titles in subplots for various initial conditions

3 visualizaciones (últimos 30 días)
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

Manikanta Aditya
Manikanta Aditya el 29 de Dic. de 2024
Editada: Manikanta Aditya el 29 de Dic. de 2024
You can modify the subplot titles dynamically within the loop by using the current value of y0(i).
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 = ', num2str(y0(i))]), plot(t,y), legend('Exact','Euler','Location','Best'), xlabel('t'), ylabel('y')
end
In this modified code, the title function within the for loop dynamically sets the title for each subplot to include the corresponding initial condition ( y_0 ). The num2str function converts the numerical value of ( y_0(i) ) to a string so it can be included in the title.
I hope this helps you.
  2 comentarios
Left Terry
Left Terry el 29 de Dic. de 2024
Editada: Left Terry el 29 de Dic. de 2024
@Manikanta Aditya I think the sprintf(...) version worked better for me. Why did you edited your answer ? Was there a mistake ?
Manikanta Aditya
Manikanta Aditya el 29 de Dic. de 2024
Editada: Manikanta Aditya el 29 de Dic. de 2024
@Left Terry, No no nothing is wrong, this is another way, I found, so updated with this.
Please find the 'sprintf' version below:
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 the 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
titleStr = sprintf('y'' = y - 0.5y^2, y_0 = %.1f', y0(i)); % Create dynamic title
subplot(2,2,i), title(titleStr), plot(t,y), legend('Exact','Euler','Location','Best'), xlabel('t'), ylabel('y')
end
  • The sprintf function is used to create a string that includes the current initial value y0(i) for each subplot title.
  • The title function is updated to use this dynamically created string.

Iniciar sesión para comentar.

Más respuestas (1)

Naga
Naga el 29 de Dic. de 2024
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 Programming en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by