# Integral fails, NIntegral correct

Angsüsser shared this problem 5 years ago
Solved

I use Geogebra Classic 5.0.445(Desktop):

I have point-list with 4 points and construct 3 cubic splines:

pointL = {(0.1, 0.2), (0.4, 1.2), (2.2, 1), (3, 2)}

the splines:

f_s(x) = If(x < 0.4, 0(0.4 - x)³ - 3.65(x - 0.1)³ + 0.67 (0.4 - x) + 4.33 (x - 0.1), 0) +

If(0.4 ≤ x < 2.2, -0.61(2.2 - x)³ + 0.36(x - 0.4)³ + 2.64 (2.2 - x) - 0.6 (x - 0.4), 0) +

If(2.2 ≤ x, 0.8(3 - x)³ + 0(x - 2.2)³ + 0.74 (3 - x) + 2.5 (x - 2.2), 0)

now the integrals differ significantly:

NIntegral(f_s, 0.1, 3) -> 3.98 (which is correct)

Integral(f_s, 0.1, 3) -> 14.28 !!!!!!!!

Is something wrong in the upper commands or is it a bug? 1

In the meantime I have found a workaround that helps:

g(x)=If(0.1 ≤ x ≤ 3, f_s(x))

Strange enough this yield the correct integral-number with Integral-command! 1

If you look at Integral(f_s) you'll see the problem (it's not continuous) 1

I can't see this (in my version of the program); I added a screenshot - as you can see even the derivative of f_s is

continuous, even the derivative of the derivative - splines must have this property.

Seems - we both see different things on the screen (therefore the screenshot!) - but thank you for thinking over it! 1

Sorry - I missed the point! You spoke of Integral(f_s) not f_s! But why is the integral-function F_s discontinuous?

As I see it, it should be

F_s(x):= int_a^x f_s(t)\, dt with "a" some real constant. 1

The integration is done piecewise. If you define this (for the appropriate value of a!)

```F(x) = If(x ≥ 2 / 5 ∧ 11 / 5 > x, 1 / 12500 (12125 / 4 x⁴ - 18575x³ + 72375 / 2 x² - 5879x), 0) + If(x ≥ 11 / 5, 1 / 25 (-5x⁴ + 60x³ - 248x² + 458x) - a, 0) + If(2 / 5 > x, 1 / 20000 (-18250x⁴ + 7300x³ + 35505x² - 3227x), 0)
```

and then

```f_s(x) = F'(x)
```
then you can get accurate integrals with:

`F(b) - F(a)` 1

We've made an improvement for the next release (v448) so that Integral() and NIntegral() give the same answer for your example.

The approach above (defining F(x) first) is more robust and accurate though.

Also it's better to define such functions with one If() if possible, eg

`F(x) = If(0.4 ≤ x < 2.2, 1 / 12500 (12125 / 4 x⁴ - 18575x³ + 72375 / 2 x² - 5879x), x ≥ 11 / 5, 1 / 25 (-5x⁴ + 60x³ - 248x² + 458x) - a, 2 / 5 > x, 1 / 20000 (-18250x⁴ + 7300x³ + 35505x² - 3227x), 0)` 1

Thank you Michael for your elaborate explanation - I must commit, I assumed the Integral-functions are also continuous - an error! Because my splines are built with Javascript as a "string", it's no problem to build a "one If" statement instead of an "If-sum";

After your proposal I think that it may be better to integrate my splines in javascript (polynomials should not be an obstacle), handle it over to geogebra, and take the derivative there - if I need an integration, I could use the "original" F_s- function with F_s(b)-F_s(a) (as you suggested)

Thank you once again - I've learned a lot!

Hans 1

v448 online now :)