开发者

How to calculate the convolution of a function with itself in MATLAB and WolframAlpha?

开发者 https://www.devze.com 2023-04-04 12:00 出处:网络
I am trying to calculate the convolution of x(t开发者_如何学C) = 1, -1<=t<=1 x(t) = 0, outside

I am trying to calculate the convolution of

x(t开发者_如何学C) = 1, -1<=t<=1
x(t) = 0, outside

with itself using the definition.

http://en.wikipedia.org/wiki/Convolution

I know how to do using the Matlab function conv, but I want to use the integral definition. My knowledge of Matlab and WolframAlpha is very limited.


I am still learning Mathematica myself, but here is what I came up with..

First we define the piecewise function (I am using the example from the Wikipedia page)

f[x_] := Piecewise[{{1, -0.5 <= x <= 0.5}}, 0]

How to calculate the convolution of a function with itself in MATLAB and WolframAlpha?

Lets plot the function:

Plot[f[x], {x, -2, 2}, PlotStyle -> Thick, Exclusions -> None]

How to calculate the convolution of a function with itself in MATLAB and WolframAlpha?

Then we write the function that defines the convolution of f with itself:

g[t_] = Integrate[f[x]*f[t - x], {x, -Infinity, Infinity}]

How to calculate the convolution of a function with itself in MATLAB and WolframAlpha?

and the plot:

Plot[g[t], {t, -2, 2}, PlotStyle -> Thick]

How to calculate the convolution of a function with itself in MATLAB and WolframAlpha?


EDIT

I tried to do the same in MATLAB/MuPad, I wasn't as successful:

f := x -> piecewise([x < -0.5 or x > 0.5, 0], [x >= -0.5 and x <= 0.5, 1])

How to calculate the convolution of a function with itself in MATLAB and WolframAlpha?

plot(f, x = -2..2)

How to calculate the convolution of a function with itself in MATLAB and WolframAlpha?

However when I try to compute the integral, it took almost a minute to return this:

g := t -> int(f(x)*f(t-x), x = -infinity..infinity)

How to calculate the convolution of a function with itself in MATLAB and WolframAlpha?

the plot (also took too long)

plot(g, t = -2..2)

How to calculate the convolution of a function with itself in MATLAB and WolframAlpha?

Note the same could have been done from inside MATLAB with the syntax:

evalin(symengine,'<MUPAD EXPRESSIONS HERE>')


Mathematica actually has a convolve function. The documentation on it has a number of different examples:

http://reference.wolfram.com/mathematica/ref/Convolve.html?q=Convolve&lang=en


I don't know much about Mathematica so I can only help you (partially) about the Matlab part.

To do the convolution with the Matlab conv functions means you do it numerically. What you mean with the integral definition is that you want to do it symbolically. For this you need the Matlab Symbolic Toolbox. This is essentially a Maple plugin for Matlab. So what you want to know is how it works in Maple.

You might find this link useful (wikibooks) for an introduction on the MuPad in Matlab.

I tried to implement your box function as a function in Matlab as

t = sym('t')
f =  (t > -1) + (t < 1) - 1

However this does not work when t is ob symbolic type Function 'gt' is not implemented for MuPAD symbolic objects.

You could declare f it as a piecewise function see (matlab online help), this did not work in my Matlab though. The examples are all Maple syntax, so they would work in Maple straight away.

To circumvent this, I used

t = sym('t')
s = sym('s')
f = @(t) heaviside(t + 1) - heaviside(t - 1)

Unfortunately this is not successful

I = int(f(s)*f(t-s),s, 0, t)

gives

Warning: Explicit integral could not be found.

and does not provide an explicit result. You can still evaluate this expression, e.g.

>> subs(I, t, 1.5)

ans =
1/2

But Matlab/MuPad failed to give you and explicit expression in terms of t. This is not really unexpected as the function f is discontinuous. This is not so easy for symbolic computations.

Now I would go and help the computer, fortunately for the example that you asked the answer is very easy. The convolution in your example is simply the int_0^t BoxFunction(s) * BoxFunction(t - s) ds. The integrant BoxFunction(s) * BoxFunction(t - s) is again a box function, just not one that goes from [-1,1] but to a smaller interval (that depends on t). Then you only need to integrate the function f(x)=1 over this smaller interval.

Some of those steps you would have to to by hand first, then you could try to re-enter them to Matlab. You don't even need a computer algebra program at all to get to the answer.

Maybe Matematica or Maple could actually solve this problem, remember that the MuPad shipped with Matlab is only a stripped down version of Maple. Maybe the above might still help you, it should give you the idea how things work together. Try to put in a nicer function for f, e.g. a polynomial and you should get it working.

0

精彩评论

暂无评论...
验证码 换一张
取 消