Syntax for numerically integrating an anonymous function on one of its variables
5 次查看(过去 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)
0 个评论
采纳的回答
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 个评论
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
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)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!