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:
trapz function for Non-uniform Intervals

Subject: trapz function for Non-uniform Intervals

From: edward kabanyas

Date: 2 May, 2012 08:43:06

Message: 1 of 5

Hi all,

I have used trapz function for uniform interval so far and now I want to use it for non-uniform interval. It seems for non uniform interval, the trapz function is not so correct. Probably it is only my mistake.

I try to calculate rainfallrate from drop size as following code:

d = [0.062, 0.187, 0.312, 0.437, 0.562, 0.687, 0.812, 0.937, 1.062, 1.187, 1.375, ...
             1.625, 1.875, 2.125, 2.375, 2.750, 3.250, 3.750, 4.250, 4.750, 5.500, 6.500, ...
             7.500, 8.500, 9.500, 11.000, 13.000, 15.000, 17.000, 19.000, 21.500, 24.500] ;
SpreD = [0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.25, 0.25, 0.25, ...
             0.25, 0.25, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3];
ClasV = [0.050, 0.150, 0.250, 0.350, 0.450, 0.550, 0.650, 0.750, 0.850, 0.950, 1.100, ...
             1.300, 1.500, 1.700, 1.900, 2.200, 2.600, 3.000, 3.400, 3.800, 4.400, 5.200, ...
             6.000, 6.800, 7.600, 8.800, 10.400, 12.000, 13.600, 15.200, 17.600, 20.800];

dsd; %drop size dsitribution

num = size(dsd);
nd1=length(d);

%using trapz:

  for n=1:num,
 
    rainrate_avg(n)=trapz(d,dsd_avg(n,1:nd1).*(d.^3).*ClasV*6*pi*1e-4);
 
end;

%using looping

for j=1:num
    for k=1:nd1
    rr(k)=ClasV(k)*dsd(j,k)*(ClasD(k).^3)*SpreD(k)*6*pi*1e-4;
    Rn=Rn+rr(k);
    end
    Rnew(j)=Rn;
    Rn=0;

end

I found the result of this two methods is different. Probably, I make mistake in using trapz function for nonuniform-interval ? Thanks for sharing..

Regards,
EDward

Subject: trapz function for Non-uniform Intervals

From: edward kabanyas

Date: 2 May, 2012 08:48:07

Message: 2 of 5

Hi all,

I have used trapz function for uniform interval so far and now I want to use it for non-uniform interval. It seems for non uniform interval, the trapz function is not so correct. Probably it is only my mistake.

I try to calculate rainfallrate from drop size as following code:

d = [0.062, 0.187, 0.312, 0.437, 0.562, 0.687, 0.812, 0.937, 1.062, 1.187, 1.375, ...
             1.625, 1.875, 2.125, 2.375, 2.750, 3.250, 3.750, 4.250, 4.750, 5.500, 6.500, ...
             7.500, 8.500, 9.500, 11.000, 13.000, 15.000, 17.000, 19.000, 21.500, 24.500] ;
SpreD = [0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.25, 0.25, 0.25, ...
             0.25, 0.25, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3];
ClasV = [0.050, 0.150, 0.250, 0.350, 0.450, 0.550, 0.650, 0.750, 0.850, 0.950, 1.100, ...
             1.300, 1.500, 1.700, 1.900, 2.200, 2.600, 3.000, 3.400, 3.800, 4.400, 5.200, ...
             6.000, 6.800, 7.600, 8.800, 10.400, 12.000, 13.600, 15.200, 17.600, 20.800];

dsd; %drop size dsitribution

num = size(dsd);
nd1=length(d);

%using trapz:

  for n=1:num,
 
    rainrate_avg(n)=trapz(d,dsd_avg(n,1:nd1).*(d.^3).*ClasV*6*pi*1e-4);
 
end;

%using looping

for j=1:num
    for k=1:nd1
    rr(k)=ClasV(k)*dsd(j,k)*(d(k).^3)*SpreD(k)*6*pi*1e-4;
    Rn=Rn+rr(k);
    end
    Rnew(j)=Rn;
    Rn=0;

end

I found the result of this two methods is different. Probably, I make mistake in using trapz function for nonuniform-interval ? Thanks for sharing..

Regards,
EDward

Subject: trapz function for Non-uniform Intervals

From: Torsten

Date: 2 May, 2012 09:04:04

Message: 3 of 5

