Info

此问题已关闭。 请重新打开它进行编辑或回答。

Unable to find a minima for a symbolic integral

5 次查看(过去 30 天)
Hello! I have the following code. Actually, I am looking to maximize w.r.t. x1 but since it appears with a negative sign it boils down to minima.
n=35;
T=15;
g=.111;
K=1/g;
d=2;
rbar=0.04;
mubar=0;
R=[1, 0; 0, 0];
M=[0, 0; 0, 1];
X012=0.002;
X0=[0.01, X012; X012, 0.001];
H=[-0.5, 0.4; 0.007, -0.008];
Q=[0.06 -0.0006; -0.06, 0.006];
Scheck7=0;
beta=3;
SIGMA=[0.006811791307233, -0.000407806990090; -0.000407806990090, 0.00039291243623];
PSICURL=[0.011526035149236, 0.758273970303934; 0.013935191202735, 0.955423605940771];
S0i=zeros(n,1);
S0i2=zeros(n,1);
Yn0=0;
Yi=zeros(n,1);
S1MC=zeros(n,1);
POW=zeros(n,1);
SPOW=0;
SPHI=0;
SPSI=0;
delta=0.75;
PSIi = cell(1, n);
PHIi=zeros(n,1);
% Prameters for the symbolic sum
syms Gamma1 ik
for i=2:n;
APHNEW=expm([(i-1).*H,(i-1).*(2*(Q'*Q));(i-1).*(R+M),(i-1).*(-(H'))]);
APHNEW11=APHNEW(1:2,1:2); %That is good
APHNEW21=APHNEW(3:4,1:2);
APHNEW12=APHNEW(1:2,3:4);
APHNEW22=APHNEW(3:4,3:4);
% Computation of PSIi and PHIi
BPHNEW22=inv(APHNEW22);
PSIi{i}=APHNEW22\APHNEW21;
M7=PSIi{i};
SPSI=SPSI+PSIi{i};
PHIi(i)=beta*(log(det(APHNEW22))+(i-1)*trace(H'))/2;
S0i(i)=exp(-((rbar+mubar)*(i-1)+PHIi(i)));
S0i2(i)=(-((rbar+mubar)*(i-1)+PHIi(i)));
Yn0=Yn0+S0i2(i);
end
SIGMAi=inv(SIGMA);
THETA1=SIGMAi*(PSICURL'*X0*PSICURL);
% i stands for iota
syms x1
fun0=@(Gamma) exp(trace(1i*(THETA1*inv(eye(2)-2i.*SIGMA*Gamma)*SIGMA*Gamma)))/((det(eye(2)-2i.*SIGMA*Gamma))^(beta/2));
%fun3 = matlabFunction((exp((1i*Gamma1+delta)*Yn0)*(symsum(S0i(ik)*fun0(1i*PSIi{ik}-(Gamma1-1i*delta)*SPSI), ik, 2, n)-(K-1)*fun0(-(Gamma1-1i*delta)*SPSI)))/(1i+Gamma1));
fun3a = arrayfun(@(idx) S0i(idx) * fun0(1i*PSIi{idx}-(Gamma1-1i*delta).*SPSI), 2:n, 'Uniform', 0);
sum3a = sum([fun3a{:}]);
fun3_formula = ((exp(-((1i*Gamma1)*x1)))*exp((1i*Gamma1+delta)*Yn0)*(sum3a-(K-1)*fun0(-(Gamma1-1i*delta).*SPSI)))/(1i+Gamma1);
a = vpa( subs(fun3_formula, Gamma1, 1) )
%q = quadgk(fun3,0,Inf)
q_formula = int(fun3_formula, Gamma1, 0, inf);
q = matlabFunction(q_formula, 'vars', x1);
%q1= exp(-(delta*x1))*max(q/pi, 0)
q1 = @(x1_x1) max(exp(-(delta * x1_x1)) * q(x1_x1)/pi, 0);
%x = fminbnd(q1,-Inf,+Inf)
%initial_guess = rand;
%x_theta1 = fminsearch(q1, initial_guess);
%x = fminbnd(q1,-Inf,+Inf)
[x,fval]=fminbnd(q1,-Inf,+Inf)
My aim is to minimize q1 w.r.t. x1. However MATLAB does not give me any answer.
Can anyone please help?
Thanks.

回答(2 个)

Walter Roberson
Walter Roberson 2017-5-20
After adjusting your code to allow computations to proceed, I find that your q1 values are mostly complex. However, there are values at which the imaginary part vanishes; there appear to be a series of such points towards positive x1 values, with real magnitude growing smaller and smaller (damping.) The negative x1 at which the imaginary components vanish are approximately
-192.141514316517 -188.655787231716 -174.349836830908 -164.117505872502 -159.453121627949 -155.267512530917 -148.325029411257 -134.466528151559 -128.757615075388 -120.467541617748 -113.873014115027 -112.710435738364 -102.681758746963 -89.6122555789685 -80.9922253336114 -75.3528553255843 -69.9030673982657 -66.9348134318309 -65.0450650466323 -62.8009792161248 -59.9236072335582 -57.4428794570977 -55.2754416634021 -53.3054688314303 -51.3143402109365 -49.3375806679832 -47.326990485515 -46.5858590341201 -30.1886651033708 -29.0337559483385 -27.498965552452 -25.8680259013049 -24.0692753628916 -22.0290261987157 -19.9586548105586 -17.9076174690997 -15.3127142047397 -12.4586653906846 -11.0638473820923 -8.23092022558657 -3.48183500381984
Note: to 16 digits of precision, some of those locations have a significant imaginary component. That is because the function is so steep at those points that the adjacent representable value has an imaginary component of opposite sign and larger magnitude: the above are the closest representations in binary floating point.
In order to minimize the function, we must select the minimum value that it achieves at the points where the imaginary component vanishes. The function is effectively not continuous for your purposes, so you have to find the points where it exists and then take the minimum among those points. The real components corresponding to the above locations are
-1.54114175976339e+48 5.66125848286782e+46 -4.43941266035521e+42 9.32268918449569e+38 -3.44960496385963e+37 1.19438990077474e+36 -1.10285019640586e+34 2.79224578549348e+29 -5.3872782404782e+26 1.31888812894348e+25 -1.6629378087883e+22 5.44482392809658e+21 -1.00225388086346e+19 747319448309669 -1414346152497.49 13447342311.9391 -56267167.2873899 9465813.2535106 543870.090637689 352535.261711927 20688.0050475019 7617.06546618484 881.274400895307 423.751022552601 78.9506814740713 27.408464913681 7.01669839359559 4.83341055220347 -1.57164698806938e-05 -5.56618491318963e-06 -1.6584846542248e-06 -3.51523264324361e-07 -1.07468533983336e-07 -1.28720508948511e-08 -4.62935878887861e-09 -3.23320760267986e-10 -1.17979676353903e-10 -6.2872110686137e-12 -3.62149260857601e-12 4.92853608204376e-13 -4.59671900947997e-14
You can observe the dampening that I mentioned earlier. The minimum of those is the first of them, -1.54114175976339e+48, at x1 = -192.141514316517
As you indicated earlier that you are expecting your q values to be real, I would tend to suspect that you still have an error in how you set up your computations. Or, possibly your expectation that the q values are real is mistaken.
  12 个评论
Walter Roberson
Walter Roberson 2017-6-18
I noticed by the way that you had
symfum( real( simplify(EXPRESSION) ) )
and thought to myself, "Ah, it would make more sense to simplify() the real() instead of real() of the simplify() in order to improve the code execution.
Unfortunately that did not work out at all! simplify(real(EXPRESSION)) was taking so long that I ended up having to kill the mupad kernel and kill the MATLAB process itself :(
Walter Roberson
Walter Roberson 2017-6-19
In your fun3_formula, part of the expression is
exp(-((1i*Gamma1)*x1)
Here Gamma1 >= 0 (because the later integral is over 0 to inf), and x1 is real valued (because you later fminbnd over -inf to +inf).
There is a problem here: this is not defined as x1 approaches +/- infinity. If you rewrite it in terms of sin and cos you end up with sin(infinity) and cos(infinity) terms, and those are not defined.
This is your only term involving x1 in fun3_formula, and as x1 grows larger in magnitude (towards +/- infinity) any uncertainty in Gamma1 becomes more and more important, to the point where the value of the exp() term becomes something random-ish in the range +/- 1 according to the noise in the value of Gamma1 and the round-off error of the calculation. My tests suggest that below x = -32 the oscillations this drives the integral to are too large to be compensated for and vpaintegral and integral will fail.

John D'Errico
John D'Errico 2017-5-20
fminbnd and fminsearch do NOT apply to symbolic functions. Nor does quadgk.
ALL of those tools are NUMERICAL tools. They do NOT operate on functions with symbolic variables in them.
Sorry, but I won't even try to wander through this mess of code, as you are doing many confusing things, half of which are commented out.
If you want to do integration, then use int for symbolic expressions.
If you want to find a minimum, then go back to basic calculus. Differentiate, set the result equal to zero, and solve. Decide which solution from that process results in a MINIMUM.

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by