Adding Color Under Certain Areas of Function Plot

I need to add color to certain areas under the plotted function but can not get anything I try to work. Help would be much appreciated.
This is what I have so far:
%% Part 1: Solar Radiation
%Constants:
h = 6.626e-34;
k = 1.381e-23;
c = 2.998e8;
t = 5800;
r_sun = 1.39e9/2;
r_earth = 1.5e11;
f_w = (r_sun/r_earth)^2;
%Function:
L_sun =@(l) f_w*((2*pi*h*c^2)./l.^5).*(1./(exp(h*c./(l*k*t))-1));
%Plotting Function:
figure(1)
fplot (L_sun, [0,3e-6],'LineWidth',2)
x0=10;
y0=10;
width=675;
height=500;
title('Solar Spectral Irradiance','fontsize',14)
ylabel('Spectral irradiance(W*m^-^2)','fontsize',14)
xlabel('Wavelegth(m)','fontsize',14)
axes('pos',[.4 .6 .5 .3])
%imshow('equation(1).png')
%% Part 2: Energy Distrubution
%I added below ths line when using the bounds use the nanometers since
% we are in meters everthing needs to be conveted.
l = [0:0.01e-6:3e-6];
X = f_w*((2*pi*h*c^2)./l.^5).*(1./(exp(h*c./(l*k*t))-1));
Max = max(X) %maximum irradiance
%Energy recieved by region:
UV = integral(L_sun,0,0.38e-6)
Vis = integral(L_sun,0.38e-6,0.78e-6)
IR = integral(L_sun,0.78e-6,3e-6)
This is the plot I currently have:
MySpectral.PNG
Below is what I am trying to accomplish:
Spectral.PNG

 Accepted Answer

Try this:
% %% Part 1: Solar Radiation
%Constants:
h = 6.626e-34;
k = 1.381e-23;
c = 2.998e8;
t = 5800;
r_sun = 1.39e9/2;
r_earth = 1.5e11;
f_w = (r_sun/r_earth)^2;
%Function:
L_sun = @(l) f_w*((2*pi*h*c^2)./l.^5).*(1./(exp(h*c./(l*k*t))-1));
patchfun = @(x,c) patch([x fliplr(x)], [L_sun(x) zeros(size(x))], c);
%Plotting Function:
figure(1)
fplot (L_sun, [0,3e-6],'LineWidth',2)
hold on
region_1 = linspace(0.1,0.4,25)*1E-6;
patchfun(region_1,'r')
region_2 = linspace(0.4, 0.75, 25)*1E-6;
patchfun(region_2, 'g')
region_3 = linspace(0.75, 3, 25)*1E-6;
patchfun(region_3, 'b')
hold off
This is only the part of your code that is relevant to your Question. (The patch function requires a bit of practice to use.) Since the function call is the same for the various regions, I created the anonymous function ‘patchfun’, and then called it for each region.
Experiment to get the result you want.
Adding Color Under Certain Areas of Function Plot - 2019 12 13.png

2 Comments

That worked perfectly! Thank you very much!
As always, my pleasure!

Sign in to comment.

More Answers (1)

Try patch() or fill()

2 Comments

I have tried both patch() and fill() however, it says the input must be numerical when trying to use my function
Of course. Star Strider is passing in numbers. What were you trying to pass in? A character string???

Sign in to comment.

Categories

Products

Release

R2017a

Community Treasure Hunt

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

Start Hunting!