Jump to content

Recommended Posts

Posted

I have been trying to plot a number of black body radiator spectra curves in MATLAB but I come across a problem - only one curve is plotted when I run the program. Essentially, I am plotting intensity I against wavelength [latex]\lambda[/latex] for an array of temperatures from 3000K to 8000K in steps of 1000K with a wavelegnth array of 100nm to 3000nm with 1000 intervals, using Planck's radiation formula:

 

[latex]I(\lambda,T) = \frac{2\pi c^2 h}{\lambda^5} \left(\frac{1}{e^\frac{hc}{\lambda kT} -1} \right)[/latex]

 

where T is th absolute temperature, c is speed of light, h is Planck's constant and k is Boltzmann's constant.

 

Here is my code:

 

Function File

 

function = intensity(L,T,c,h,k)

A = (2.*pi.*(c.^2).*h)./(L.^5);
B = exp((h.*c)./(L.*k.*T));

I = A.*((1)./(B - 1));

 

Main File

 

% Part A

% Define constants


h = 6.6260E-34; % Planck's Constant
c = 2.9979E8; % Speed of Light
k = 1.3806E-23; % Boltzmann's Constant

% Define array of radiation wavelengths

L = linspace(100E-9,3000E-9,1000);

% For-loop calling the function for intensity

for T = 3000:1000:8000
= intensity(L,T,c,h,k);
end

plot(L,)

xlabel('Wavelength L (m)');
ylabel('Intensity I (W m^{-2})');
title('Black Body Radiation Spectra');
grid on;

 

Any help will be greatly appreciated, thanks!

 

 

P.S: Just in case it's difficult to read - there are dots in front of the operators which matter in the equation in order for the program to recognise element-by-element computation.

Posted

I am not familiar with the language you are using. However shouldn't "end" be after "plot"? Alternatively should be a set of answers. The code may be using the last value.

Posted

I am not familiar with the language you are using. However shouldn't "end" be after "plot"? Alternatively should be a set of answers. The code may be using the last value.

The "end" is to denote the end of the for loop - "plot" can be defined inside the for loop but it would have to be written in a different way than simply defining it outside the loop as I have done.

 

If I print to the command window then it is appearing as an array - however, what concerns me is that a lot of the values are equal to zero for some reason.

Posted

It looks like you keep overwriting inside your loop, so the only data left after the loop exits will be the last call to intensity(). My MATLAB knowledge is minimal though, so I may be entirely wrong.

Posted

Hmmm, I see, yes that could be the problem. Perhaps I need another for loop within the first one where I can define the wavelength array too. I'll try to solve this later after some sleep.

Posted

So, I managed to obtain the right plot in the end by using several for loops for the different temperatures. However, I am sure there is a quicker and more efficient way to achieve this - I am fairly new to MATLAB, so I guess I'll learn these things over time.

Posted

I still think putting plot inside the loop will do the trick.

 

I tried that and it does not do the trick - I tried both of these:

 

 

plot(L,I)

 

plot(L,I(:,T))

 

inside the for loop and they both yielded the same result - only one curve was plotted; i.e. all the other temperatures were overwritten except for 8000K. Thus, somehow I need to extract an array for each temperature result for the intensity from the single for loop and then plot a graph of these. Instead, for now, I have just written multiple for loops for each temperature which has given me the graph I wanted - I will keep thinking of a way to do this as described above.

Posted

I am not familiar with the mechanics of the program, but when you say "plot" where does it put the information? If it is somewhere in memory, you need to able to specifiy where it is going so that it doesn't keep putting it in the same place.

  • 2 weeks later...
Posted (edited)

" By the way, no this is not a "homework" question - I am not the kind of person who cheats in such a way for homework answers; however, evidently I cannot prove this so believe this is a homework question if you want to, it makes no difference to me as I know it isn't!"

 

Also if you move the plot into the for loop but type "hold on" (without quotation marks) before it it will plot all the graphs.

Edited by fghjkqwe
  • 2 months later...
Posted

I hope is not too late for an answer here...

 

You have two options for plot what you want:

 

1. Add an index to the "I" in to the loop:

i=1;

for T = 3000:1000:8000
[i(:,i)] = intensity(L,T,c,h,k);

i=i+1;

end

 

2. just add the plot inside the loop but you need to include the "hold on" instruction to keep the previous plot.

 

for T = 3000:1000:8000
= intensity(L,T,c,h,k);
plot(L,)

hold on

end

grid on

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.