Hey there! Sign in to join this conversationNew here? Join for free
x Turn on thread page Beta

MATLAB for computing the approximation of an integral. watch

    • Thread Starter
    Offline

    21
    ReputationRep:
    I have the following code, which works for a given function:
    function s = int(f,a,b,n) % function, input: f = function, a,b
    % end points, n = number of rectangles.
    % output is s = approx to integral.
    format long

    h = (b-a)/n; % the width of each rectangle

    s = 0; % running total of area of rectangles
    for j=1:n % loop over all rectangles
    t = t+h*f(a+(j-0.5)*h); % add the areas of each rectangle
    end

    disp t % display result

    end

    However I was experimenting about how to to use arrays in this, so far I've ended up with:

    function s = intarray(f,a,b,n) % function, input: f = function, a,b are endpoints
    % n = number of rectangles.
    % output is s = approx to integral.

    t = zeros(1,n);

    a = t(1);

    b = t(n);

    h = (b-a)/n;
    for j=2:n-1
    t(j) = a + j*h;
    s = h/2*(f(a) + f(b)) + h*(t(j)+t(j-1));
    end
    disp s
    end


    The main problem seems to be the for loop. I can't have it go from 1:n-1, it will result in an error.

    Any help?
    Offline

    18
    ReputationRep:
    (Original post by TheBBQ)
    I have the following code, which works for a given function:

    function t = int(f,a,b,n)
    format long
    h = (b-a)/n;
    t = 0.5*h*(f(a)+f(b));
    for j=1:n-1
    t = t + h*f(a+j*h);
    end
    disp t
    end

    However I was experimenting about how to to use arrays in this, so far I've ended up with:

    function s = intarray(f,a,b,n)
    t = zeros(1,n);
    a = t(1);
    b = t(n);
    h = (b-a)/n;
    for j=2:n-1
    t(j) = a + j*h;
    s = h/2*(f(a) + f(b)) + h*(t(j)+t(j-1));
    end
    disp s
    end

    The main problem seems to be the for loop. I can't have it go from 1:n-1, it will result in an error.

    Any help?
    Yeah, it would help if you defined what all your variables were and what the hell is happening here.
    • Thread Starter
    Offline

    21
    ReputationRep:
    (Original post by pleasedtobeatyou)
    Yeah, it would help if you defined what all your variables were and what the hell is happening here.
    Oops, sorry, this should explain everything:

    s or t are the integrals of a function f(t) with end points a to b.
    n = number of "rectangles"

    I'm using the following method to approximate the integral numerically.





 
 
 
Reply
Submit reply
Turn on thread page Beta
Updated: January 10, 2015
Poll
Do you agree with the proposed ban on plastic straws and cotton buds?
Useful resources

Make your revision easier

Maths

Maths Forum posting guidelines

Not sure where to post? Read the updated guidelines here

Equations

How to use LaTex

Writing equations the easy way

Student revising

Study habits of A* students

Top tips from students who have already aced their exams

Study Planner

Create your own Study Planner

Never miss a deadline again

Polling station sign

Thinking about a maths degree?

Chat with other maths applicants

Can you help? Study help unanswered threads

Groups associated with this forum:

View associated groups

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.