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

9 ビュー (過去 30 日間)
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
Stephan 2018 年 9 月 6 日
編集済み: Stephan 2018 年 9 月 6 日
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 件のコメント
Paul Fini
Paul Fini 2018 年 9 月 6 日
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
Walter Roberson 2018 年 9 月 6 日
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.

サインインしてコメントする。

その他の回答 (1 件)

YT
YT 2018 年 9 月 6 日
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)

カテゴリ

Help Center および File ExchangeLinear Algebra についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by