Wrong numerical integration value but no error

조회 수: 9 (최근 30일)
Zozo torc
Zozo torc 2018년 4월 3일
편집: John D'Errico 2018년 4월 3일
Hi all,
I have been running in a issue I do not understand this morning, the "integrate" function in Matlab gives me a false result for some integral.
The function to integrate is:
f(x,t) = ((1+cosh(2*t*x))/sinh(2*pi*x))*tanh((pi-t)*x)
I want to integrate this function over 0-->Inf but matlab would not do it so I tried (0,1) with t=1 for testing instead. On this interval we have f(0)-->0.68 and f(1)~0.0173 but matlab gives me an integral of 0.01596 which is below the lowest value of the function...
So I tried integrating the function on something else (TI-89 calculator) and got 0.258 as the integral which seems way more reasonable.
Does anyone knows why matlab gives me a wrong value ?
Thanks a lot !
  댓글 수: 1
Torsten
Torsten 2018년 4월 3일
편집: Torsten 2018년 4월 3일
You did not yet show the MATLAB code you are using.
And do you see the problem here:
https://www.wolframalpha.com/input/?i=plot+(1%2BCosh%5B2*x%5D)%2FSinh%5B2*Pi*x%5D*Cosh%5B(Pi-1)*x%5D%2FSinh%5B(Pi-1)*x%5D
?
Best wishes
Torsten.

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

답변 (2개)

John D'Errico
John D'Errico 2018년 4월 3일
편집: John D'Errico 2018년 4월 3일
Here is the code you wrote:
fct_bad = @(tau,theta) ((1+cosh(2*tau*theta))/(sinh(2*pi*tau)))*tanh((pi-theta)*tau);
Lets plot the function that you wrote:
ezplot(@(tau) fct_bad(tau,theta),[0 1])
So as you wrote the function, it produces garbage, the integral of which was some non-meaningful value.
The problem is, the code used the wrong operators. Code where the arguments will be vectors or arrays should use the ./ and .* operators. Integral will assume your function accepts a vector of values.
fct = @(tau,theta) ((1+cosh(2*tau*theta))./(sinh(2*pi*tau))).*tanh((pi-theta)*tau);
By use of the wrong operators, / and * there, you created a function that actually did manage to work, surprisingly without error, but it gave the wrong answer.
theta = 1;
ezplot(@(tau) fct(tau,theta),[0 1])
As you can see, when written properly, I get:
integral(@(tau) fct(tau,theta),0,1)
ans =
0.25828
Please unaccept your answer (that is not in fact an answer) and accept mine, which explains why you need to change the function and how to fix your problem.

Zozo torc
Zozo torc 2018년 4월 3일
Sorry for the code I forgot it:
theta=1;
fct = @(tau,theta) ((1+cosh(2*tau*theta))/(sinh(2*pi*tau)))*tanh((pi-theta)*tau);
test = integral(@(tau)fct(tau,1),0,1);
disp(test);
About the wolframalpha link you posted the function is wrong you put cotanh instead of tanh :)
Best,
  댓글 수: 3
John D'Errico
John D'Errico 2018년 4월 3일
Why are you accepting your own answer you posted that has no reason except to add information to your question. This is not an answer. And since you accepted it, there is no reason for someone else to truly answer your question. So you should un-accept your answer, if you have any desire to get a real answer.
Zozo torc
Zozo torc 2018년 4월 3일
It was a mistake but Torsten gave the answer : I had to use ./ instead of / for some reason...

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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by