Integral from 0 to inf with imaginary numbers takes too long
조회 수: 3 (최근 30일)
이전 댓글 표시
I'm testing my code and most lines goes well. When it comes to the last opration it takes too long and is always busy.
Here's my code:
syms Kt Ks Cs M m to
syms x t
Kt=1
Ks=1
Cs=1
M=1
m=1
to=1
y=x*(exp(1))^(-sqrt(-1)*t*x)
S=int(y,-to/2,to/2)
SS=(1/2)*(abs(S))^2
G=2*SS
H=(t^2)*abs((Kt*(Ks+sqrt(-1)*t*Cs))/(((-(t^2)*M+Ks+sqrt(-1)*t*Cs)*(-(t^2)*m+Ks+sqrt(-1)*t*Cs))-((Ks+sqrt(-1)*t*Cs)^2)))
sigpre=G/(H^2)
sig2=int(sigpre,0,inf)
sig=sig2^(1/2)
and it stucks when doing this:
sig2=int(sigpre,0,inf)
Could anyone please fix my code?
댓글 수: 0
채택된 답변
Star Strider
2023년 2월 11일
First, the Symbolic Math Toolbox is not going to be as efficient as direct numerical calculations.
Second, you never told the int function what variable of integration is. It is necessary to specify whether it is ‘x’ or ‘t’ (or something else).
Supply that information, and use the integral or integral2 functions instead (depending on what you are integrating, something I cannot figure out from the posted code, so it does not surprise me that the int function could not figure it out either).
Here is an example —
% syms Kt Ks Cs M m to
% syms x t
Kt=1
Ks=1
Cs=1
M=1
m=1
to=1
y = @(x,t) x.*exp(-sqrt(1i*t*x));
S = @(x) integral(@(t) y(x,t),-to/2,to/2)
SS = @(x) (1/2)*(abs(S(x))).^2;
G = @(x) 2*SS(x);
H = @(x,t) (t.^2).*abs((Kt.*(Ks+sqrt(-1).*t.*Cs))./(((-(t.^2).*M+Ks+sqrt(-1).*t.*Cs).*(-(t.^2).*m+Ks+sqrt(-1).*t.*Cs))-((Ks+sqrt(-1).*t.*Cs).^2)));
sigpre = @(x,t) G(x)./(H(x,t).^2)
sig2 = @(t) integral(sigpre(x,t),0,Inf)
sig = sig2(t).^(1/2)
Make appropriate changes to get the result you want.
I will help with this, however I need much more information than the code provides.
.
댓글 수: 4
추가 답변 (2개)
Paul
2023년 2월 11일
편집: Paul
2023년 2월 11일
Hi BOSHU
Here is my attempt.
syms Kt Ks Cs M m to % overwritten later
syms x
Assume t is real, is that a good assumption?
syms t real
Kt = 1;
Ks = 1;
Cs = 1;
M = 1;
m = 1;
to = 1;
When using symbolic, it's best to use symbolic constants to force the desired expression. Here, exp(1) is first evaluated as a double
y = x*(exp(1))^(-sqrt(-1)*t*x)
Force symbolic
y = x*(exp(sym(1)))^(-1i*t*x)
Or more simply
y = x*exp(-1i*t*x)
Evaluate S and simplify
symvar(y)
symvar(y,1)
This line integratres wrt x, better to make that explicit.
%S = int(y,-to/2,to/2)
S = int(y,x,-to/2,to/2)
[num,den] = numden(S)
S = num/den
Evaluate SS and simplify. Sometimes using conj works better than abs()^2
SS=(1/2)*(abs(S))^2
SS=(1/sym(2))*S*conj(S)
[num,den] = numden(SS)
SS = num/den
Compute G
G=2*SS;
Compute and simplify H
H = (t^2)*abs((Kt*(Ks+sqrt(-1)*t*Cs))/(((-(t^2)*M+Ks+sqrt(-1)*t*Cs)*(-(t^2)*m+Ks+sqrt(-1)*t*Cs))-((Ks+sqrt(-1)*t*Cs)^2)))
H = simplify(H,100)
Sigpre
sigpre = G/(H^2)
sigpre = simplify(sigpre,10)
symvar(sigpre)
sigpre is a function of only one variable, so it's not necesary to specif the variable of integration to int, but it makes things cleare IMO
sig2 = int(sigpre,t,0,inf)
Integral not found. Sometimes operatingon the integrand can help.
sig2 = int(expand(sigpre),t,0,inf)
Still doesn't look too promising. We can try to get a better expression for sigpre
sigpre = simplify(expand(rewrite(sigpre,'sincos')))
sig2 = int(sigpre,t,0,inf)
Are you sure all the equations are implemented correctly?
%sig=sig2^(1/2)
댓글 수: 0
Torsten
2023년 2월 11일
편집: Torsten
2023년 2월 11일
It seems your integral does not exist.
Note that by default, y is integrated with respect to x. I don't know if this is wanted or not. If not, you will have to sepcify the integration variable:
S=int(y,t,-to/2,to/2);
or
S=int(y,x,-to/2,to/2);
But in case
S=int(y,t,-to/2,to/2);
is correct, values for x in the integral
integral(sigpre,0,Inf)
are missing.
syms Kt Ks Cs M m to
syms x t
Kt=1;
Ks=1;
Cs=1;
M=1;
m=1;
to=1;
y=x*(exp(1))^(-sqrt(-1)*t*x);
S=int(y,-to/2,to/2);
SS=(1/2)*(abs(S))^2;
G=2*SS;
H=(t^2)*abs((Kt*(Ks+sqrt(-1)*t*Cs))/(((-(t^2)*M+Ks+sqrt(-1)*t*Cs)*(-(t^2)*m+Ks+sqrt(-1)*t*Cs))-((Ks+sqrt(-1)*t*Cs)^2)));
sigpre=G/(H^2);
sigpre = matlabFunction(sigpre);
integral(sigpre,0,Inf)
참고 항목
카테고리
Help Center 및 File Exchange에서 Calculus에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!














