Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Romberg's method

Subject: Romberg's method

From: Tran Quang Dat

Date: 2 Jan, 2012 08:08:08

Message: 1 of 5

Hi,
I would like to integrate a function f(x) in the interval [a,b], and I know some values of the function f(x) between the given interval, for example fi = {c, d, e, f, g, ...}.
So, how can I solve this integration by using Romberg's method?
Thank You very much,

Tran Quang Dat

Subject: Romberg's method

From: Torsten

Date: 2 Jan, 2012 08:22:17

Message: 2 of 5

On 2 Jan., 09:08, "Tran Quang Dat" <tranda...@gmail.com> wrote:
> Hi,
> I would like to integrate a function f(x) in the interval [a,b], and I know some values of the function f(x) between the given interval, for example fi = {c, d, e, f, g, ...}.
> So, how can I solve this integration by using Romberg's method?
> Thank You very much,
>
> Tran Quang Dat

To use Romberg's method of integration, you must be able to evaluate f
for _all_ points x in [a,b].
If you only know f on a discrete set of points a<=x1<x2<x3<...<xn<=b,
use MATLAB's trapz or something similar.

Best wishes
Torsten.

Subject: Romberg's method

From: Tran Quang Dat

Date: 2 Jan, 2012 08:36:08

Message: 3 of 5

Torsten <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message <189f8049-acfd-4c73-962c-6416915d3db8@e2g2000vbb.googlegroups.com>...
> On 2 Jan., 09:08, "Tran Quang Dat" <tranda...@gmail.com> wrote:
> > Hi,
> > I would like to integrate a function f(x) in the interval [a,b], and I know some values of the function f(x) between the given interval, for example fi = {c, d, e, f, g, ...}.
> > So, how can I solve this integration by using Romberg's method?
> > Thank You very much,
> >
> > Tran Quang Dat
>
> To use Romberg's method of integration, you must be able to evaluate f
> for _all_ points x in [a,b].
> If you only know f on a discrete set of points a<=x1<x2<x3<...<xn<=b,
> use MATLAB's trapz or something similar.
>
> Best wishes
> Torsten.

Thank you very much for fast reply,
Actually, I have a vector that included all points x in the interval [a,b].
Can you tell me the way to solve it by using Romberg's integration method.
Once again, thank you very much.

Tran Quang Dat

Subject: Romberg's method

From: Nasser M. Abbasi

Date: 2 Jan, 2012 09:06:31

Message: 4 of 5

On 1/2/2012 2:36 AM, Tran Quang Dat wrote:

>
> Thank you very much for fast reply,
> Actually, I have a vector that included all points x in the interval [a,b].
> Can you tell me the way to solve it by using Romberg's integration method.
> Once again, thank you very much.
>
> Tran Quang Dat

The way is to write the Romberg's integration method?

had this for a HW sometime, looking through old files,
here is what I have, use at your own risk.

------------------------
function R = nma_test_romberg
%function nma_testRomberg
%
% returns the romberg integration table
%
% Example call
% R = nma_test_romberg()
% nma_test_romberg
% ans =
% 1.8154e+003 6.6540e+002 5.1408e+002 -2.7149e+002 1.9493e+001
% 9.5291e+002 5.2354e+002 -2.5921e+002 1.8357e+001 0
% 6.3088e+002 -2.1029e+002 1.4020e+001 0 0
% 0 0 0 0 0
% 0 0 0 0 0
% Copyright: Nasser M. Abbasi


% change this as needed
f = 'x*exp(2*x)';
from = 0;
to = 3;

nStrips = 5;
rowNumber = 0;
nSoFar = 0;
c = zeros(nStrips,1);
while(1)
     rowNumber = rowNumber+1;
     nSoFar = 2^(rowNumber-1);
     if nSoFar>nStrips
         break;
     end
     c(rowNumber) = nma_trapezoidal(f,from,to,nSoFar);
end

R=nma_romberg(c);
------------------------------------------



