Wrong numerical integration value but no error
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
Antworten (2)
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.
0 Kommentare
Zozo torc
am 3 Apr. 2018
3 Kommentare
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.
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!