1
$\begingroup$

I am implementing a double integration of a function; numerically in Matlab. What I basically do is:

number_of_elements = 10;
x = linspace(0,3,number_of_elements);
y = linspace(0,3,number_of_elements);
dx = (3-0)/number_of_elements;
dy = (3-0)/number_of_elements;

for i = 1:number_of_elements
    for j = 1:number_of_elements
        f_j(j) = (x(i)^2 + y(j)^2) * dx * dy;
    end
    f_i(i) =  trapz(f_j) % trapezoidal rule
end

f = trapz(f_i) % trapezoidal rule

To do its tests, I am comparing my code's result to an analytically calculated result of simple functions.

For e.g: I am integrating the function: function

Calculating analytically using pen and paper, its result is 54. If my implementation is right, than I would expect my calculated value to be near 54 and would get nearer and nearer to 54 as I make my step sizes of integration smaller , i.e. the number_of_elements larger.

Good news that this is exactly what happens: results. On x axis is the number of elements(which increases with smaller step size). On y axis in the function value in blue, and real value in red.

Its all good. But then I tried an another function: function2. Here dr and dgama is similar to dx and dy as in 'function1'

The results are: results (function2). The limits for the first integral are R_hub = 0.3 to R_tip =1.25. The calculated value is in yellow and real value in black.

Its right that the results for both cases improves as the steps get smaller.

But why does for larger step sizes, the numerically calculated values starts from a higher value than the true value in the first case and then from a lower value than the true value in the second case.

Thanks in advance.

$\endgroup$
5
  • $\begingroup$ It looks like your function1 and function2 are identical. Could you write these into the question using MathJax? $\endgroup$ Commented Aug 6, 2023 at 14:47
  • 1
    $\begingroup$ Sorry, my bad. I added the 2nd function. Thanks. $\endgroup$ Commented Aug 6, 2023 at 15:42
  • $\begingroup$ @ChrisLewis In the 2nd function, the two variables for integration are r and gamma. Its a bit complicated than the simple function1. The variables in the function2 contains r and gamma inside it for calculating them. $\endgroup$ Commented Aug 6, 2023 at 15:44
  • $\begingroup$ This is to do with the shape of the function you're integrating (convexity vs non-convexity). A simpler example is to look at the trapezium rule for the functions $f(x)=x^2$ and $g(x)=1-x^2$ between, say, $x=0$ and $x=1$. Try drawing the shape of the trapezia vs the shape of the curve - can you see why in one case the trapezium rule overestimates the integral and in the other it underestimates? $\endgroup$ Commented Aug 6, 2023 at 16:34
  • 1
    $\begingroup$ @ChrisLewis Yes, right...now I get it. Thank you very much. $\endgroup$ Commented Aug 6, 2023 at 17:00

1 Answer 1

1
$\begingroup$

If your function $f$ is convex, such as $f(x,y) =x^2+y^2$, the trapezoid rule will overestimate the true integral, since the height of points on the interior of each trapezoid will be greater than the value of $f$ at the corresponding point. Similarly if $-f$ is convex the numerical integral will underestimate the true value.

For more complicated functions that contain a mix of convex, concave, and saddle regions, such as your second function, it's hard to say anything meaningful generically about the sign of the error. For a concrete $f$ you can compute it in terms of $f$'s higher-order derivatives by Taylor-expanding $f$, as sketched in the error analysis section on Wikipedia.

$\endgroup$
1
  • $\begingroup$ Thanks, that make sense. This would essentially mean that the second function is a non-convex function, or the part of the second function where we are looking at, is atleast non-convex, right? $\endgroup$ Commented Aug 6, 2023 at 17:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .