Adding Color Under Certain Areas of Function Plot

조회 수: 12 (최근 30일)
Chase Valo
Chase Valo 2019년 12월 13일
댓글: Star Strider 2019년 12월 15일
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

채택된 답변

Star Strider
Star Strider 2019년 12월 14일
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
Chase Valo
Chase Valo 2019년 12월 15일
That worked perfectly! Thank you very much!
Star Strider
Star Strider 2019년 12월 15일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2019년 12월 13일
Try patch() or fill()
  댓글 수: 2
Chase Valo
Chase Valo 2019년 12월 14일
I have tried both patch() and fill() however, it says the input must be numerical when trying to use my function
Image Analyst
Image Analyst 2019년 12월 15일
편집: Image Analyst 2019년 12월 15일
Of course. Star Strider is passing in numbers. What were you trying to pass in? A character string???

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Color and Styling에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by