lsqcurvefit can't get to the right values

81 views (last 30 days)
Eli
Eli on 7 Apr 2024 at 8:29
Edited: Torsten on 8 Apr 2024 at 21:19
Hi
I'm trying to run my code but for some reason it's not trying to converge. the numbers don't change at all
What can be the cause of it? and how do i fix it?
clear
clc
x0 = [75,775 0 0 290 788];
lb = [0 750 -2 -2 280 780];
ub = [90 800 2 2 300 800];
needed_results = zeros(1,5);
harminics_positions_pix = [596, 548, 510, 480, 457];
x = lsqcurvefit(@simulation_grating, x0, harminics_positions_pix,needed_results,lb,ub)
epsilon_equation = 1x5
1.0e-03 * -0.663922364844540 -0.504252608292091 -0.386921645947969 -0.298242547108015 -0.495377384240422
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Warning: Trust-region-reflective algorithm requires at least as many equations as variables; using Levenberg-Marquardt algorithm instead.
epsilon_equation = 1x5
1.0e-03 * -0.663922432249899 -0.504252662749099 -0.386921690925949 -0.298242584926721 -0.495377436938948
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
epsilon_equation = 1x5
1.0e-03 * -0.663922381844982 -0.504252621722722 -0.386921656843434 -0.298242556135210 -0.495377384240422
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
epsilon_equation = 1x5
1.0e-03 * -0.663922365618754 -0.504252608893471 -0.386921646426198 -0.298242547495274 -0.495377384819959
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
epsilon_equation = 1x5
1.0e-03 * -0.663922365262067 -0.504252608623340 -0.386921646222971 -0.298242547344451 -0.495377384554310
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
epsilon_equation = 1x5
1.0e-03 * -0.663922364469610 -0.504252607754201 -0.386921645197090 -0.298242546143862 -0.495377383922764
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
epsilon_equation = 1x5
1.0e-03 * -0.663922364083193 -0.504252608341175 -0.386921646815808 -0.298242548719612 -0.495377383765274
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
epsilon_equation = 1x5
1.0e-03 * -0.663921043265454 -0.504251557941020 -0.386920789638927 -0.298241834660169 -0.495376371941583
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
epsilon_equation = 1x5
1.0e-03 * -0.663921110670646 -0.504251612397876 -0.386920834616810 -0.298241872478751 -0.495376424639943
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
epsilon_equation = 1x5
1.0e-03 * -0.663921060265869 -0.504251571371610 -0.386920800534378 -0.298241843687337 -0.495376371941583
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
epsilon_equation = 1x5
1.0e-03 * -0.663921042491253 -0.504251557339612 -0.386920789160712 -0.298241834272896 -0.495376371362047
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
epsilon_equation = 1x5
1.0e-03 * -0.663921042847940 -0.504251557609743 -0.386920789363967 -0.298241834423705 -0.495376371627696
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
epsilon_equation = 1x5
1.0e-03 * -0.663921042890496 -0.504251557403130 -0.386920788888062 -0.298241833696044 -0.495376371623897
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
epsilon_equation = 1x5
1.0e-03 * -0.663921042504107 -0.504251557990118 -0.386920790506780 -0.298241836271794 -0.495376371466449
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Local minimum possible. lsqcurvefit stopped because the relative size of the current step is less than the value of the step size tolerance.
x = 1x6
0.749999886362764 7.749999997838026 -0.000000094280308 -0.000000052120071 2.900000000290558 7.879999999991456
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
function [epsilon_equation] = simulation_grating(x0,harminics_positions_pix)
format long
theta_i = x0(1);
lamda0 = x0(2)*1e-9;
diff_dist1 = x0(3)*1e-2;
diff_dist2 = x0(4)*1e-2;
pix_start = x0(5);
pix_end = x0(6);
d= 1e-3/600;
dia_mcp = 27e-3;
pixels_dia = pix_end-pix_start+1;
pos_middle = (pix_end+pix_start)/2;
pix_size=dia_mcp/pixels_dia;
%theta_i = 75; %deg
const_sin_i = sind(theta_i);
%lamda0=775e-9; %nm
theta_out=asind(const_sin_i-lamda0/d);
harmonics = [13,15,17,19];
harmonics_position = (pos_middle-harminics_positions_pix).*pix_size;
theta_out = asind(const_sin_i-lamda0/d./harmonics);
I_p_helium = 24.6; %eV
lamda_Ip=1240*1e-9/I_p_helium;
pix_helium = 545;
theta_out = [theta_out asind(const_sin_i-lamda_Ip/d)];
harmonics_position = [harmonics_position (pos_middle-545).*pix_size];
dist1= 20e-2+diff_dist1;
dist2 = 10.5e-2+diff_dist2;
L_harmonic = sqrt(dist1^2+(dist2-harmonics_position).^2);
L_harmonic_13 = sqrt(dist1^2+(dist2-harmonics_position(1)).^2);
diff_from_13 = harmonics_position - harmonics_position(1);
L_harmonic = L_harmonic(2:end);
diff_from_13 = diff_from_13(2:end);
epsilon_equation = diff_from_13.^2 - (L_harmonic.^2 + L_harmonic_13.^2 - 2.*L_harmonic.*L_harmonic_13.*cosd(theta_out - theta_i))
end
  1 Comment
Sam Chak
Sam Chak on 7 Apr 2024 at 9:41
Use "comments" to annotate the equation number, variable names, type (vector or matrix) and generally what the code does.
Try avoiding using "long" variable names to improve the readability of the code, which normally speeds up the efficiency when troubleshooting the code.

Sign in to comment.

Answers (1)

Torsten
Torsten on 7 Apr 2024 at 9:49
Edited: Torsten on 7 Apr 2024 at 9:50
It seems that a variation of your parameters has almost no effect on epsilon_equation. This makes lsqcurvefit assume it cannot make progress from your initial values. Maybe you could use another unit for epsilon_equation that multiplies it's current values by 1e6 up to 1e9, e.g.
  3 Comments
Eli
Eli on 8 Apr 2024 at 20:38
what kind of other units? I assume you don't meand just multiplying the epsilon by 10^9 in the function result - > i tried it and it doesn't converge to where i expect it to.
Maybe you can take a look at the error message?
Warning: Trust-region-reflective algorithm requires at least as many
equations as variables; using Levenberg-Marquardt algorithm instead.
> In lsqncommon (line 94)
In lsqcurvefit (line 306)
In grating_spectrum (line 9)
Local minimum found.
Optimization completed because the size of the gradient is less than
1e-4 times the value of the function tolerance.
<stopping criteria details>
Torsten
Torsten on 8 Apr 2024 at 21:17
Edited: Torsten on 8 Apr 2024 at 21:19
It's not an error message - it tells you that lsqcurvefit succeeded.
But you should ask yourself whether it makes sense to fit 7 parameters if you only have 6 equations to fix them. Usually this means that you have one degree of freedom and that your problem has an infinite number of solutions. It means further that the parameters you get will most probably have no physical meaning - in accordance with your observation that " it doesn't converge to where i expect it to".

Sign in to comment.

Categories

Find more on Systems of Nonlinear Equations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!