On 2 Mai, 10:48, "edward kabanyas" <djuky_...@yahoo.com> wrote:
> Hi all,
>
> I have used trapz function for uniform interval so far and now I want to use it for non-uniform interval. It seems for non uniform interval, the trapz function is not so correct. Probably it is only my mistake.
>
> I try to calculate rainfallrate from drop size as following code:
>
> d = [0.062, 0.187, 0.312, 0.437, 0.562, 0.687, 0.812, 0.937, 1.062, 1.187, 1.375, ...
>              1.625, 1.875, 2.125, 2.375, 2.750, 3.250, 3.750, 4.250, 4.750, 5.500, 6.500, ...
>              7.500, 8.500, 9.500, 11.000, 13.000, 15.000, 17.000, 19.000, 21.500, 24.500] ;
> SpreD = [0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.25, 0.25, 0.25, ...
>              0.25, 0.25, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3];
> ClasV = [0.050, 0.150, 0.250, 0.350, 0.450, 0.550, 0.650, 0.750, 0.850, 0.950, 1.100, ...
>              1.300, 1.500, 1.700, 1.900, 2.200, 2.600, 3.000, 3.400, 3.800, 4.400, 5.200, ...
>              6.000, 6.800, 7.600, 8.800, 10.400, 12.000, 13.600, 15.200, 17.600, 20.800];
>
> dsd; %drop size dsitribution
>
> num = size(dsd);
> nd1=length(d);
>
> %using trapz:
>
>   for n=1:num,
>
>     rainrate_avg(n)=trapz(d,dsd_avg(n,1:nd1).*(d.^3).*ClasV*6*pi*1e-4);
>
> end;
>
> %using looping
>
> for j=1:num
>     for k=1:nd1
>     rr(k)=ClasV(k)*dsd(j,k)*(d(k).^3)*SpreD(k)*6*pi*1e-4;
>     Rn=Rn+rr(k);
>     end
>     Rnew(j)=Rn;
>     Rn=0;
>
> end
>
> I found the result of this two methods is different. Probably, I make mistake in using trapz function for nonuniform-interval ? Thanks for sharing..
>
> Regards,
> EDward

1. The method you implemented using looping is _not_ the trapezoidal
rule.
2. Is dsd(j,k)*SpreD(k) the same as dsd_avg(j,k) ?
3. It seems you forgot to muliply rr(k) by d(k+1)-d(k) ?

Best wishes
Torsten.

Subject: trapz function for Non-uniform Intervals

From: edward kabanyas

Date: 2 May, 2012 11:50:09

Message: 4 of 5

Dear Torsten;

Thanks for your reply.

1. The method you implemented using looping is _not_ the trapezoidal
rule.
It is summation (Sigma) and for the uniform interval the result is close/the same as trapz function, isn't it ?

2. Is dsd(j,k)*SpreD(k) the same as dsd_avg(j,k) ?
Sorry, dsd(j,k) = dsd_avg(j,k)

3. It seems you forgot to muliply rr(k) by d(k+1)-d(k) ?
SpreadD(k) = d(k+1)-d(k) ,

I don't know why this two method provide different result. If the interval is uniform, the result is the same. Thus, I thought that trapz function only applicable for uniform interval.

Edward




Torsten <Torsten.Hennig@umsicht.fraunhofer.de> wrote in message <d0438c84-392f-484e-921c-33d4dae767db@dc2g2000vbb.googlegroups.com>...
> On 2 Mai, 10:48, "edward kabanyas" <djuky_...@yahoo.com> wrote:
> > Hi all,
> >
> > I have used trapz function for uniform interval so far and now I want to use it for non-uniform interval. It seems for non uniform interval, the trapz function is not so correct. Probably it is only my mistake.
> >
> > I try to calculate rainfallrate from drop size as following code:
> >
> > d = [0.062, 0.187, 0.312, 0.437, 0.562, 0.687, 0.812, 0.937, 1.062, 1.187, 1.375, ...
> >              1.625, 1.875, 2.125, 2.375, 2.750, 3.250, 3.750, 4.250, 4.750, 5.500, 6.500, ...
> >              7.500, 8.500, 9.500, 11.000, 13.000, 15.000, 17.000, 19.000, 21.500, 24.500] ;
> > SpreD = [0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.25, 0.25, 0.25, ...
> >              0.25, 0.25, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3];
> > ClasV = [0.050, 0.150, 0.250, 0.350, 0.450, 0.550, 0.650, 0.750, 0.850, 0.950, 1.100, ...
> >              1.300, 1.500, 1.700, 1.900, 2.200, 2.600, 3.000, 3.400, 3.800, 4.400, 5.200, ...
> >              6.000, 6.800, 7.600, 8.800, 10.400, 12.000, 13.600, 15.200, 17.600, 20.800];
> >
> > dsd; %drop size dsitribution
> >
> > num = size(dsd);
> > nd1=length(d);
> >
> > %using trapz:
> >
> >   for n=1:num,
> >
> >     rainrate_avg(n)=trapz(d,dsd_avg(n,1:nd1).*(d.^3).*ClasV*6*pi*1e-4);
> >
> > end;
> >
> > %using looping
> >
> > for j=1:num
> >     for k=1:nd1
> >     rr(k)=ClasV(k)*dsd(j,k)*(d(k).^3)*SpreD(k)*6*pi*1e-4;
> >     Rn=Rn+rr(k);
> >     end
> >     Rnew(j)=Rn;
> >     Rn=0;
> >
> > end
> >
> > I found the result of this two methods is different. Probably, I make mistake in using trapz function for nonuniform-interval ? Thanks for sharing..
> >
> > Regards,
> > EDward
>
> 1. The method you implemented using looping is _not_ the trapezoidal
> rule.
> 2. Is dsd(j,k)*SpreD(k) the same as dsd_avg(j,k) ?
> 3. It seems you forgot to muliply rr(k) by d(k+1)-d(k) ?
>
> Best wishes
> Torsten.

