Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

How to generate matrix filled with function values

Asked by Jack Dempsey on 16 Jan 2013

I'm trying to create a surface plot of the Plack radiation law over a range of temperatures and wavelengths, but when so far I've been unsuccessful. I've tried to use meshgrid to set my range of values as

EDU>> [l,T] = meshgrid(0:25:1000,0:300:12000);

where l is wavelength in nm and T is temperature in Kelvins. However, this means that when I plug it into my energy distribution function, I get a matrix filled with the same row.

distr = Planck(l,T);

where Planck is the function defined by

function [ distr ] = Planck( lam,temp )

%Applies the Planck radiation law to give the energy emitted by a black

%body for some given wavelength and temperature. Units are dimensionless,

%and given by u(lamda,T)/(8*pi*c*h/((550nm)^5).

% Input:

%lam = the actual wavelength

%temp = the actual temperature

%For our reference wavelength, we will use 550 nm, meaning that our

%reference temperature will be 26180 in accordance with the relationship

%T-knot = (h*c)/(lambda-knot*k)

L = lam/(550);

T = temp/(26180);

e = exp(1);

distr = (1./(L.^5))./(e.^(1./(L*T))-1);

end

Having used this to create a number of 2-D plots, the 3-D version should have lower z values for lower temperatures, but because all of the rows of distr are the same, the function is being displayed as constant with respect to T. So if I have a range of x values and a range of y values, and z is a function of x and y, how do I create a matrix filled with z-values for each combination of x and y in that range?

4 Comments

Jack Dempsey on 18 Jan 2013

Could you please be a little bit more specific in your criticism? For future reference, what exactly should I be aware of?

Cedric Wannaz on 18 Jan 2013

Was note your code per se I guess but your post. Just by adding a few spaces on lines starting with code and using || to make e.g. variable names standing out in chunks of text, you would have made it more readable:

I'm trying to create a surface plot of ...

 EDU>> [l,T] = meshgrid(0:25:1000,0:300:12000);

where l is wavelength in nm and T is temperature in Kelvins. However, this means that when I plug it into my energy distribution function, I get a matrix filled with the same row.

distr = Planck(l,T);

where Planck is the function defined by

function [ distr ] = Planck( lam,temp )
   % Applies the Planck radiation law to give the energy emitted by a 
   % black body for some given wavelength and temperature. Units are 
   % dimensionless, and given by u(lamda,T)/(8*pi*c*h/((550nm)^5).
   % Input:
   %    lam = the actual wavelength
   %    temp = the actual temperature
   % For our reference wavelength, we will use 550 nm, meaning that our
   % reference temperature will be 26180 in accordance with the 
   % relationship T-knot = (h*c)/(lambda-knot*k)
   L = lam/(550);
   T = temp/(26180);
   e = exp(1);
   distr = (1./(L.^5))./(e.^(1./(L*T))-1);
end

Having used this to create a number of 2-D plots, the 3-D version should have lower z values for lower temperatures, but because all of the rows of distr are the same, the function is being displayed as constant with respect to T. So if I have a range of x values and a range of y values, and z is a function of x and y, how do I create a matrix filled with z-values for each combination of x and y in that range?

Jack Dempsey on 19 Jan 2013

Alright, I'll keep that in mind. Thanks.

Jack Dempsey

Products

No products are associated with this question.

1 Answer

Answer by Thorsten on 16 Jan 2013
Accepted answer

You have to use element-wise multiplication L.*T:

    distr = (1./(L.^5))./(e.^(1./(L.*T))-1);

0 Comments

Thorsten

Contact us