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!