How do I plot functions involving integration?

조회 수: 3 (최근 30일)
FancisHsu
FancisHsu 2015년 12월 22일
댓글: Brendan Hamm 2015년 12월 24일
I have a characteristic function CF(t, param1, param2, ...) available (here param's are parameters of some probability distribution and are considered fixed). I want to use the Gil-Pelaez formula to obtain the CDF instead, so I wrote the following:
function [ F ] = CDF( x, param1, param2, ... )
integrand = @(v) imag(exp(-1i.*v.*x) .* CF(t, param1, param2, ...)) ./ v;
F = 0.5 - (1./pi) .* integral(integrand, 0, 100);
end
This works for single value, e.g. CDF(0.1, param1, param2, ...) gives me desired result. Now I want to plot CDF against a range of x, so I did:
x = linspace(-1,1,100);
y = CDF(x, param1, param2, ...)
plot(x, y)
and I end up with the errors like these:
Not enough input arguments.
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
But y = CDF(x, param1, param2, ...) does work, so the culprit seems to be the exp(-1i.*v.*x) part of the integrand, as the size of v and x does not match. But I am not sure how to fix this.
  댓글 수: 1
Brendan Hamm
Brendan Hamm 2015년 12월 23일
I removed the plot tag as this question really has nothing to do with the plotting. I would suggest a question Title change as well.

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

채택된 답변

Brendan Hamm
Brendan Hamm 2015년 12월 23일
We can not anticipate ahead of time what the size of v will be in the integral function, in fact I think this changes to approximate the integral within a tolerance. For this reason we can only pass a scalar at a time, but we can vectorize this still with a function-function.
The use of arrayfun should do this:
x = linspace(-1,1); % Default is 100 elements
y = arrayfun(@(x) CDF(x,param1,param2),x); % Uses each value of x as a separate input to a call to CDF
%%Example
f = @(x,y) y*sin(x);
% I want to find the integral w.r.t x from 0 to pi/6 with different values of y fixed:
vals = 0:5;
arrayfun(@(y) integral(@(x) f(x,y),0,pi/6),vals)
ans =
Columns 1 through 4
0 0.1340 0.2679 0.4019
Columns 5 through 6
0.5359 0.6699
In my example arrayfun will pass each value of vals to y in a separate call. This value of y is then used in an anonymous function input to the integral function.
  댓글 수: 2
Walter Roberson
Walter Roberson 2015년 12월 23일
Wouldn't it be appropriate to switch to a ArrayValued function in integral(); http://www.mathworks.com/help/matlab/ref/integral.html#btw3ipp-6 ? Though we need clarification of the size returned by FC... it looks like it is scalar but it is difficult to say. And I am not sure where the t parameter in the CF call is coming from -- is it the same as the x parameter to the function?
Brendan Hamm
Brendan Hamm 2015년 12월 24일
I like your answer better Walter, I would suggest to place this as an answer so we can up vote you :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by