HP Forums
error in the integration of complex functions (FW10077) - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: error in the integration of complex functions (FW10077) (/thread-6187.html)



error in the integration of complex functions (FW10077) - slawek39 - 05-01-2016 02:46 PM

The integration of the function ft(x,CEILING(x)) gives erroneous results. My TI V200 gives different results.

[attachment=3498]
[attachment=3499]
[attachment=3500]
[attachment=3501]


RE: error in the integration of complex functions (FW10077) - Tim Wessman - 05-01-2016 04:38 PM

I don't have an 89/200 emulator or unit handy, but the nspire emu seems to perfectly match the prime output with my function I just made. Can you post your code/function definition, and maybe a picture of the expected output?


RE: error in the integration of complex functions (FW10077) - slawek39 - 05-01-2016 04:53 PM

(05-01-2016 04:38 PM)Tim Wessman Wrote:  Can you post what you are expecting the results to be?


The graph is the same on both calculators. However, the integration gives the other (non-zero) results on TI V200.

The first integral from last screenshot (0 to 2) on TI V200: 0.076247465759
The second integral from last screenshot (0 to 1) on TI V200: 0.841470984808

On HP Prime (FW10077) both integrals: 0 (obvious error if you look at the graph)


RE: error in the integration of complex functions (FW10077) - slawek39 - 05-01-2016 05:39 PM

Code/function definition from TI V200:

:f(x,n)
:Func
:Local i,s
:0->s
:For i,1,n
: s+cos(i*x)->s
:EndFor
:EndFunc

Grapg is the same but integral from f(x,ceiling(x)) id different.


RE: error in the integration of complex functions (FW10077) - Helge Gabert - 05-02-2016 02:15 AM

It seems like the built-in integration routines (int, but also romberg) have trouble with discontinuities. Same result (0) is obtained for int(ft(x,1/x),0,1) or int(ft(x, 10*sin(x)),0,1). Or any other bounds between 0 and 1, for that matter.

It is also interesting that a plot of ceiling(X) and ft(X, ceiling(X)): plot menu: signed area recognizes ft, but if the user attempts to calculate the area between both functions, the result is only for the area between ceiling(X) and the X-axis.

Of course, one can always write his/her own integration routine . . . Smile


RE: error in the integration of complex functions (FW10077) - parisse - 05-02-2016 05:47 AM

Please enter your function in plain text (like it was done for the TI), not as a screenshot, so that it's easy to copy/paste it to check. I don't have time to enter all reported questions here by hand.


RE: error in the integration of complex functions (FW10077) - parisse - 05-02-2016 02:20 PM

If f is entered as an algebraic function, you get the exact answer
Code:
f(x,n):=sum(cos(k*x),k,1,n);
int(f(x,ceil(x)),x,0,2);
returns 4*tan(1)/(tan(1)^4+2*tan(1)^2+1)+2*tan(1/2)/(tan(1/2)^2+1)-4*tan(1/2)/(tan(1/2)^4+2*tan(1/2)^2+1)
If f is entered as a program
Code:
f(x,n):= begin local j,y; y:=0; for j from 1 to n do y:=y+cos(j*x); end; end;
then f(x,ceiling(x)) returns an error in Xcas while returning 0.0 on the Prime. I guess it's again because an exception is not raised on the Prime. This explains why you get 0 for the integral.


RE: error in the integration of complex functions (FW10077) - parisse - 05-02-2016 02:39 PM

The best workaround I've found for a function like that (where evaluation with x symbolic should fail) is
Code:
romberg('f(x,ceiling(x))',x,0.0,2.0);
gaussquad would give a better approximation than romberg, but it is unfortunately not activated on the Prime.


RE: error in the integration of complex functions (FW10077) - Helge Gabert - 05-02-2016 03:18 PM

Interesting . . . So why is it that quoting f, i.e., 'f(x,ceiling(x))', works with romberg?

I always thought that int() is based on the gaussian quadrature. Any chance gaussquad could be made available for the Prime? (that is probably a question for Tim).


RE: error in the integration of complex functions (FW10077) - slawek39 - 05-02-2016 04:49 PM

(05-02-2016 05:47 AM)parisse Wrote:  Please enter your function in plain text (like it was done for the TI), not as a screenshot, so that it's easy to copy/paste it to check. I don't have time to enter all reported questions here by hand.

The function in text format:

#cas
ft(x,n):=
BEGIN
LOCAL i,s:=0;
FOR i FROM 1 TO n DO
s+COS(i*x)▶s
END
END;
#end


RE: error in the integration of complex functions (FW10077) - parisse - 05-03-2016 05:14 AM

(05-02-2016 03:18 PM)Helge Gabert Wrote:  Interesting . . . So why is it that quoting f, i.e., 'f(x,ceiling(x))', works with romberg?
Because the problem is in evaluating f(x,ceiling(x)), not in numerical integration. Quoting prevents evaluation. The program evaluation raises an exception because the test in the loop can not be evaluated with x symbolic, but the Prime does not have exceptions. This means that if I want to fix this bug, I must mimic the exception mechanism by handcoding specific error code (and this is an endless game...)


RE: error in the integration of complex functions (FW10077) - parisse - 05-03-2016 08:16 AM

As I feared it requires a lot of small changes ... but now I can get the exact answer which was a surprise to me.


RE: error in the integration of complex functions (FW10077) - Helge Gabert - 05-03-2016 02:28 PM

Great news, and thanks for the explanation!