# Syntax for numerically integrating an anonymous function on one of its variables

8 vues (au cours des 30 derniers jours)
Paul Fini le 6 Sep 2018
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)
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Stephan le 6 Sep 2018
Modifié(e) : Stephan le 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
##### 2 commentairesAfficher AucuneMasquer Aucune
Paul Fini le 6 Sep 2018
Thanks for that, Stephan. Coming from (basic) Python, I'm not used to dealing with variables as matrices/arrays by default.
I'm still not quite clear on the use of "@(x)" for anonymous functions... is this to tell Matlab that x is the variable which the function will be integrated on?
Walter Roberson le 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.

Connectez-vous pour commenter.

### Plus de réponses (1)

YT le 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)
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Paul Fini le 6 Sep 2018
Thank you YT, this cleared things up.

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Linear Algebra dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by