how to use optimization iterative display

how would i use optimization iterative display to display the value of Ein after each iteration?
clc
close all
clear all
gam = 10e-3; % damping frequency
f = 8.8e-3; % oscillator strength
eb = 11.2; % background epsilon
x = 5.0; %constant
om = 1.5; % frequency array;%frequency array om=1.4:0.001:1.6
syms hw0 k wp Ein er
V = [hw0, k ,wp, Ein, er ];
eq1=hw0 - 1i*((gam+sqrt(-(gam*gam)-(4*k*k)))/(2)); % resonant frequency
eq2=k - sqrt((wp*wp)/(1+2*eb) ); %to find resonant frequency
eq3=wp - sqrt(hw0^2+x*Ein*Ein);%plasma frequency
eq4=Ein - (eb-er)/(er+2*eb);%internal electric field
eq5=er - 1- ( ( f * wp*wp )./ (hw0*hw0-om.*om-1i.*om*gam));%reletive permittivity
eqs = matlabFunction([eq1;eq2;eq3;eq4;eq5], 'Vars', {[hw0 k wp Ein er ].'});
initialguess=[1.5; 2.5 ;1.4; 0.436 - 1i*0.00000112 ; -16.5-1i*1.06 ];
options = optimoptions('fsolve','Display','none','PlotFcn',@optimplotfirstorderopt,'MaxFunctionEvaluations',100);
sol=fsolve(eqs,initialguess,options);
hw0=sol(1);
k=sol(2);
wp=sol(3);
Ein=sol(4);
er=sol(5);

 Respuesta aceptada

Ameer Hamza
Ameer Hamza el 13 de Oct. de 2020
Editada: Ameer Hamza el 13 de Oct. de 2020
This shows how to print value of Ein in each iteration using OutputFcn
clc
close all
clear all
gam = 10e-3; % damping frequency
f = 8.8e-3; % oscillator strength
eb = 11.2; % background epsilon
x = 5.0; %constant
om = 1.5; % frequency array;%frequency array om=1.4:0.001:1.6
syms hw0 k wp Ein er
V = [hw0, k ,wp, Ein, er ];
eq1=hw0 - 1i*((gam+sqrt(-(gam*gam)-(4*k*k)))/(2)); % resonant frequency
eq2=k - sqrt((wp*wp)/(1+2*eb) ); %to find resonant frequency
eq3=wp - sqrt(hw0^2+x*Ein*Ein);%plasma frequency
eq4=Ein - (eb-er)/(er+2*eb);%internal electric field
eq5=er - 1- ( ( f * wp*wp )./ (hw0*hw0-om.*om-1i.*om*gam));%reletive permittivity
eqs = matlabFunction([eq1;eq2;eq3;eq4;eq5], 'Vars', {[hw0 k wp Ein er ].'});
initialguess=[1.5; 2.5 ;1.4; 0.436 - 1i*0.00000112 ; -16.5-1i*1.06 ];
options = optimoptions('fsolve','Display','none', 'OutputFcn', @outFcn, 'PlotFcn',@optimplotfirstorderopt,'MaxFunctionEvaluations',100);
sol=fsolve(eqs,initialguess,options);
hw0=sol(1);
k=sol(2);
wp=sol(3);
Ein=sol(4);
er=sol(5);
function y = outFcn(x, ~, s)
if strcmp(s, 'iter') % only print value when the current iteration finishes
fprintf('Ein=%f\n', x(4));
end
y = 0;
end

8 comentarios

Anushka Deshpandey
Anushka Deshpandey el 13 de Oct. de 2020
also how would i plot Ein against the range 1.4:1.6?
Ameer Hamza
Ameer Hamza el 13 de Oct. de 2020
Can you explain what do you mean by "range 1.4:1.6"?
Anushka Deshpandey
Anushka Deshpandey el 13 de Oct. de 2020
I want for the value of om to be an array from 1.4 to 1.6 instead of the constant 1.5. After that i need to plot Ein vs om
Ameer Hamza
Ameer Hamza el 13 de Oct. de 2020
Editada: Ameer Hamza el 14 de Oct. de 2020
You can write a for loop. Following code is untested, but it gives a general idea
clc
close all
clear all
gam = 10e-3; % damping frequency
f = 8.8e-3; % oscillator strength
eb = 11.2; % background epsilon
x = 5.0; %constant
OM = 1.4:0.01:1.6; % frequency array;%frequency array om=1.4:0.001:1.6
Ein_sol = zeros(size(OM));
for i = 1:numel(OM)
om = OM(i);
syms hw0 k wp Ein er
V = [hw0, k ,wp, Ein, er ];
eq1=hw0 - 1i*((gam+sqrt(-(gam*gam)-(4*k*k)))/(2)); % resonant frequency
eq2=k - sqrt((wp*wp)/(1+2*eb) ); %to find resonant frequency
eq3=wp - sqrt(hw0^2+x*Ein*Ein);%plasma frequency
eq4=Ein - (eb-er)/(er+2*eb);%internal electric field
eq5=er - 1- ( ( f * wp*wp )./ (hw0*hw0-om.*om-1i.*om*gam));%reletive permittivity
eqs = matlabFunction([eq1;eq2;eq3;eq4;eq5], 'Vars', {[hw0 k wp Ein er ].'});
initialguess=[1.5; 2.5 ;1.4; 0.436 - 1i*0.00000112 ; -16.5-1i*1.06 ];
options = optimoptions('fsolve','Display','none', 'OutputFcn', @outFcn, 'PlotFcn',@optimplotfirstorderopt,'MaxFunctionEvaluations',100);
sol=fsolve(eqs,initialguess,options);
hw0_sol=sol(1);
k_sol=sol(2);
wp_sol=sol(3);
Ein_sol(i)=sol(4);
er_sol=sol(5);
end
figure()
axes();
plot(OM, Ein_sol)
function y = outFcn(x, ~, s)
if strcmp(s, 'iter') % only print value when the current iteration finishes
fprintf('Ein=%f\n', x(4));
end
y = 0;
end
Anushka Deshpandey
Anushka Deshpandey el 13 de Oct. de 2020
can i store sym variables in a double array?
Ameer Hamza
Ameer Hamza el 14 de Oct. de 2020
No, they cannot be used in same array. I have fixed the issue with my code. Try the updated code.
Anushka Deshpandey
Anushka Deshpandey el 14 de Oct. de 2020
thanks a lot!!it worked
Ameer Hamza
Ameer Hamza el 14 de Oct. de 2020
I am glad to be of help!

Iniciar sesión para comentar.

Más respuestas (1)

Mario Malic
Mario Malic el 13 de Oct. de 2020
Editada: Mario Malic el 13 de Oct. de 2020

0 votos

Edit: Display does not return values of variables in iterations, my bad. Ameer's answer should get you what you wanted.
Go to documentations on fsolve, see the left section and find Input Arguments -> Options, find Display option and set it appropriately.

Preguntada:

el 13 de Oct. de 2020

Comentada:

el 14 de Oct. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by