how to make an infinite sum
Show older comments
Hi guys,
I have an infintite sum, I write the following code, but it doesn't compute the values.
function y=Phiplus(p,k,delta,alpha)
h=0;
for n=1:1000,
f=2^p*exp(-alpha/2)*(alpha/2)^n*(gamma(k/2)-gamma(p+delta/2+n)*gammainc(p+delta/2+n,k/2))/(factorial(n)*gamma(delta/2+n));
h=h+f;
end
y=h;
end
Pleas help! I use this function to calculate the price of a call option:
Call(i,j,m,n)=exp(-q(j)*T(i))*S*Phiplus(0,k(i,j,n)^2/tau(i,j), delta(m), x^2/tau(i,j))-exp(-(r(j)+b(j))*T(i))*K(n)*(x^2/tau(i,j))^(1/(2*abs(beta)))* Phiplus(-0.5/abs(beta),k(i,j,n)^2/tau(i,j),delta(m),x^2/tau(i,j));
If you need any additional info I'm ready to provide it.
2 Comments
dpb
on 9 Jun 2013
Instead of using a fixed upper limit on the loop, consider using a conditional loop, stopping when the additional term no longer adds more than some preset tolerance (say 0.1% or so?) to the value.
factorial(1000) (and long before then) is going to be inf so at that point the term is going to be identically zero and since there are negative exponentials, they're going to zero as well and so I presume you're getting NaN as a result owing to adding 0/0 to the result.
The answer is to only compute the minimum number of terms you need and to probably eliminate the factorial function entirely but instead simply divide each time by the next N in the sequence.
I'd think you probably will need only a few terms at most to get the result but that will depend upon the magnitude of the various coefficients. If both numerator and denominator are small from the git-go, you may need to recast entirely, but try the obvious first.
Daniel
on 9 Jun 2013
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!