convolution of two functions

조회 수: 129 (최근 30일)
richard
richard 2013년 9월 1일
댓글: Michael Reshko 2019년 4월 4일
I want to convolve two functions:
f=inline('gaussmf(x,[3,0])','x')
h=inline('normpdf(x,0,4)','x')
So the conv will not work since it only deals with vectors, I have functions I am wanting to convolve.
How do I tell matlab to do this? Do I turn my function into a matrix/vector or something??

답변 (3개)

John BG
John BG 2016년 8월 25일
편집: John BG 2016년 8월 25일
Richard
You don't really need the Symbolic toolbox. MATLAB already have classes to define standard and custom probability functions. For instance to define the Gaussian pdf:
pd1=ProbDistUnivParam('normal',[0 3]);
Since plotting is windowing, you have to define the x range, and then obtain the values of the pdf to input to conv:
x=-10:.1:10;y1=pdf(pd1,x);
The second pdf:
pd2=makedist('Uniform');
pd2.Lower=0;pd2.Upper=4;
y2=pdf(pd2,x);
Now convolving y1 and y2
z=conv(y1,y2);figure(1);plot(x,y1,x,y2);figure(2);plot(z);
The use of function int suggested by Roger comes from the definition of the convolution, that can be obtained with symbolic parameters. But you will need to 'frame' or 'window' anyway when attempting any plot as you mention is your goal here.
Such answer, integrating along t the product pdf1(t)*pdf2(t-x), is explained in the question
with accepted answer by Ghada Saleh, following the key lines of that answer:
syms x b c t;
f1 = a*exp((-x)/(b))+1;
f2 = (1/(4*pi*c^2))*exp((-x^2)/(4*c^2));
f2_c = (1/(4*pi*c^2))*exp((-(t-x)^2)/(4*c^2)); %f2_c = f2(t-x)
result = int(f1*f2_c,t,-inf,inf);
Richard, please if you find my answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
  댓글 수: 1
Michael Reshko
Michael Reshko 2019년 4월 4일
You begin with two functions defined on [-10:10], but your last plot of the convolution is on [0; 450]. What are the x values corresponding to the convolution z?

댓글을 달려면 로그인하십시오.


Roger Stafford
Roger Stafford 2013년 9월 2일
This is hopefully a problem that the symbolic toolbox's 'int' function could solve for you. The convolution of the two functions you have given can be expressed as:
F(t) = int(f(s)*h(t-s),'s',-inf,+inf)
If 'int' can get an answer, it will depend on t and that will be your convolution function.
The reason I have hope that 'int' can succeed in this is that I know I could solve it by hand. Your integrand would look something like this:
exp(-s^2/18)*1/sqrt(2*pi)/4*exp(-(t-s)^2/32) =
1/sqrt(2*pi)/4 * exp(-s^2/18-(t-s)^2/32)
The quantity within 'exp' is a quadratic in s and by doing a "completion of the square" it can be expressed in the form
exp(-(s-a)/(2*b^2)) * something that doesn't depend on s
where a and b are certain quantities which depend on t. Hence this is something that can be evaluated exactly as a function of t (with sufficient sweat.) But as I say, hopefully 'int' could save you that sweat.
  댓글 수: 1
richard
richard 2013년 9월 2일
My goal is to plot F(t) but an 'explicit integral' could not be found. Am I at a dead end, or can matlab plot F(t) without knowing what it is/computing it...

댓글을 달려면 로그인하십시오.


Roger Stafford
Roger Stafford 2013년 9월 2일
I was hoping 'int' would solve this for you. Shame on 'int'! Oh well, assisted by my own ancient symbolic toolbox, I have hand-derived the convolution function for you. It is simply this. With the functions
f(x) = gaussmf(x,[sg,mg])
h(x) = normpdf(x,mn,sn))
their convolution will be:
F(t) = int(f(s)*h(t-s),'s',-inf,inf) =
sg/sqrt(sg^2+sn^2) * exp(-(t-mn-mg)^2/(2*(sg^2+sn^2)))
You can plug in your desired values for the mean and standard deviation for the two distributions as values for mg, sg, mn, sn.
  댓글 수: 2
richard
richard 2013년 9월 4일
So I know realize the goal of my actual assignment was not to find the actual F(t) function, I just had to plot it, and find the area under the F(t) curve.
I plotted out F(t) by using the conv2 function, and then I used the trapz command to find the area under F(t).
The F(t) you found is correct, because I reconciled finding the integral of your F(t) (using 'int') & using the trapz command for the curve I obtained and the results matched.
I thought I owed you an explanation for your assistance, and your help made me think more about the problem (and it helped me check my work!)
Now I know how 'conv2' works, I was using the wrong command in 'conv'. How does 'accept this answer' work anyway? Do you get free toolboxes if enough people approve your answers?
Roger Stafford
Roger Stafford 2013년 9월 4일
You don't actually need to use 'trapz' to find the area under F(t). There is an exact answer.
int(F(t),'t','-inf','inf') =
sg/sqrt(sg^2+sn^2) * sqrt(2*pi)*sqrt(sg^2+sn^2) =
sqrt(2*pi)*sg
which interestingly enough is the normalizing factor whose reciprocal is needed to convert the 'gaussmf' gaussian membership function to a normal pdf distribution.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MuPAD에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by