Subject: trapz function for Non-uniform Intervals

From: Torsten

Date: 2 May, 2012 12:17:24

Message: 5 of 5

On 2 Mai, 13:50, "edward kabanyas" <djuky_...@yahoo.com> wrote:
> Dear Torsten;
>
> Thanks for your reply.
>
> 1. The method you implemented using looping is _not_ the trapezoidal
> rule.
> It is summation (Sigma) and for the uniform interval the result is close/the same as trapz function, isn't it ?
>
> 2. Is dsd(j,k)*SpreD(k) the same as dsd_avg(j,k) ?
> Sorry, dsd(j,k) = dsd_avg(j,k)
>
> 3. It seems you forgot to muliply rr(k) by d(k+1)-d(k) ?
> SpreadD(k) = d(k+1)-d(k) ,
>
> I don't know why this two method provide different result. If the interval is uniform, the result is the same. Thus, I thought that trapz function only applicable for uniform interval.
>
> Edward
>
>
>
> Torsten <Torsten.Hen...@umsicht.fraunhofer.de> wrote in message <d0438c84-392f-484e-921c-33d4dae76...@dc2g2000vbb.googlegroups.com>...
> > On 2 Mai, 10:48, "edward kabanyas" <djuky_...@yahoo.com> wrote:
> > > Hi all,
>
> > > I have used trapz function for uniform interval so far and now I want to use it for non-uniform interval. It seems for non uniform interval, the trapz function is not so correct. Probably it is only my mistake.
>
> > > I try to calculate rainfallrate from drop size as following code:
>
> > > d = [0.062, 0.187, 0.312, 0.437, 0.562, 0.687, 0.812, 0.937, 1.062, 1.187, 1.375, ...
> > >              1.625, 1.875, 2.125, 2.375, 2.750, 3.250, 3.750, 4.250, 4.750, 5.500, 6.500, ...
> > >              7.500, 8.500, 9.500, 11.000, 13.000, 15.000, 17.000, 19.000, 21.500, 24.500] ;
> > > SpreD = [0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.25, 0.25, 0.25, ...
> > >              0.25, 0.25, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3];
> > > ClasV = [0.050, 0.150, 0.250, 0.350, 0.450, 0.550, 0.650, 0.750, 0.850, 0.950, 1.100, ...
> > >              1.300, 1.500, 1.700, 1.900, 2.200, 2.600, 3.000, 3.400, 3.800, 4.400, 5.200, ...
> > >              6.000, 6.800, 7.600, 8.800, 10.400, 12.000, 13.600, 15.200, 17.600, 20.800];
>
> > > dsd; %drop size dsitribution
>
> > > num = size(dsd);
> > > nd1=length(d);
>
> > > %using trapz:
>
> > >   for n=1:num,
>
> > >     rainrate_avg(n)=trapz(d,dsd_avg(n,1:nd1).*(d.^3).*ClasV*6*pi*1e-4);
>
> > > end;
>
> > > %using looping
>
> > > for j=1:num
> > >     for k=1:nd1
> > >     rr(k)=ClasV(k)*dsd(j,k)*(d(k).^3)*SpreD(k)*6*pi*1e-4;
> > >     Rn=Rn+rr(k);
> > >     end
> > >     Rnew(j)=Rn;
> > >     Rn=0;
>
> > > end
>
> > > I found the result of this two methods is different. Probably, I make mistake in using trapz function for nonuniform-interval ? Thanks for sharing..
>
> > > Regards,
> > > EDward
>
> > 1. The method you implemented using looping is _not_ the trapezoidal
> > rule.
> > 2. Is dsd(j,k)*SpreD(k) the same as dsd_avg(j,k) ?
> > 3. It seems you forgot to muliply rr(k) by d(k+1)-d(k) ?
>
> > Best wishes
> > Torsten.- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

Try looping as
for j=1:num
    Rnew(j) = 0.0;
    for k=1:nd1-1
      Rnew(j)=Rnew(j)+0.5*(ClasV(k)*dsd(j,k)*d(k)^3+ClasV(k+1)*dsd(j,k
+1)*d(k+1)^3)*(d(k+1)-d(k));
    end
    Rnew(j) = Rnew(j)*6*pi*1e-4
end
That's the trapezoidal rule.

Best wishes
Torsten.

Tags for 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