fitting a function of three output

2 views (last 30 days)
Nadav Wetzler
Nadav Wetzler on 21 Oct 2021
Answered: MULI on 25 Apr 2024
Hi everyone!
I need you advise on fitting coefficientts of a function that output 3 valus:
E = calcE(N, Ds, Dd, k), where E is a vector: E(1), E(2), E(3)
Ds, Dd and k are know. I need to find the set of N, where N is a vector N(1),N(2),N(3), that result E1,2,3 that is equal to a given e: e(1), e(2), e(3).
I tied:
function N1 = calcN(Ds, Dd, k, e)
b0 = [1, 1, 1];
fun = @(N)calcE(N,Ds, Dd,k)./e;
N1 = lsqcurvefit(fun,b0, N, e);
end
tryig to force E ./ e =1
But did't work...
with calcE
function E = calcE(N,Ds, Dd,k)
S(1) = -sqrt((1-N(1)^2-N(2)^2) / (1-N(2)^2+(N(1)^2)*((((k^2) * (1-N(1)^2))/N(2)^2)+2*k)));
S(2) = (S(1)*N(1)*k/N(2));
S(3) = -sqrt(1-S(1)^2 - S(2)^2);
E(1) = sqrt((N(1)*Dd(1))^2 + (S(1)*Ds(1))^2);
E(2) = sign(k)*sqrt((N(2)*Dd(2))^2 + (S(2)*Ds(2))^2);
E(3) = sqrt((N(3)*Ds(3))^2 + (S(3)*Dd(3))^2);
end
Thanks!
Nadav

Answers (1)

MULI
MULI on 25 Apr 2024
Hi Nadav,
I understand you are trying to get optimized values of (N) which are used to compute vector (E) that should match with target vector (e).
The function "lsqcurvefit is designed to minimize the difference between function's outputs and target values. It is typically more effective to minimize the absolute or squared differences than trying to make each ratio equal to 1.
Below is the modified code which you can try:
function N_opt = calcN(Ds, Dd, k, e)
b0 = [1, 1, 1]; % Initial guess for the N vector
% Objective function: modeled E based on N, Ds, Dd, k
fun = @(N, xdata) calcE(N, Ds, Dd, k); % x is not used in calcE but is required by lsqcurvefit syntax
% Use lsqcurvefit to find the N that minimizes the difference between calcE's output and e
N_opt = lsqcurvefit(fun, b0, [], e); % xdata is set to [] since it's not used
end
You may refer to this documentation for more information and examples related to “lsqcurvefit”.
Hope this answers your query!

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!