Nonlinear least square minimization using 2 variables
4 次查看(过去 30 天)
显示 更早的评论
Hello,
I am trying to fit computed data (dIdV) with experimental data (STSavg). I would like write a function where A and B is varied such that the sum of the squares of the function (f) is minimized. Ideally, I would be able to see the minimum value and also the values for A and B.
f = ((STSavg - B)/ A - 10^10*dIdV)^2
%STSavg is a 300x1 array. (Values ~ 10^-1)
%dIdV is also a 300x1 array. (Values ~ 10^-9)
%dIdV by a factor of 10^10, to have comparable magnitude as STSavg
I have written this in Mathematica, but would like to convert it to Matlab. Here is the Mathematica script, which may provide an idea of what my goal is:
1. Minimize X^2 between STS and dI/dV, assuming linear transformation.
2. Cut off points near edge, as they may be noisy.
cutpts = 10;
OM = FindMinimum[Total[((STSavg2[[cutpts ;; TotPts - cutpts]] - B)/A - 10^10 didv[[cutpts ;; TotPts - cutpts]])^2],{{A, 0.1}, {B, 1}}]
OFFSET = B /. OM[[2]] , MAGNITUDE = 10^10 A /. OM[[2]]
Output: {639.834, {A -> 0.0389278, B -> -0.174553}}
Output: OFFSET = -0.174553
Output: MAGNITUDE = 3.89278 * 10^8
I've read about "fminsearch" and using anonymous functions, but I'm slightly confused as to what the input arguments should be when creating the function for two variables (A and B).
Thanks, Chris
0 个评论
采纳的回答
Matt J
2018-2-5
Since it's a linear transformation, it is as simple as
sol=[10^10*dIdV, ones(300,1)]\STSavg;
A=sol(1); B=sol(2);
3 个评论
Matt J
2018-2-5
编辑:Matt J
2018-2-5
The least squares problem you have posed can be solved analytically. It doesn't require fminsearch or any other iterative solver. What I posted for you above constitutes a complete solution.
The solution I posted for you actually minimizes
f = ( A* 10^10*dIdV+B - STSavg )^2
This is slightly different from your original formulation, but better in my opinion. It avoids error-in-variable issues by applying the linear transformation to the theoretical data instead of the empirical data.
If you really insist on your original formulation, you can also do that analytically as follows,
sol=[STSavg, ones(300,1)]\(10^10*dIdV);
A=1./sol(1); B=-sol(2)*A;
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear Least Squares 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!