---------------------------
function R=nma_romberg(firstColumn)
%function nma_romberg(firstColumn)
%
% Function to generate the Romberg integration table given the
% first column (which should be the result of the trapozidal
% integration, for number of strips n=1,2,4,8,16,....
%
% INPUT
% firstColumn: a Vector whose elements represent the integral
% for different number of strips. The first element if trapozidal
% integral for n=1, second element in the vector is for
% trapozidal integral for n=2, the third element is for n=4,
% and so forth. Notice that n goes as 2^i where i is the
% position number in the vector. Use nma_trapezoidal to generate
% the integral for specific n values and use the output to feed
% it to this function.
%
% OUTPUT:
% a Matrix R the reppresents the romberg table. The first column of the
% matrix is the same as the input argument. The remaining columns are
% the result of the extrapolation used.
%
% see nma_testRomberg.m for an example of how to use these functions
%
% Copyright: Nasser Abbasi
%

% change history
% name date change
% nma 5/2/03 started. To solve HW#4, MAE 185. UCI
% nma 10/18/2010 cleaned up.

len=length(firstColumn);

R=zeros(len);
R(:,1)=firstColumn';
nEntries=len;
k=1;
while(nEntries ~= 0)
     k=k+1;
     F= 4^(k-1);
     for j=1:nEntries-1
         R(j,k)= ( F*R(j+1,k-1) - R(j,k-1) )/ (F-1);
     end
     nEntries=nEntries-1;
end
-------------------------------------------


--Nasser

Subject: Romberg's method

From: Nasser M. Abbasi

Date: 2 Jan, 2012 09:12:13

Message: 5 of 5


Forgot to give you also this function used by the test function below

----------------------------------------------------
function I=nma_trapezoidal(func,from,to,nStrips)
%function r=nma_trapezoidal(f,from,to,nStrips)
%
% integrates a function using trapezoidal rule using
% specific number of strips.
%
% INPUT:
% f : a string that repesents the function itself
% for example 'x*sin(x)'. The independent variable
% used in the string must be 'x' and no other letter.
%
% from: lower limit
% to : upper limit
% nStrips: number of strips to use
%
% OUTPUT
% I : The integral.
%
%
% Example:
% nma_trapezoidal('cos(x)',-1,0,10)
% ans =
% 0.8408
%
% nma_trapezoidal('cos(x)',-1,0,100)
% ans =
% 0.8415
%
% quad('cos(x)',-1,0)
% ans =
% 0.8415
%
% copyright: Nasser M. Abbasi
% May 3, 2003

I=0;

if(from > to)
     error('lower limit can not be smaller than upper limit');
end

if(nStrips<=0)
     error('number of strips must be > 0');
end

if( abs(from-to)<eps )
     I = 0;
else

     nPoints = nStrips+1;
     h = abs((from-to))/(nStrips);
     X = from:h:to;

     for i=1:nPoints
         x=X(i);
         f = eval(func);
         if(i==1 || i==nPoints )
             I=I+f ;
         else
             I=I+2*f;
         end
     end

     I=I*h/2;
end
----------------------------------

On 1/2/2012 3:06 AM, Nasser M. Abbasi wrote:
> On 1/2/2012 2:36 AM, Tran Quang Dat wrote:
>
>>
>> Thank you very much for fast reply,
>> Actually, I have a vector that included all points x in the interval [a,b].
>> Can you tell me the way to solve it by using Romberg's integration method.
>> Once again, thank you very much.
>>
>> Tran Quang Dat
>
> The way is to write the Romberg's integration method?
>
> had this for a HW sometime, looking through old files,
> here is what I have, use at your own risk.
>
> ------------------------
> function R = nma_test_romberg
> %function nma_testRomberg
> %
> % returns the romberg integration table
> %
> % Example call
> % R = nma_test_romberg()
> % nma_test_romberg
> % ans =
> % 1.8154e+003 6.6540e+002 5.1408e+002 -2.7149e+002 1.9493e+001
> % 9.5291e+002 5.2354e+002 -2.5921e+002 1.8357e+001 0
> % 6.3088e+002 -2.1029e+002 1.4020e+001 0 0
> % 0 0 0 0 0
> % 0 0 0 0 0
> % Copyright: Nasser M. Abbasi
>
>
> % change this as needed
> f = 'x*exp(2*x)';
> from = 0;
> to = 3;
>
> nStrips = 5;
> rowNumber = 0;
> nSoFar = 0;
> c = zeros(nStrips,1);
> while(1)
> rowNumber = rowNumber+1;
> nSoFar = 2^(rowNumber-1);
> if nSoFar>nStrips
> break;
> end
> c(rowNumber) = nma_trapezoidal(f,from,to,nSoFar);
> end
>
> R=nma_romberg(c);
> ------------------------------------------
>
>
>
> ---------------------------
> function R=nma_romberg(firstColumn)
> %function nma_romberg(firstColumn)
> %
> % Function to generate the Romberg integration table given the
> % first column (which should be the result of the trapozidal
> % integration, for number of strips n=1,2,4,8,16,....
> %
> % INPUT
> % firstColumn: a Vector whose elements represent the integral
> % for different number of strips. The first element if trapozidal
> % integral for n=1, second element in the vector is for
> % trapozidal integral for n=2, the third element is for n=4,
> % and so forth. Notice that n goes as 2^i where i is the
> % position number in the vector. Use nma_trapezoidal to generate
> % the integral for specific n values and use the output to feed
> % it to this function.
> %
> % OUTPUT:
> % a Matrix R the reppresents the romberg table. The first column of the
> % matrix is the same as the input argument. The remaining columns are
> % the result of the extrapolation used.
> %
> % see nma_testRomberg.m for an example of how to use these functions
> %
> % Copyright: Nasser Abbasi
> %
>
> % change history
> % name date change
> % nma 5/2/03 started. To solve HW#4, MAE 185. UCI
> % nma 10/18/2010 cleaned up.
>
> len=length(firstColumn);
>
> R=zeros(len);
> R(:,1)=firstColumn';
> nEntries=len;
> k=1;
> while(nEntries ~= 0)
> k=k+1;
> F= 4^(k-1);
> for j=1:nEntries-1
> R(j,k)= ( F*R(j+1,k-1) - R(j,k-1) )/ (F-1);
> end
> nEntries=nEntries-1;
> end
> -------------------------------------------
>
>
> --Nasser

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us