Matlab - multiple integral Riemann sums

In summary, a multiple integral Riemann sum in Matlab is a numerical approximation method used to estimate the value of a double or triple integral by dividing the region of integration into smaller subregions and calculating the sum of function values at specific points within each subregion. To calculate it, the function, limits of integration, and number of subregions must be defined, and built-in functions such as "meshgrid" and "sum" are used to create a grid of points and calculate the sum of function values. The advantages of using this method include its simplicity, accuracy, and the convenience of Matlab's tools. However, there are limitations, such as its computational intensity and the dependence on subregion and function choice. It can be used for
  • #1
mathmannn
15
0
1. Homework Statement

Write an m.file that will integrate a function [itex]f(x, y)[/itex] on any given rectangle [itex] (a,b)\times(c,d)[/itex] and returns the value of the integral from [itex]a [/itex] to [itex] b [/itex] and [itex]c [/itex] to [itex]d [/itex] of the function [itex]f(x,y) [/itex]. Include error-catching code in the case that the integral diverges. The program should use the notion of a limit of sums, so that you increase the number of Riemann cubes until the approximate value of the integral shows a relative error [itex]\displaystyle \frac{S_{new} - S_{old}}{S_{new}}[/itex] of less than 0.001.


Homework Equations





The Attempt at a Solution



Ok so here is what I have right now
Code:
function [re,risum]=ftc3(f,a,b,c,d,maxit)
% ftc3: Finds the riemann Sum for the function f
% input:
% f = function, a = x lower bound, b = x upper bound
% c = y lower bound, d = y upper bound
% output:
% re = relative error, risum = value of the definite integral

% Note:
%       The 'maxit' input is just something I'm using so 
%       I don't get stuck in a loop.

if nargin<6|isempty(maxit),maxit=100;end
err=1;
s=0;
n=1;
its=0;
fprintf('\nn\t error\t   dx\t   dy\t   dA\t  sum\n\n');
while(1)
    s0=s;
    dx=(b-a)/n;
    dy=(d-c)/n;
    xi=a+dx:dx:b;
    yi=c+dy:dy:d;
    dA=dx*dy;
    s=sum(f(xi,yi))*dA;
    if s>realmax,error('This integral diverges');end
    rerr = abs((s-s0)/s);
% Note:    
%       I'm just using this table so I can see what
%       is going on with my code. It's not needed for the problem
    z=[n;rerr;dx;dy;dA;s];
    fprintf('%d\t %2.4f\t %2.4f\t %2.4f\t %2.4f\t %2.4f\n',z);
    its=its+1;
    n=n+1;
    if rerr<.001|its>=maxit,break,end
    
end
risum=s;
re = rerr;
disp(['Number of iterations ',num2str(its)])
but I can not figure out what is wrong with my code. When I test it using [itex] \int_0^1 \int_0^1 xy \quad dx dy[/itex] which I know should be [itex] \frac{1}{4} [/itex]

This is what it returns (without the table)
Code:
EDU>> f=@(x,y) x.*y;a=0;b=1;c=0;d=1;
EDU>> [relative_error,riemann_sum]=ftc3(f,a,b,c,d)
Number of iterations 100

relative_error =

    0.0103


riemann_sum =

    0.0034


Any kind of help would be awesome!
 
Physics news on Phys.org
  • #3
You have three possible exit conditions for your loop. One is a divergence catch, which isn't an issue here. The second is achieved convergence, and the third is if the number of iterations exceeds the number "itmax." You have set this maximum at 100, and clearly it's taking 100 iterations, which means it hasn't converged because it's reached that limit and not gotten within the desired error yet. I am guessing the reason you don't reach convergence within 100 steps is that you are only increasing [itex]n[/itex] by one each time. Probably a better strategy is to double it with each iteration.
 

FAQ: Matlab - multiple integral Riemann sums

What is a multiple integral Riemann sum in Matlab?

A multiple integral Riemann sum in Matlab is a numerical approximation method used to estimate the value of a double or triple integral. It involves dividing the region of integration into smaller subregions and calculating the sum of the function values at specific points within each subregion. As the number of subregions increases, the accuracy of the approximation improves.

How do I calculate a multiple integral Riemann sum in Matlab?

To calculate a multiple integral Riemann sum in Matlab, you will need to define the function to be integrated, the limits of integration, and the number of subregions. Then, use the built-in functions "meshgrid" and "sum" to create a grid of points within each subregion and calculate the sum of the function values at these points. Finally, multiply this sum by the area or volume of each subregion to get the overall approximation.

What are the advantages of using a multiple integral Riemann sum in Matlab?

There are several advantages to using a multiple integral Riemann sum in Matlab. First, it is a relatively simple and straightforward method to approximate integrals, making it accessible to users with varying levels of expertise. Second, it allows for a more accurate estimation of integrals compared to using a single interval or midpoint Riemann sum. Lastly, Matlab has a wide range of built-in functions and tools that make it efficient and convenient to perform calculations.

Are there any limitations to using a multiple integral Riemann sum in Matlab?

While the multiple integral Riemann sum is a useful numerical approximation method, it does have some limitations. One limitation is that it can be computationally intensive, especially for integrals with a large number of subregions. Additionally, the accuracy of the approximation is highly dependent on the choice of subregions and the function being integrated. Therefore, it may not always provide an exact solution.

Can I use a multiple integral Riemann sum in Matlab for any type of function?

Yes, you can use a multiple integral Riemann sum in Matlab for any type of function as long as it is defined within the limits of integration and can be evaluated at the chosen points within each subregion. However, for functions that are particularly complicated or have a high degree of variation, the accuracy of the approximation may be compromised. In such cases, it may be necessary to use more advanced numerical methods.

Similar threads

Replies
3
Views
1K
Replies
14
Views
1K
Replies
2
Views
2K
Replies
13
Views
3K
Replies
6
Views
4K
Replies
16
Views
3K
Back
Top