I created an iteration in order to evaluate the price of a Call with Heston model, but when it comes the time to integrate, Matlab shows me lots of errors.
Everything works fine until the integral, I evaluated everything ignoring the integral part, and the functions work fine.
u=[0.5,-0.5];
a=kappa*theta;
b=[kappa+lambda-rho*sigma,kappa+lambda];
for m=1:2
d{m} = @(x) sqrt((rho*sigma*x*i-b(m))^2-(sigma^2)*(2*u(m)*x*i-x^2)^2);
g{m} = @(x) (b(m)-rho*sigma*x*i+d{m}(x))/(b(m)-rho*sigma*x*i-d{m}(x));
D{m} = @(x) ((b(m)-rho*sigma*x*i+d{m}(x))/sigma^2)*((1-exp(1)^(tau*d{m}(x)))/(1-g{m}(x)*exp(1)^(tau*d{m}(x))));
C{m} = @(x) r*x*i*tau+(a/sigma^2)*((b(m)-rho*sigma*x*i+d{m}(x))*tau-2*log((1-g{m}(x)*exp(1)^(tau*d{m}(x)))/(1-g{m}(x))));
f{m} = @(x) exp(C{m}(x)+D{m}(x)*v0+i*x*log(S0));
F{m} = @(x) real((exp(-i*x*log(K))*f{m}(x))/(i*x));
P(m) = integral(F{m},0,inf);
end
This are the errors I get once I evaluate the integral:
Error using ^ One argument must be a square matrix and the other must be a scalar. Use POWER (.^) for elementwise power.
(Lots of errors with the functions but they worked fine before trying the integral)
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 83) [q,errbnd] = vadapt(@AToInfInvTransform,interval);
Error in integral (line 88) Q = integralCalc(fun,a,b,opstruct);
You need to convert every multiplication, division and exponent operation to its scalar version:
for m=1:2
d{m} = @(x) sqrt((rho.*sigma.*x.*i-b(m)).^2-(sigma.^2).*(2.*u(m).*x.*i-x.^2).^2);
g{m} = @(x) (b(m)-rho.*sigma.*x.*i+d{m}(x))./(b(m)-rho.*sigma.*x.*i-d{m}(x));
D{m} = @(x) ((b(m)-rho.*sigma.*x.*i+d{m}(x))./sigma.^2).*((1-exp(1).^(tau.*d{m}(x)))./(1-g{m}(x).*exp(1).^(tau.*d{m}(x))));
C{m} = @(x) rho.*x.*i.*tau+(a./sigma.^2).*((b(m)-rho.*sigma.*x.*i+d{m}(x)).*tau-2.*log((1-g{m}(x).*exp(1).^(tau.*d{m}(x)))./(1-g{m}(x))));
f{m} = @(x) exp(C{m}(x)+D{m}(x).*v0+i.*x.*log(S0));
F{m} = @(x) real((exp(-i.*x.*log(K)).*f{m}(x))./(i.*x));
P(m) = integral(F{m},0,inf);
end
This works because while integrating, Matlab will evaluate your function F{m}
over a vector and not scalar values. The errors you get are basically telling you the same.