Integral from 0 to inf with imaginary numbers takes too long

4 Ansichten (letzte 30 Tage)
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?

Akzeptierte Antwort

Star Strider
Star Strider am 11 Feb. 2023
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
Kt = 1
Ks=1
Ks = 1
Cs=1
Cs = 1
M=1
M = 1
m=1
m = 1
to=1
to = 1
y = @(x,t) x.*exp(-sqrt(1i*t*x));
S = @(x) integral(@(t) y(x,t),-to/2,to/2)
S = function_handle with value:
@(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)
sigpre = function_handle with value:
@(x,t)G(x)./(H(x,t).^2)
sig2 = @(t) integral(sigpre(x,t),0,Inf)
sig2 = function_handle with value:
@(t)integral(sigpre(x,t),0,Inf)
sig = sig2(t).^(1/2)
Unrecognized function or variable 't'.
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 Kommentare
BOSHU
BOSHU am 12 Feb. 2023
I realized that I have made a mistake.
As the picture (Written by my classmate after reading the paper), means vertical acceleration and it should be a function fitting to discrete data which should not be monotone functions like , so I changed it into and everything turns out well. I'm just testing my code without getting the data (which we will get later) and I set them all as 1, so I made the mistake.
Thank you very much.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Paul
Paul am 11 Feb. 2023
Bearbeitet: Paul am 11 Feb. 2023
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)
y = 
Force symbolic
y = x*(exp(sym(1)))^(-1i*t*x)
y = 
Or more simply
y = x*exp(-1i*t*x)
y = 
Evaluate S and simplify
symvar(y)
ans = 
symvar(y,1)
ans = 
x
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)
S = 
[num,den] = numden(S)
num = 
den = 
S = num/den
S = 
Evaluate SS and simplify. Sometimes using conj works better than abs()^2
SS=(1/2)*(abs(S))^2
SS = 
SS=(1/sym(2))*S*conj(S)
SS = 
[num,den] = numden(SS)
num = 
den = 
SS = num/den
SS = 
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 = 
H = simplify(H,100)
H = 
Sigpre
sigpre = G/(H^2)
sigpre = 
sigpre = simplify(sigpre,10)
sigpre = 
symvar(sigpre)
ans = 
t
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)
sig2 = 
Integral not found. Sometimes operatingon the integrand can help.
sig2 = int(expand(sigpre),t,0,inf)
sig2 = 
Still doesn't look too promising. We can try to get a better expression for sigpre
sigpre = simplify(expand(rewrite(sigpre,'sincos')))
sigpre = 
sig2 = int(sigpre,t,0,inf)
sig2 = 
Are you sure all the equations are implemented correctly?
%sig=sig2^(1/2)

Torsten
Torsten am 11 Feb. 2023
Bearbeitet: Torsten am 11 Feb. 2023
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)
Warning: Reached the limit on the maximum number of intervals in use. Approximate bound on error is 3.1e+24. The integral may not exist, or it may be difficult to approximate numerically to the requested accuracy.
ans = 3.8771e+25
  1 Kommentar
BOSHU
BOSHU am 11 Feb. 2023
Thank you very much.
In S, I'd like to integrate with respect to x, so it should be right
In integral to sigpre, I'd like to do it with respect to t, I changed your code but it still don't works.

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by