MATLAB: How to plot functions involving integration

integrationMATLAB

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.

Best Answer

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.