Matlab - multiple integral Riemann sums

Click For Summary
SUMMARY

The forum discussion focuses on creating a MATLAB function, ftc3, to compute the Riemann sum for a two-variable function f(x, y) over a specified rectangle (a, b) × (c, d). The function aims to achieve a relative error of less than 0.001 by increasing the number of Riemann cubes iteratively. Users reported issues with convergence, as the function reached the maximum iteration limit of 100 without achieving the desired accuracy. A suggested improvement is to double the number of subdivisions (n) in each iteration instead of incrementing it by one.

PREREQUISITES
  • MATLAB programming skills
  • Understanding of Riemann sums
  • Knowledge of numerical integration techniques
  • Familiarity with error analysis in numerical methods
NEXT STEPS
  • Implement a strategy to double the number of subdivisions in the ftc3 function.
  • Explore MATLAB's built-in functions for numerical integration, such as dblquad.
  • Study convergence criteria in numerical methods to enhance accuracy.
  • Learn about error handling in MATLAB to manage divergent integrals effectively.
USEFUL FOR

Students and professionals in mathematics, engineering, and computer science who are working on numerical integration and seeking to improve their MATLAB coding skills for Riemann sums.

mathmannn
Messages
15
Reaction score
0
1. Homework Statement

Write an m.file that will integrate a function f(x, y) on any given rectangle (a,b)\times(c,d) and returns the value of the integral from a to b and c to d of the function f(x,y). 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 \displaystyle \frac{S_{new} - S_{old}}{S_{new}} 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 \int_0^1 \int_0^1 xy \quad dx dy which I know should be \frac{1}{4}

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
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 n by one each time. Probably a better strategy is to double it with each iteration.
 

Similar threads

Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 16 ·
Replies
16
Views
4K
Replies
3
Views
2K