MATLAB Answers

# Plotting multiple functions, while showing only parts of them

8 views (last 30 days)
Maxtron Moon on 27 Jun 2020
Commented: Mara on 2 Jul 2020
So, I have this code that plots the blackbody spectrum for two temperatures (so, technically two functions). The problem is that I don't want to plot the entire functions. I want the blue graph to stop after it reaches its maximum (given by the vertical line) so the part right to the max, and the orange plot to show only parts after it reaches its max (only parts to its right), because I want to plot a third linear function to connect these two maximums, Is that possible?
% plot the blackbody curves - spectral radiance at the aperture of the
% blackbody (L(nu,T)=(c/4pi)*u(nu,T)) where u is the spectral
% distribution of radiant energy in the blackbody
freq = c./(1e-6.*[0.0001:0.0001:0.05 0.05:0.01:10000.0]); % (Hz)
A = (h.*freq)./(k.*Tstar); % (dimensionless)
B = (2.0.*h./c^2).*(freq).^3; % (W/m2/sr/Hz)
BBSpectralRad = B.*(1.0./(exp(A)-1)); % (W/m2/sr/Hz)
plot(freq,BBSpectralRad);
hold all;
plot([freqStar freqStar],[1e-15 max(BBSpectralRad)], ...
'--','color',[0.5 0.5 0.5]);
grid on
hold all;
% label the plot
xlim([1e11 1e17]);
ylim([1e-15 1e-4]);
set(gca, 'YScale', 'log');
set(gca, 'XScale', 'log');
text(2e11,1e-7, ...
'$\nu_{peak} = 5.879\times 10^{10} T$', 'fontsize',16,'Interpreter','latex');
xlabel('\bf\fontname{arial}\fontsize{14}Frequency (\nu) [Hz]');
ylabel('\bf\fontname{arial}\fontsize{14}Spectral Radiance [W m^{-2} sr^{-1} {Hz}^{-1}]');
title('\bf\fontname{arial}\fontsize{14}Blackbody Radiation','Fontsize',14);
hold on
Tstar = 18974; % (K)
freqStar = WeinLawCnst.*Tstar; % (Hz)
freq = c./(1e-6.*[0.0001:0.0001:0.05 0.05:0.01:10000.0]); % (Hz)
A = (h.*freq)./(k.*Tstar); % (dimensionless)
B = (2.0.*h./c^2).*(freq).^3; % (W/m2/sr/Hz)
BBSpectralRad = B.*(1.0./(exp(A)-1)); % (W/m2/sr/Hz)
plot(freq,BBSpectralRad);
hold all;
plot([freqStar freqStar],[1e-15 max(BBSpectralRad)], ...
'--','color',[0.5 0.5 0.5]);
grid on
hold all;
##### 1 CommentShowHide None
Ameer Hamza on 27 Jun 2020
Can you share the value of variables needed to run this code?

Sign in to comment.

### Answers (1)

Mara on 27 Jun 2020
Hello,
you can plot only to the maximal y-value by finding the index of it in your vector of y-values. When you plot x and y, you specify that you only want to plot to that point or from that point on to the end.
idx = find(y==max(y));
plot(x(1:idx), y(1:idx)) % or plot(x(idx:end), y(idx:end))
you can also retrieve and change the x and y data later from the properties of your axes:
p = plot(x,y)
idx = find(p.YData == max(p.YData));
set(p, 'XData' , p.XData(1:idx), 'YData', p.YData(1:idx))
##### 2 CommentsShowHide 1 older comment
Mara on 2 Jul 2020
Ah, alright, thank you!

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!