x(x-y) Posted January 30, 2013 Posted January 30, 2013 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 constantsh = 6.6260E-34; % Planck's Constantc = 2.9979E8; % Speed of Lightk = 1.3806E-23; % Boltzmann's Constant% Define array of radiation wavelengthsL = linspace(100E-9,3000E-9,1000);% For-loop calling the function for intensityfor T = 3000:1000:8000 = intensity(L,T,c,h,k);endplot(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.
mathematic Posted January 30, 2013 Posted January 30, 2013 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.
x(x-y) Posted January 30, 2013 Author Posted January 30, 2013 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.
Cap'n Refsmmat Posted January 31, 2013 Posted January 31, 2013 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.
x(x-y) Posted January 31, 2013 Author Posted January 31, 2013 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.
x(x-y) Posted February 1, 2013 Author Posted February 1, 2013 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.
mathematic Posted February 3, 2013 Posted February 3, 2013 I still think putting plot inside the loop will do the trick.
x(x-y) Posted February 3, 2013 Author Posted February 3, 2013 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.
mathematic Posted February 3, 2013 Posted February 3, 2013 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.
Bignose Posted February 3, 2013 Posted February 3, 2013 You have to set up a handle for each figure, then when you create a new line, you specify to plot it on that same handle. The plot command by default starts anew each time See http://stackoverflow.com/questions/12134406/matlab-several-graphs-in-1-loop-each-iteration-adds-a-line-on-every-figure for example.
x(x-y) Posted February 4, 2013 Author Posted February 4, 2013 You have to set up a handle for each figure, then when you create a new line, you specify to plot it on that same handle.The plot command by default starts anew each timeSee http://stackoverflow.com/questions/12134406/matlab-several-graphs-in-1-loop-each-iteration-adds-a-line-on-every-figure for example. Ah, I see, that was very helpful - thanks.
fghjkqwe Posted February 16, 2013 Posted February 16, 2013 (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 February 16, 2013 by fghjkqwe
cadekd Posted April 23, 2013 Posted April 23, 2013 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now