lsqcurvefit can't get to the right values
2 次查看(过去 30 天)
显示 更早的评论
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)
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 个评论
Sam Chak
2024-4-7
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.
回答(1 个)
Torsten
2024-4-7
编辑:Torsten
2024-4-7
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 个评论
Torsten
2024-4-8
编辑:Torsten
2024-4-8
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".
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!