Wrong numerical integration value but no error

7 Ansichten (letzte 30 Tage)
Zozo torc
Zozo torc am 3 Apr. 2018
Bearbeitet: John D'Errico am 3 Apr. 2018
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 Kommentar
Torsten
Torsten am 3 Apr. 2018
Bearbeitet: Torsten am 3 Apr. 2018
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.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

John D'Errico
John D'Errico am 3 Apr. 2018
Bearbeitet: John D'Errico am 3 Apr. 2018
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 am 3 Apr. 2018
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 Kommentare
John D'Errico
John D'Errico am 3 Apr. 2018
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 am 3 Apr. 2018
It was a mistake but Torsten gave the answer : I had to use ./ instead of / for some reason...

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by