# why does this integral fail?

조회 수: 2(최근 30일)
William Rose 2021년 6월 27일
댓글: William Rose 2021년 6월 27일
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.

댓글을 달려면 로그인하십시오.

### 채택된 답변

Matt J 2021년 6월 27일
편집: Matt J 2021년 6월 27일
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표시숨기기 없음
William Rose 2021년 6월 27일
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.

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!