Thread Subject: conv doesn't agree with analytical solution!

Subject: conv doesn't agree with analytical solution!

From: Ida Haggstrom

Date: 26 Oct, 2008 15:06:01

Message: 1 of 6

Hi all!
I've encountered somewhat of a problem in Matlab... that is when I try to convolve two functions! I've tried to convolve two exponentials, a convolution I can solve analytically. The problem is that when I plot the analytical solution to the solution from matlabs "conv" function, they don't agree! I also tried to make the convolution from the definition:
a(t) x b(t) = int(a(tau)*b(t-tau)dtau)
using the trapz function. This result agrees with the analytical result, but conv doesn't! Here's a bit of the code I used for testing conv:
%---------------------------------------
t = 0:20; %times
a = exp(-t); %first function
b = exp(-t); %second function
c = t.*exp(-t); %Analytical solution to (a x b)

%%%%%%%% Matlabs conv-function %%%%%%%%%%%%%%
test1 = conv(a,b);
test1 = test1(1:numel(t)); %should equal c...
%%%%%%%% Matlabs trapz-function %%%%%%%%%%%%%%
test2 = zeros(1,numel(t));
for i = 2:numel(t)
    test2(i) = trapz(t(1:i), a(1:i).*exp(-(t(i)-t(1:i))));
end

plot(t,c,'-o',t,test1,'r',t,test2,'g');
legend('Analytical','conv','trapz');
%--------------------------------------
What is actually happening??? Can I trust conv? Thanks!
/Ida

Subject: conv doesn't agree with analytical solution!

From: Bruno Luong

Date: 26 Oct, 2008 15:29:01

Message: 2 of 6

Conv returns a discrete sum and not integral. If you intend to use it to compute integral convolution (it is *not* its purpose), you won't get anything accurate with a time step as coarse as 1: your function a and b decrease by 2.71828 from one point to another.

dt=0.01;
t = 0:dt:20; %times
a = exp(-t); %first function
b = exp(-t); %second function
c = t.*exp(-t); %Analytical solution to (a x b)

%%%%%%%% Matlabs conv-function %%%%%%%%%%%%%%
test1 = dt*conv(a,b);
test1 = test1(1:numel(t)); %should equal c...
%%%%%%%% Matlabs trapz-function %%%%%%%%%%%%%%
test2 = zeros(1,numel(t));
for i = 2:numel(t)
    test2(i) = trapz(t(1:i), a(1:i).*exp(-(t(i)-t(1:i))));
end

plot(t,c,'-o',t,test1,'r',t,test2,'g');
legend('Analytical','conv','trapz');

Bruno

Subject: conv doesn't agree with analytical solution!

From: Ida Haggstrom

Date: 27 Oct, 2008 10:07:01

Message: 3 of 6

Hi Bruno, and thanks for your time!
I see what you mean, and the results are good when I try your function. However, the reason I'm trying this (conv and trapz) is because I have physical data, with a nonlinear time vector like:
t = [0 5 10 15 ... 60 70 ... 150 180 ...240 300 ... 480 600 ...1620];
When I convolve a and b (as below) using conv, the results are not the same as when I use the trapz approach below. The trapz approach agrees with the analytical solution (using the same t vector of course) whereas the conv solution doesn't. Can I use conv in any way for my problem? Cheers,
/Ida


"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ge22bt$een$1@fred.mathworks.com>...
> Conv returns a discrete sum and not integral. If you intend to use it to compute integral convolution (it is *not* its purpose), you won't get anything accurate with a time step as coarse as 1: your function a and b decrease by 2.71828 from one point to another.
>
> dt=0.01;
> t = 0:dt:20; %times
> a = exp(-t); %first function
> b = exp(-t); %second function
> c = t.*exp(-t); %Analytical solution to (a x b)
>
> %%%%%%%% Matlabs conv-function %%%%%%%%%%%%%%
> test1 = dt*conv(a,b);
> test1 = test1(1:numel(t)); %should equal c...
> %%%%%%%% Matlabs trapz-function %%%%%%%%%%%%%%
> test2 = zeros(1,numel(t));
> for i = 2:numel(t)
> test2(i) = trapz(t(1:i), a(1:i).*exp(-(t(i)-t(1:i))));
> end
>
> plot(t,c,'-o',t,test1,'r',t,test2,'g');
> legend('Analytical','conv','trapz');
>
> Bruno

