MATLAB Answers

why does this integral fail?

2 views (last 30 days)
William Rose
William Rose on 27 Jun 2021
Commented: William Rose on 27 Jun 2021
I created an anonymous function.
fun=@(x,t,A,w,t1) (A*w/pi)*(1-exp(-(t/exp(x))^2))/((x-log(t1))^2+w^2)
It gives reasonable results by itself:
>> disp([fun(-10,1e-4,1,1,1),fun(0,1e-4,1,1,1),fun(10,1e-4,1,1,1)])
0.0031 0.0000 0
When I try to integrate it, I get an error:
>> q=integral(@(x)fun(x,1e-4,1,1,1),-1,1)
Error using /
Matrix dimensions must agree.
Error in @(x,t,A,w,t1)(A*w/pi)*(1-exp(-(t/exp(x))^2))/((x-log(t1))^2+w^2)
Error in @(x)fun(x,1e-4,1,1,1)
I expect all terms in the function to be scalars, so I don't understand this error. The function works without error when called on its own, as in the disp(...) command shown above. Changing the limits of integration did not change the error rmessage.
What I have tried: The function has three "/" in it, so I changed each one to "./", separately. The changed function worked fine on its own, in all three cases. When I replaced the first or third "/" by "./", I got the same error as before with integral(...). When I replaced the second "/" with"./", I got a different error:
>> fun2=@(x,t,A,w,t1) (A*w/pi)*(1-exp(-(t./exp(x))^2))/((x-log(t1))^2+w^2)
>> q=integral(@(x)fun2(x,1e-4,1,1,1),-1,1)
Error using ^
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square
and the power is a scalar. To perform elementwise matrix powers, use '.^'.
Error in @(x,t,A,w,t1)(A*w/pi)*(1-exp(-(t./exp(x))^2))/((x-log(t1))^2+w^2)
Error in @(x)fun2(x,1e-4,1,1,1)
This makes me suspect that the term "(t/exp(x))" is the problem, and that either the numerator or denominator is a vector when called by integral() - but it works fine when called on its own. Thank you in advance for your assistance.

Accepted Answer

Matt J
Matt J on 27 Jun 2021
Edited: Matt J on 27 Jun 2021
I expect all terms in the function to be scalars
If so then, use the ArrayValued option.
fun=@(x,t,A,w,t1) (A*w/pi)*(1-exp(-(t/exp(x))^2))/((x-log(t1))^2+w^2);
q=integral(@(x)fun(x,1e-4,1,1,1),-1,1,'ArrayValued',true)
q = 8.2849e-09
  1 Comment
William Rose
William Rose on 27 Jun 2021
Thanks! I read about that option in the hep and thougtht it was the opposite of what I wanted. I should have just tried it.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!