Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I'm new to MATLAB and I'm having a really hard time converting an algorithm that I constructed, into code in MATLAB. Can anyone please help me?

Here is my problem and algorithm: Romberg Integration

To approximate the integral I = integral from a to b of f(x) dx, select an integer n>0.

INPUT: endpoints a,b; integer n
OUTPUT: an array R. (Compute R by rows; only the last 2 rows are saved in storage.)
Step 1: Set h=b-a; R_(1,1) = h/2((f(a)+f(b)).
Step 2: OUTPUT(R_(1,1)).
Step 3: For i=2,...,n do Steps 4-8.
Step 4: Set R_(2,1) = 1/2[R_(1,1)+h*(sum from i=1 to 2^(i-2) of f(a+(k-0.5)*h)) 
        (Approximation from Trapezoidal Method)
Step 5: For j=2,...,i
        set R_(2,j)=R_(2,j-1)+(R_(2,j-1)-R_(1,j-1))/(4^(j-1)-1). (Extrapolation).
Step 6: OUTPUT (R_(2,j) for j=1,2,...,i).
Step 7: Set h = h/2.
Step 8: For j=1,2,...,i st R_(1,j) = R_(2,j).
Step 9: STOP

Thanks in advance.

share|improve this question
See cs.jhu.edu/CIRL/class/600.161/class2/shortmatlab.pdf for a fairly complete and brief introduction to Matlab. Because of Matlab's syntax, your code will end up being very similar to the way you described your algorithm. –  Kyle Oct 26 '13 at 10:52
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.