Subject: conv doesn't agree with analytical solution!

From: Bruno Luong

Date: 27 Oct, 2008 10:54:01

Message: 4 of 6

"Ida Haggstrom" <ida_haggstrom@yahoo.se> wrote in message <ge43s5$pcj$1@fred.mathworks.com>...
> Hi Bruno, and thanks for your time!
> I see what you mean, and the results are good when I try your function. However, the reason I'm trying this (conv and trapz) is because I have physical data, with a nonlinear time vector like:
> t = [0 5 10 15 ... 60 70 ... 150 180 ...240 300 ... 480 600 ...1620];
> When I convolve a and b (as below) using conv, the results are not the same as when I use the trapz approach below. The trapz approach agrees with the analytical solution (using the same t vector of course) whereas the conv solution doesn't. Can I use conv in any way for my problem? Cheers,

I don't think so, conv function only approximates the mathematical integral convolution when the time-step is constant (and fine enough). In your case the first criteria is not fulfilled.

However you could preprocess the data by interpolating them on the regular grid.

Bruno

Subject: conv doesn't agree with analytical solution!

From: Rune Allnor

Date: 28 Oct, 2008 07:47:14

Message: 5 of 6

On 27 Okt, 11:07, "Ida Haggstrom" <ida_haggst...@yahoo.se> wrote:
> Hi Bruno, and thanks for your time!
> I see what you mean, and the results are good when I try your function. However, the reason I'm trying this (conv and trapz) is because I have physical data, with a nonlinear time vector like:
> t = [0 5 10 15 ... 60 70 ... 150 180 ...240 300 ... 480 600 ...1620];
> When I convolve a and b (as below) using conv, the results are not the same as when I use the trapz approach below. The trapz approach agrees with the analytical solution (using the same t vector of course) whereas the conv solution doesn't. Can I use conv in any way for my problem?

No. CONV requires that the data are regularly sampled.
It is not obvious how to process irregularly sampled
data. One place where I have seen this discussed is

Press & al: "Numerical recipies in C" (1992)

The book contains very good overview articles and many
good references. Well worth a try.

Rune

Subject: conv doesn't agree with analytical solution!

From: Ida Haggstrom

Date: 28 Oct, 2008 15:08:03

Message: 6 of 6

Hi Rune!
Hm, I guess conv is not such a good idea for my problem... I will try another approach to solving it instead! Thanks all for your valuable tips! Cheers,
/Ida

Rune Allnor <allnor@tele.ntnu.no> wrote in message <92eaeb03-3727-40dd-9b61-d7ab96174fd8@u28g2000hsc.googlegroups.com>...
> On 27 Okt, 11:07, "Ida Haggstrom" <ida_haggst...@yahoo.se> wrote:
> > Hi Bruno, and thanks for your time!
> > I see what you mean, and the results are good when I try your function. However, the reason I'm trying this (conv and trapz) is because I have physical data, with a nonlinear time vector like:
> > t = [0 5 10 15 ... 60 70 ... 150 180 ...240 300 ... 480 600 ...1620];
> > When I convolve a and b (as below) using conv, the results are not the same as when I use the trapz approach below. The trapz approach agrees with the analytical solution (using the same t vector of course) whereas the conv solution doesn't. Can I use conv in any way for my problem?
>
> No. CONV requires that the data are regularly sampled.
> It is not obvious how to process irregularly sampled
> data. One place where I have seen this discussed is
>
> Press & al: "Numerical recipies in C" (1992)
>
> The book contains very good overview articles and many
> good references. Well worth a try.
>
> Rune

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
conv Ida Haggstrom 26 Oct, 2008 11:10:05
convolution Ida Haggstrom 26 Oct, 2008 11:10:05
trapz Ida Haggstrom 26 Oct, 2008 11:10:05
rssFeed for this Thread

Contact us at files@mathworks.com