How do I plot functions involving integration?
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
FancisHsu
el 22 de Dic. de 2015
Comentada: Brendan Hamm
el 24 de Dic. de 2015
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 comentario
Brendan Hamm
el 23 de Dic. de 2015
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.
Respuesta aceptada
Brendan Hamm
el 23 de Dic. de 2015
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 comentarios
Walter Roberson
el 23 de Dic. de 2015
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
el 24 de Dic. de 2015
I like your answer better Walter, I would suggest to place this as an answer so we can up vote you :)
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!