Results 1 to 2 of 2

Math Help - Some MatLab help

  1. #1
    Newbie
    Joined
    Sep 2009
    Posts
    1

    Some MatLab help

    I'm not very good with matlab and have been trying to get some practice in over the summer. Ive just about managed to write an M-file for the trapezium rule and was just wondering how I could modify it and set a tolerance level.

    This is what ive got so far:

    Code:
    function trapezium(a,b,n)
    h=(b-a)/n;
    sum = 0;
    for i=1:n-1;
        x(i)=a + i*h;
        sum = sum + f(x(i));
    end
    I = h*(f(a)+f(b)+2*sum)/2
    
    function y=f(x)
    y=sin(x)

    So i can use it for n strips but im not sure how to make it run for increasing strips :S

    Can anyone help me out at all? Thanks
    Last edited by CaptainBlack; September 1st 2009 at 08:39 PM.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by ussvoyager View Post
    I'm not very good with matlab and have been trying to get some practice in over the summer. Ive just about managed to write an M-file for the trapezium rule and was just wondering how I could modify it and set a tolerance level.

    This is what ive got so far:

    Code:
    function trapezium(a,b,n)
    h=(b-a)/n;
    sum = 0;
    for i=1:n-1
        x(i)=a + i*h;
        sum = sum + f(x(i));
    end
    I = h*(f(a)+f(b)+2*sum)/2
    
    function y=f(x)
    y=sin(x);

    So i can use it for n strips but im not sure how to make it run for increasing strips :S

    Can anyone help me out at all? Thanks
    1. Your function trapezium does not return anything (it just prints it as there is no semi colon after the last line.

    2. The error formula for the trapezium rule depends on the function (or rather its second derivative), so if you want the error estimate to be independedent of the function you will either need to obtain the second derivative some how or call the function twice with n and 2*n steps.

    3. trapezium(0,1,10) then trapezium(0,1,20)

    4. It is always a good idea to just run your code and see what happens (you won't break anything)

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Matlab help
    Posted in the Math Software Forum
    Replies: 1
    Last Post: February 11th 2010, 02:59 AM
  2. Matlab FFT Help
    Posted in the Math Software Forum
    Replies: 0
    Last Post: June 3rd 2009, 12:48 PM
  3. help about ODE in matlab
    Posted in the Math Software Forum
    Replies: 1
    Last Post: May 31st 2009, 03:10 AM
  4. Matlab Please help me
    Posted in the Math Software Forum
    Replies: 8
    Last Post: November 10th 2008, 04:40 PM
  5. Matlab
    Posted in the Calculus Forum
    Replies: 3
    Last Post: August 20th 2008, 10:39 PM

Search Tags


/mathhelpforum @mathhelpforum