Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Matlab - multiple integral Riemann sums

  1. Apr 30, 2012 #1
    1. The problem statement, all variables and given/known data

    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.

    2. Relevant equations

    3. The attempt at a solution

    Ok so here is what I have right now
    Code (Text):

    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
    fprintf('\nn\t error\t   dx\t   dy\t   dA\t  sum\n\n');
        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
        fprintf('%d\t %2.4f\t %2.4f\t %2.4f\t %2.4f\t %2.4f\n',z);
        if rerr<.001|its>=maxit,break,end
    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 (Text):

    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 =


    riemann_sum =


    Any kind of help would be awesome!
  2. jcsd
  3. Apr 30, 2012 #2
  4. May 3, 2012 #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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook