Sum of difference of integrals

3 vues (au cours des 30 derniers jours)
Salwa Ben Mbarek
Salwa Ben Mbarek le 12 Mai 2020
Commenté : Walter Roberson le 8 Juil 2020
Hello,
I've tried to visualise the value of a function F below (that it's the sum of the difference between two integrals, between 10e3 et 30.1e6 ), but the values of the function F when running the programm is "NaN" . Could you please tell me when I am wrong?
The program is below . Thank you enourmously !
%% Sth
z= 76.05*1e-2;
delta=6*1e-3;
c = 3e8;
muo=4*pi*10^-7;
fr= [10e3, 20e3, 50e3, 100e3, 200e3, 500e3, 1e6, 2e6, 5.1e6, 10.1e6, 20.1e6, 30.1e6];
mur = 1;
w=2*pi*fr;
lambda = c./fr;
gamao= 1j*w/c;
s=1;
gama= sqrt(1j.*2.*pi.*fr.*muo.*mur.*s);
too= sqrt(lambda.^2 -gamao.^2);
to= sqrt(lambda.^2- gama.^2);
K= ((to./too+mur).^2-(to./too-mur).^2.*exp(-2.*to.*delta)).^-1;
R = 16.05*1e-2;
J = besselj(1,lambda*R);
L = lambda.*lambda.*((too).^-1).*J.*exp(-too.*z);
G= int(L,sym('lambda'),0,inf);
M = K.*(lambda.^2).*(to).*(too.^-2).*J.*exp(-too*z-(to-too)).* delta;
H= int(K.*(lambda.^2).*(to).*(too.^-2).*J.*exp(-too*z-(to-too)),sym('lambda'),0,inf);
Sth= abs((1/4*mur).*(G./H));
% %% Se
Se = [1, 2, 3, 6, 9 ,10 , 12 ,14 ,18, 22 ,24, 28];
%% F calculation
F= symsum(abs(Se-Sth).^2,f,10e3,30.1e6);

Réponses (1)

Walter Roberson
Walter Roberson le 12 Mai 2020
G= int(L,sym('lambda'),0,inf);
the sym('lambda') there has nothing to do with the lambda in the previous lines, which are numeric lambda.
Your L is numeric, and int() of a constant is the constant times the difference in bounds.
You symsum() with respect to f, but the inputs are an array of numeric values, and you have no variable named f to sum over.
symsum() should typically only be used when you need to generate a formula from the summation, because you have an indefinite bound. If you have a vector of values, then you should sum() the vector instead. If you have definite upper and lower bound and a formula that you are symsum() then typically you should instead subs() lower:upper into the formula and sum() that.
  24 commentaires
Salwa Ben Mbarek
Salwa Ben Mbarek le 7 Juil 2020
Modifié(e) : Walter Roberson le 7 Juil 2020
Hello Sir,
Thank you so much for your help . I understand.
I told vpainintegral to use Reltol , this is the program :
% Sth
z_Ns = 92.18*1e-2;
delta_Ns = 0.8*1e-3;
c_Ns = 3e8;
muo_Ns = 4 .* pi .* 10^-7;
fr_Nv = [10e3, 20e3, 50e3, 100e3, 200e3, 500e3, 1e6, 2e6, 5.1e6, 10.1e6, 20.1e6, 30.1e6];
mur_Ns = 1;
w_Nv = 2 .* pi .* fr_Nv;
%in this section, I chose to put lambda as a sym in calculations, and to
%substitute its value at the end
lambda_Ss = sym('lambda');
gamao_Nv = 1j.*w_Nv./c_Ns;
%s is symbolic
s_Ss = sym('s');
gama_Sv = sqrt(1j .* 2 .* pi .* fr_Nv .* muo_Ns .* mur_Ns) .* sqrt(s_Ss); %uses s
too_Sv = sqrt(lambda_Ss.^2 - gamao_Nv.^2); %does not use s
to_Sv = sqrt(lambda_Ss.^2 - gama_Sv.^2); %uses s
K_Sv = ((to_Sv./too_Sv + mur_Ns).^2 - (to_Sv./too_Sv - mur_Ns).^2 .* exp(-2 .* to_Sv .* delta_Ns)).^-1; %uses s
R_Ns = 16.05*1e-2; %does not use s
J_Sv = besselj(1, lambda_Ss .* R_Ns); %does not use s
L_Sv = lambda_Ss .* lambda_Ss .* ((too_Sv).^-1) .* J_Sv .* exp(-too_Sv .* z_Ns); %does not use s
% L is now symbolic
%now notice that here we are integrating the purely numeric L with respect to symbolic lambda
G_Sv = vpaintegral(L_Sv, lambda_Ss, 0, inf,'RelTol',1e-30,'AbsTol', 0);
G_Nv = double(G_Sv);
M_Sv = K_Sv .* (lambda_Ss.^2) .* (to_Sv) .* (too_Sv.^-2) .* J_Sv .* exp(-too_Sv .* z_Ns - (to_Sv - too_Sv)) .* delta_Ns;
%the integral for H_Sv is going to make it into the final expression. However, matlabFunction cannot work
%with vpaintegral() without falling over, and int() takes a long time to figure out that it is not going to
%be able to find an answer. In the interests of performance, code vpaintegral() here, and we will use
%a hack to swap it out to int() later.
H_Sv = vpaintegral(M_Sv, lambda_Ss, 0, inf, 'RelTol',1e-30,'AbsTol', 0); %this takes a long time to figure out it cannot do anything.
%%lambda_Ss= c_Ns./fr_Nv;
p_Nv = c_Ns ./ fr_Nv;
%N_Sv = arrayfun(@(G,P) subs(G, lambda_Ss, P), G_Sv, lambda_Ss); %Cannot substitute and replace the lambda by o and I don't know why
%%Q_Sv = arrayfun(@(H,P) subs(H, lambda_Ss, P), H_Sv, lambda_Ss);
%{
G_Sv / G_Nv are independent of any variables so no point doing the subs()
N_Sv = arrayfun(@(G,P) subs(G, lambda_Ss, P), G_Nv, p_Nv); %Cannot substitute and replace the lambda by o and I don't know why
%}
N_Nv = G_Nv;
%{
H_Sv is independent of lambda, so there is no point doing the subs()
Q_Sv = arrayfun(@(H,P) subs(H, lambda_Ss, P), H_Sv, p_Nv); %Same substitution problem
%}
Q_Sv = H_Sv;
temp_Sv = (1/(4 .* mur_Ns)) .* (N_Nv ./ Q_Sv);
SEHth_Sv = sqrt(real(temp_Sv).^2 + imag(temp_Sv).^2); %avoid using abs, it is not continuously differentiable
% %% Se
Se_Nv = [12, 19, 21, 26, 29 ,34 , 38 ,39 ,41, 42 ,41, 42];
%{
output_Sv is not used so no point computing it
output_Sv = arrayfun(@(S, SE) subs(S, s_Ss, SE), SEHth_Sv, Se_Nv);
%}
%X= abs((Se_Nv - SEHth_Sv).^2); %abs is not differentiable, use the smooth function instead
%{
SEHth_Sv is already abs() so we do not have to worry about imaginary
%X = (real(Se_Nv - SEHth_Sv)).^2+(imag(Se_Nv - SEHth_Sv)).^2;
%}
X = (Se_Nv - SEHth_Sv).^2;
%{
X is independent of lambda, so no point doing the subs
F=sum(subs(X,lambda_Ss,p_Nv));
%}
F = sum(X);
%%Fint = mapSymType(F, 'vpaintegral', @(V) feval(symengine, 'hold(int)', feval(symengine, 'op', V, 1), 'lambda= 3.0000..0.0010'));
Fint = mapSymType(F, 'vpaintegral', @(V) feval(symengine, 'hold(int)', feval(symengine, 'op', V, 1), 'lambda = 0..infinity'));
%%RelTol',1e-30;
%%% objective harmonization
Fc = matlabFunction(Fint,'Vars',{s_Ss}); %if you send F to file then optimize must be off
gama_fun = matlabFunction(gama_Sv, 'Vars', {s_Ss}, 'File', 'gama_fun.m', 'optimize', true);
obj = Fc;
A=[];
b=[];
% s_Ss0=1e-22;
% lb=1e8;
% ub=1e-22;
s_Ss0=-2;
lb=-inf;
ub=inf;
%ub=1e8;
Aeq = [];
beq = [];
% %nonlcon = @(s_Ss) constrain(s_Ss, fr_Nv, muo_Ns, mur_Ns, gama_fun);
nonlcon= @(s_Ss) constrain(s_Ss);
% optimize with fmincon
%[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN]
%%fmin= fmincon(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON,OPTIONS);
%%fmin = fmincon(obj, s_Ss0, A, b, Aeq, beq, lb, ub, nonlcon);
s_Ss = fmincon(obj,s_Ss0,A,b,Aeq,beq,lb,ub,nonlcon);
% show final objective
disp(['Final Objective: ' num2str(obj(s_Ss))])
Constrain function :
function [c,ceq] = constrain(s_Ss)
c = [];
ceq = [];
end
I still get the value of 1.7427e+07. You're right, the value increases rapidly
obj(100) =3.6222e+94
obj(1000) = 6.9310e+297
Do you think is it logical to have this minimum even when we use Reltol and have -inf and + inf as lower and upper bounds?
Thank you so much !
Walter Roberson
Walter Roberson le 8 Juil 2020
Whether it makes sense or not, with the equations you give, the derivative of the function is nearly linear up to about 2E-8, falling a little less than linear after that. The derivative is approximately 4E10*s for values between 0 and 2E-8. In the tracking I did, the derivative for positive s is always positive.
... And that means that the minimum value of the function is going to be at the lowest point that you evaluate the function at.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by