How to plot solutions for different values of parameters on same file?

1 view (last 30 days)
How to plot solutions for different values of parameters on same file i..e,
close all;
X=-5:.1:5;
T=-5:.1:5;
%i=1;
w0=.5;
%h=3;
eta=(1./2);
sig=-1;
e0=2*w0*sqrt(eta-sig);
mu1=.5*1i;
[x,t]=meshgrid(X,T);
%a=4*mu1.^2-(w0.^2./2);
A=(1./(-2*w0.^2+2*mu1.^2)*sqrt(e0.^2-4*mu1.^2).*((-2*w0.^2+2*mu1.^2)*x+t));
a=e0*sinh(A)-2*1i*mu1;
b=e0+2*1i*mu1*sinh(A);
r1=e0-4*1i*mu1*(a./b);
surf(x,t,abs(r1));
shading flat;
grid off;
How to plot 'r1' for different values of 'mu1' on same figure like this

Answers (1)

Vedant Shah
Vedant Shah on 30 Jun 2025
To plot solutions for different values of a parameter on the same figure, the hold function in MATLAB can be utilized. By looping through various values of mu1, calculating r1 for each, and plotting the corresponding surface using the surf function, it is possible to visualize multiple solutions simultaneously. Assigning distinct colours to each surface and adding a legend increases the interpretability of the plot.
The following sample code demonstrates this approach:
mu1_list = [0.5i, 1i, 1.5i];
figure;
hold on;
colors = lines(length(mu1_list));
for k = 1:length(mu1_list)
mu1 = mu1_list(k);
A = (1./(-2*w0.^2 + 2*mu1.^2) * sqrt(e0.^2 - 4*mu1.^2) .* ((-2*w0.^2 + 2*mu1.^2)*x + t));
a = e0 * sinh(A) - 2*1i*mu1;
b = e0 + 2*1i*mu1 * sinh(A);
r1 = e0 - 4*1i*mu1 * (a ./ b);
surf(x, t, abs(r1), 'FaceAlpha', 0.6, 'EdgeColor', 'none', 'FaceColor', colors(k,:));
end
shading flat;
grid off;
xlabel('x');
ylabel('t');
zlabel('|r_1|');
title('Solutions for different values of \mu_1');
legend(arrayfun(@(m) sprintf('\\mu_1 = %.1fi', imag(m)), mu1_list, 'UniformOutput', false));
hold off;
The result obtained using the above sample code is as follows:
The resulting figure displays the surfaces corresponding to different values of mu_1 on the same axes, each distinguished by a unique colour and identified in the legend.
For further information, refer to the following documentations:

Community Treasure Hunt

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

Start Hunting!