Please, help to find a mistake in the code for double integral
1 次查看(过去 30 天)
显示 更早的评论
I have a double integral (a picture of formula is attached) and a code. But this integral should be equal 1 at s = 0 at any parameters n, t, k. Thus, all curves should start from the point (0,1). But, I obtain at vpa the ans = 0.35331 at s = 0, and curves start from different points at different parameters. I suspect that there is an error somewhere in the code entry itself, but I can't see it now. Please help me to find it.
n = 1 ;
t = 1;
k = 1; %(k is ksi in formula)
s = 0:0.5:3;
fun = @(x,q,p) ((x.*exp(2*n*t.*(x.^2)))./sqrt(1-x.^2)).*(exp(-2*t.*(q.^2)).*besselj(0,q.*p.*x).*(1+((sqrt(-pi)./(q*k)).*(1+((q*k).^2)/2).*erfi(q*k*sqrt(-1)/2).*exp(-((q*k).^2)/4))));
f3 = arrayfun(@(p)integral2(@(x,q)fun(x,q,p),0,1,0,Inf),s);
Cor = -((2*k*sqrt(2*n*t)*exp(-2*n*t))/(pi*erf(sqrt(2*n*t))*(atan(k/(2*sqrt(2*t)))-sqrt(2*t)/(2*k*(0.25+(2*t)/(k^2)))))).*f3;
plot(Cor,'b-')
%vpa(Cor,5)
4 个评论
Torsten
2023-2-24
And if "erf" does not work, you take another function that sounds similarily and hope you get the correct result ?
Google
error function for complex argument & matlab
and you will find some functions from the file exchange to compute the error function with complex argument (if this is really what is meant in the formula).
回答(2 个)
Torsten
2023-2-24
移动:Image Analyst
2023-2-24
You can use
erf(i*z) = i*erfi(z)
Thus in your case
erf(i*q*k/2) = i*erfi(q*k/2)
But you will come into trouble here since you integrate from 0 to Inf with respect to q.
And erfi(x) -> Inf very fast as x-> Inf.
You will have to evaluate
i*erfi(q*k/2)*exp(-(q*k/2).^2)
as one expression to avoid Inf * 0 here.
Torsten
2023-2-24
n = 1 ;
t = 1;
k = 1; %(k is ksi in formula)
s = 0:0.5:20;
fun = @(x,q,p) ((x.*exp(2*n*t.*(x.^2)))./sqrt(1-x.^2)).*(exp(-2*t.*(q.^2)).*besselj(0,q.*p.*x).*(1+((sqrt(-pi)./(q*k)).*(1+((q*k).^2)/2).*func(q,k))));
f3 = arrayfun(@(p)integral2(@(x,q)fun(x,q,p),0,1,0,Inf),s);
Cor = -((2*k*sqrt(2*n*t)*exp(-2*n*t))/(pi*erf(sqrt(2*n*t))*(atan(k/(2*sqrt(2*t)))-sqrt(2*t)/(2*k*(0.25+(2*t)/(k^2)))))).*f3;
plot(s,Cor,'b-')
grid on
function values = func(q,k)
values = arrayfun(@(q)1i*2/sqrt(pi)*integral(@(t)exp(t.^2-((q*k)/2)^2),0,q*k/2),q);
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Software Development Tools 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!