least square curve fitting on a nonlinear equation , set of data available

4 ビュー (過去 30 日間)
Mohammad
Mohammad 2023 年 2 月 17 日
編集済み: Star Strider 2023 年 2 月 17 日
Hi all, I want to use least square curve fitting on a nonlinear equation , set of data available , I did the analytics on paper and want to be sure using pure code.
to summrize the method;
we first partial derivative with respect to a and equate it to 0
then partial derivative with respect to b and equate it to 0 as well.
we end up with 2 equations ( nonlinear).
how to solve and find a and b using code? I am new to matlab, any help would be appriciated.
the equation is where y and x are the data set and it goes 27 iterations.
Thanks.

採用された回答

Star Strider
Star Strider 2023 年 2 月 17 日
編集済み: Star Strider 2023 年 2 月 17 日
What you appear to be describing is the derivation of a linear least square regression. In that context, ‘linear’ implies ‘linear in the parameters’, such the partial derivatives of the objective function with respect to each parameter are not functions of that parameter or of any other parameters.
Your data are best determined by fminsearch. There are other options, however those require the Optimization Toolbox, the Statistics and Machine Learning Toolbox or Curve Fitting Toolbox.
Example —
xv = 1:10;
yv = sqrt(xv);
objfcn = @(b,x) b(1).*(1-exp(-b(2).*x));
B0 = rand(2,1);
[B,fval] = fminsearch(@(b)norm(yv-objfcn(b,xv)), B0)
B = 2×1
3.2928 0.2495
fval = 0.3841
figure
plot(xv,yv,'x', 'DisplayName','Data')
hold on
plot(xv, objfcn(B,xv), '-r', 'DisplayName','Regression Fit')
hold off
grid
xlabel('x')
ylabel('y')
legend('Location','best')
text(1,3, sprintf('$y = %.2f\\ (1-e^{-%.2f \\ x})$',B), 'Interpreter','latex')
EDIT — (17 Feb 2023 at 16:58)
Corrected typographical errors.
.

その他の回答 (1 件)

Matt J
Matt J 2023 年 2 月 17 日
編集済み: Matt J 2023 年 2 月 17 日
I would recommend this FEX download,
fun={@(b,x) (1-exp(-b*x))};
[b,a]=fminspleas(fun,b0); %b0 is initial guess of b

カテゴリ

Help Center および File ExchangeGet Started with Curve Fitting Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by