MATLAB Answers

Using fminsearch with a function containing summation (via for loop)

11 views (last 30 days)
Dear MATLAB community,
I would like to obtain the values of , γ, δ, and L in the following equations:
To obtain these values, I am optimizing the error function :
where is the experimental result.
The strategy that I would like to implement is:
(1) Generate a function that calculates both the and
(2) Use that function to optimize the values of , γ, δ, and L using fminsearch (since I want to use Nelder-Mead optimization).
The script that I have written for the function is:
function [f] = ErrorFunction(x)
% n_ave = x(1)
% gamma = x(2)
% delta = x(3)
% length = x(4)
c = 299792458; % Speed of light in m/s
n_air = 1.000293; % Index of refraction of air
k = 1e-12; % ps to s coversion
cor_fac = 0.04;
M = 4;
fp_signal = 0.0; %fp_signal is E_FP(t)
for k=1:M
fp_signal = fp_signal +...
((x(1)-1)/(x(1)+1))^(2*k).*...
x(2)^(2*k).*...
raw_signal_ref.*k... % raw_signal_ref is E_reference(t)
(raw_time_ref.*(1e-12)-(x(1)*2*x(4)*M/c)-((x(1)-n_air)*x(4)/c))/...
x(3)^(2*M+1);
end
signal_simulated = cor_factor*(4*x(1))/(x(1)+1)^2*x(2)*...
((raw_signal_ref.*k.*((raw_time_ref*(1e-12)-(x(1)-1)*(x(4)*(1e-3)/c))/x(3))) + fp_signal); % raw_time_ref = t
f = sum(sim_signal_sam - raw_signal_sam); %raw_signal_sam is E_sample_(t)
To find , γ, δ, and L, I use fminsearch using the following separate script:
%% Optimization of paramters: n_ave , gamma, delta, length
x0 = [n_ave_in;gamma_in;delta_in;thickness_in];
x_optimized = fminsearch('ErrorFunction',x0);
The initial values for these parameters are , , , and .
When I run both scripts, however, I am getting this result:
Questions now are:
  1. Should I use a different approach on implementing the summation (via for loop) for the FP effect, i.e. using symsum?
  2. Can optimization using fminsearch be even done if a summation is inside the equation?
  3. What should be changed in the function script for fminsearch to work?
Thanks in advanced for your help.

Accepted Answer

Alan Weiss
Alan Weiss on 21 Apr 2021
Youu have several typos in the problem. I corrected them as best I could, but please make sure that I didn't change anything that you meant to write.
Your main problem is not passing the extra data for the problem. Here is my version, with typos corrected as best I could and with data passed correctly:
function [f] = ErrorFunction(x,raw_signal_ref,raw_signal_sam,raw_time_ref)
% n_ave = x(1)
% gamma = x(2)
% delta = x(3)
% length = x(4)
c = 299792458; % Speed of light in m/s
n_air = 1.000293; % Index of refraction of air
k = 1e-12; % ps to s coversion
cor_fac = 0.04;
M = 4;
fp_signal = 0.0; %fp_signal is E_FP(t)
for k=1:M
fp_signal = fp_signal +...
((x(1)-1)/(x(1)+1))^(2*k)*...
x(2)^(2*k)*...
raw_signal_ref*k.*... % raw_signal_ref is E_reference(t)
(raw_time_ref*(1e-12)-(x(1)*2*x(4)*M/c)-((x(1)-n_air)*x(4)/c))/...
x(3)^(2*M+1);
end
sim_signal_sam = cor_fac*(4*x(1))/(x(1)+1)^2*x(2)*...
((raw_signal_ref*k.*((raw_time_ref*(1e-12)-(x(1)-1)*(x(4)*(1e-3)/c))/x(3))) + fp_signal); % raw_time_ref = t
f = sum(sim_signal_sam - raw_signal_sam); %raw_signal_sam is E_sample_(t)
Here is how I called that function after placing x0 in the workspace:
fun = @(x)ErrorFunction(x,raw_signal_ref,raw_signal_sam,raw_time_ref);
x_optimized = fminsearch(fun,x0);
To learn about passing extra parameters this way, see Passing Extra Parameters.
When I run the problem fminsearch exits with this message:
Exiting: Maximum number of function evaluations has been exceeded
- increase MaxFunEvals option.
Current function value: -907777101105805751482258996129483335894703616332083863052416498711493108878399830640514887677380554503199361365622832602682026028412939674686787717791027291595548412048643393197842340492625295522296145364560679359874843053719749781643093886750418805308954509312.000000
So I am not sure that my corrections were complete or correct, but at least it now runs.
Alan Weiss
MATLAB mathematical toolbox documentation
  2 Comments
Mark Justine Zapanta
Mark Justine Zapanta on 22 Apr 2021
Dear Dr. Weiss,
Thanks for the help! The script now works after your suggested improvements. Indeed, better result is obtained when the error is squared.
Kind regards,
Mark

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!