DSPRelated.com
Forums

cumsum() in Matlab

Started by cirip April 8, 2004
Hello All,

I believe a lot of the people on this ng are users of Matlab and heavily
into mathematics, so here is my question.

I am trying to integrate a cosine function. If I compare the analitical
formula result with the numerical integration result using cumsum(), there
is a small difference. Is there anything I can do to minimize that
difference, ideally make it zero?

Unfortunately, using another integration method is not an option in my case.

I can come back with more details if required.

Thank you,
Cirip


Cirip,

   Numerical integration (cumsum) cannot guarantee absolute accuracy.
There are more accurate schemes (cumtrapz),
to enhance the accuracy  Increase the sampling rate.

 Here are two examples


dt=1/1000; wt=linspace(0,10,10/dt)';   y=cos(wt); yi1=cumsum(y)*dt ;
yi2=cumtrapz(y)*dt; plot([sin(wt)-yi1 sin(wt)-yi2]);
legend('cumsum-error','trapez-error')


% use interpolation

dt2=1/10000;                            % fine grid
wt2=linspace(0,10,10/dt2)';    % prepare grid
yI=spline(wt,y,wt2);                  % interpolate
yi3=cumtrapz(yI)*dt2;              % integrate on finner grid
plot(wt,[sin(wt)-yi1 sin(wt)-yi2],wt2,sin(wt2)-yi3 );  % compare


HTH
-- 
izhak

"cirip" <cirip@NOT_VALID.com> wrote in message
news:wc4dc.15651$JTG1.14466@news04.bloor.is.net.cable.rogers.com...
> Hello All, > > I believe a lot of the people on this ng are users of Matlab and heavily > into mathematics, so here is my question. > > I am trying to integrate a cosine function. If I compare the analitical > formula result with the numerical integration result using cumsum(), there > is a small difference. Is there anything I can do to minimize that > difference, ideally make it zero? > > Unfortunately, using another integration method is not an option in my
case.
> > I can come back with more details if required. > > Thank you, > Cirip > >
Thank you Izhak,
Cirip


"Izhak" <bucher@technion.ac.il> wrote in message
news:c52mpq$fc$1@news.iucc.ac.il...
> Cirip, > > Numerical integration (cumsum) cannot guarantee absolute accuracy. > There are more accurate schemes (cumtrapz), > to enhance the accuracy Increase the sampling rate. > > Here are two examples > > > dt=1/1000; wt=linspace(0,10,10/dt)'; y=cos(wt); yi1=cumsum(y)*dt ; > yi2=cumtrapz(y)*dt; plot([sin(wt)-yi1 sin(wt)-yi2]); > legend('cumsum-error','trapez-error') > > > % use interpolation > > dt2=1/10000; % fine grid > wt2=linspace(0,10,10/dt2)'; % prepare grid > yI=spline(wt,y,wt2); % interpolate > yi3=cumtrapz(yI)*dt2; % integrate on finner grid > plot(wt,[sin(wt)-yi1 sin(wt)-yi2],wt2,sin(wt2)-yi3 ); % compare > > > HTH > -- > izhak > > "cirip" <cirip@NOT_VALID.com> wrote in message > news:wc4dc.15651$JTG1.14466@news04.bloor.is.net.cable.rogers.com... > > Hello All, > > > > I believe a lot of the people on this ng are users of Matlab and heavily > > into mathematics, so here is my question. > > > > I am trying to integrate a cosine function. If I compare the analitical > > formula result with the numerical integration result using cumsum(),
there
> > is a small difference. Is there anything I can do to minimize that > > difference, ideally make it zero? > > > > Unfortunately, using another integration method is not an option in my > case. > > > > I can come back with more details if required. > > > > Thank you, > > Cirip > > > > > >