1 view (last 30 days)

I'm a Matlab newbie and am struggling to get the right syntax for numerically integrating a simple anonymous function on one of its variables. The M_e function (Planck's law) below is supposed to set up x (the wavelength) as the variable of interest, while the values of other parameters (h, c, k, T) are provided in earlier lines. M_e_int should integrate this function between two user-input wavelengths (lambda1, lambda2). However, I'm getting an unspecified error on the M_e_int line.

What am I doing wrong? Do I need to define x as a scalar somehow?

M_e = @(x) (2. * pi * h * c.^2) / (x.^5 * (exp((h * c)/(x * k * T)) - 1));

M_e_int = integral(M_e,lambda1,lambda2)

Stephan
on 6 Sep 2018

Edited: Stephan
on 6 Sep 2018

Hi,

try:

M_e = @(x) (2 * pi * h * c^2) ./ (x.^5 .* (exp((h * c)./(x * k * T)) - 1));

M_e_int = integral(M_e,lambda1,lambda2)

Best regards

Stephan

Walter Roberson
on 6 Sep 2018

The format

@(variable1, variable2, variable3, ...) expression involving those variables

is mostly the same as if you had written @InternallyGeneratedFunctionName together with

function output = InternallyGeneratedFunctionName(variable1, variable2, variable3, ...)

output = expression involving those variables;

There are some differences involving scope of variables

In short, what appears in the () after the @ gives the names of the dummy variables used to receive the values passed in positionally when the function is called.

This is almost the same as saying that x is the variable that the function will be integrated on, as long as you keep in mind that you are doing numeric integration and that it is a positional notation not a symbolic notation. Like in

syms x

y = x.^2;

integral(@(x) diff(y, x), 1, 2)

is not the same as

f = diff(y, x);

integral(@(x) f(x), 1, 2)

because in integral(@(x) diff(y, x), 1, 2) the x will be a numeric value that has no relationship to the symbolic x present in the expression y.

Sign in to comment.

YT
on 6 Sep 2018

You're missing some dots in your expression. Dots stand for element-wise multiplication when using arrays/matrices, so you need them when your doing calculations with 'x'

M_e = @(x) (2 * pi * h * c^2)./(x.^5.*(exp((h * c)./(x * k * T)) - 1));

M_e_int = integral(M_e,lambda1,lambda2)

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.