如何在 MATLAB 和 WolframAlpha 中计算函数与其自身的卷积?

发布于 2024-12-04 05:20:38 字数 299 浏览 4 评论 0原文

来计算 与自身的卷积

x(t) = 1, -1<=t<=1
x(t) = 0, outside

我正在尝试使用定义

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

我知道如何使用 Matlab函数 conv,但我想使用积分定义。我对 Matlab 和 WolframAlpha 的了解非常有限。

I am trying to calculate the convolution of

x(t) = 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜巴黎 2024-12-11 05:20:38

我自己仍在学习 Mathematica,但这是我想到的..

首先我们定义分段函数(我使用维基百科页面中的示例)

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

piecewise_function

让我们绘制函数:

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

function_plot

然后我们编写定义f 与其自身的卷积:

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

volvo_integral

和绘图:

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

volvo_plot


编辑

我尝试在 MATLAB/MuPad 中执行相同操作,但没有那么成功:

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

func_def

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

func_plot

但是当我尝试计算积分,花了将近一分钟才返回:

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

卷积

绘图(也花了太长时间)

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

<图像src =“https://i.sstatic.net/pnY90.png”alt =“卷积_plot”>

请注意,可以使用以下语法从 MATLAB 内部完成相同的操作:

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

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]

piecewise_function

Lets plot the function:

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

function_plot

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

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

convolution_integral

and the plot:

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

convolution_plot


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])

func_def

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

func_plot

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)

convolution

the plot (also took too long)

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

convolution_plot

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

evalin(symengine,'<MUPAD EXPRESSIONS HERE>')
爱你不解释 2024-12-11 05:20:38

Mathematica 实际上有一个卷积函数。它的文档有许多不同的示例:

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

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

残疾 2024-12-11 05:20:38

我对 Mathematica 不太了解,所以我只能(部分)帮助你了解 Matlab 部分。

使用 Matlab 卷积函数进行卷积意味着您可以通过数值方式进行卷积。积分定义的意思是你想象征性地进行它。为此,您需要 Matlab 符号工具箱。这本质上是 Matlab 的 Maple 插件。所以你想知道的是它在 Maple 中是如何工作的。

您可能会发现此链接对于介绍 Matlab 中的 MuPad 很有用(wikibooks)

我尝试将您的框函数实现为 Matlab 中的函数,但是

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

当 t 为 ob 符号类型 MuPAD 符号对象未实现函数“gt”时,这不起作用

您可以将 f 声明为分段函数,请参阅

为了避免这种情况,我使用了

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

不幸的是这并没有成功

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

给出

Warning: Explicit integral could not be found.

并且没有提供明确的结果。您仍然可以评估这个表达式,例如

>> subs(I, t, 1.5)

ans =
1/2

,但是Matlab/MuPad 未能为您提供以t 表示的显式表达式。这并不意外,因为函数 f 是不连续的。这对于符号计算来说并不容易。

现在我要去帮助计算机,幸运的是,对于你问的例子来说,答案很简单。您的示例中的卷积只是 int_0^t BoxFunction(s) * BoxFunction(t - s) ds。积分 BoxFunction(s) * BoxFunction(t - s) 又是一个框函数,只是不是从 [-1,1] 开始,而是进入更小的区间(取决于 t)。那么您只需在这个较小的区间内对函数 f(x)=1 进行积分即可。

其中一些步骤您必须先手动执行,然后您可以尝试将它们重新输入到 Matlab。您甚至根本不需要计算机代数程序即可得到答案。

也许 Matematica 或 Maple 实际上可以解决这个问题,请记住,Matlab 附带的 MuPad 只是 Maple 的精简版本。也许上面的内容可能仍然对您有帮助,它应该让您了解事物如何协同工作。尝试为 f 添加一个更好的函数,例如多项式,您应该可以让它工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文