How to use least square fit in matlab to find coefficients of a function?

8 views (last 30 days)
consider the following 10 data points
x = 7.38, 5.86, 2.46, 6.66, 0.83, 6.26, 6.61, 7.29, 8.91, 9.82
y = 11.89, 2.01, 4.54, 7.26, 1.61, 3.99, 7.16, 11.17, 10.44, 1.97
the two variables x and y have a relationship of the following form:
y(x) = c1x + c2x^(2/3) + c3xsin(x)
how can i apply the least square fit algorithm for a linear combination of functions to find the optimal parameters c1,c2,and c3?
Also, i need to plot the data points and the cit curve for 0<x<10
any idea? Can't figure this one out
  1 Comment
Thalia Putri
Thalia Putri on 4 Apr 2018
how if i have 3 variables? for example x1, x2 and y. the equation is y = c1 x1^2 x2 + c2 x1 x2 +c3 x1 x2^2 how can i get the coefficient c1 c2 c3 ?? please help me. thankyou

Sign in to comment.

Accepted Answer

dpb
dpb on 30 Apr 2016
Edited: dpb on 1 May 2016
Matlab to the rescue!!! :) One of the most usefulest features, indeed!
>> x = [7.38, 5.86, 2.46, 6.66, 0.83, 6.26, 6.61, 7.29, 8.91, 9.82];
y = [11.89, 2.01, 4.54, 7.26, 1.61, 3.99, 7.16, 11.17, 10.44, 1.97];
>> z=x.^2/3; w=x.*sin(x); % define transformed variables in x
>> A=[x.' z.' w.']; % build system matrix
>> b=A\y.' % solve the system
b =
1.0146
-0.1297
1.0349
>> plot(x,y,'*')
>> xh=[1:10] ; % define the desired output points
>> zh=[xh; xh.^2/3; xh.*sin(xh)]; % and the transformed variable array from them
>> yh=b.'*zh % evaluate the fit at those points
yh =
1.8423 3.7385 3.0929 0.2338 -0.9699 2.7963 9.7436 13.5416 9.4685 0.1927
>> hold on
>> plot(xh,yh,'b-')
>>
Actually, somewhat to my surprise after the first plot, the results aren't too bad...
ADDENDUM After the transformation, can use any of the curve fitting tools that solve the OLS problem; specifically depending on which Toolboxen you have installed, but the above is in base product and the "left divide" operator is worth the price of Matlab alone at times like this...and was particularly so before there were other alternatives readily available without "roll you own".
  6 Comments
dpb
dpb on 4 Apr 2018
That's
y = (c1 x1^2 + c2 x1 +c3 x1 x2)*x2
In Wilkinson notation that fitlm uses, that can be written as
y ~ (A + A:B + A^2) * B
Since all the Stat Toolbox functions incorporate the intercept term by default, you'll have to explicitly remove it--
y ~ (A + A:B + A^2) * B - 1
I believe should estimate the model altho I've not used the toolset much; I was so ingrained in using the backslash operator with explicitly-formed models I've just not gotten practice since TMW introduced the later tools.
pooja sudha
pooja sudha on 9 Oct 2020
what if we don't have a linear combination of terms?
or if we have a function which includes constant in them and we can't seperate them & write them in a linear combination.

Sign in to comment.

More Answers (0)

Categories

Find more on Fit Postprocessing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!