How to minimize a function with a summation of a set of data?

Hello
Sorry if my english is not so good.
I'm am trying to calculate the slope and the intercept of a set of data using the Least Squares Method. By that, I need to minimize the sum of squares of the error between the y-data and the y-model (ym) data.
Here will be my set of data.
x = [0 1 2 3 4 5 6 7 8 9 10];
y = [0.4505 1.0838 2.2290 3.9133 4.1524 5.8258 6.5383 7.9961 8.07820 9.4427 10.1067];
I am going to minimize the sum of ((y-ym)^2) where ym=a+bx.
'a' will be the intersection and 'b' will be the slope.
How I am going to do that?
Thank You.

Answers (4)

x = [0 1 2 3 4 5 6 7 8 9 10];
y = [0.4505 1.0838 2.2290 3.9133 4.1524 5.8258 6.5383 7.9961 8.07820 9.4427 10.1067];
c1 = y(:)\[x(:),ones(size(x(:)))]
c2 = polyfit(x,y,1)
xx = (0:.1:10);
plot(x,y,'go',xx,polyval(c1,xx),'r-',xx,polyval(c2,xx),'b-')
please use polyfit

1 Comment

Thank you for the respond.
But is there any step to do it using the minimizing tools under the optimizer?
Because I will be using the steps to calculate for the non-linear problems.
Thank you.

Sign in to comment.

Knowing the details you provided to Andrei Bobrov, I suggest:
x = [0 1 2 3 4 5 6 7 8 9 10];
y = [0.4505 1.0838 2.2290 3.9133 4.1524 5.8258 6.5383 7.9961 8.07820 9.4427 10.1067];
fcn = @(b,x) b(1).*x + b(2); % Function to fit to data
OLS = @(b) sum((fcn(b,x) - y).^2); % Ordinary Least Squares cost function
opts = optimset('MaxFunEvals',50000, 'MaxIter',10000);
B = fminsearch(OLS, rand(2,1), opts); % Use ‘fminsearch’ to minimise the ‘OLS’ function
fcnfit = fcn(B,x); % Calculate function with estimated parameters
figure(1)
plot(x, fcnfit, '-r')
hold on
plot(x, y, '+b')
hold off
grid
produces:
B =
998.3171e-003
446.3164e-003
and a plot of the fitted equation to your data. To do a nonlinear fit, simply change the equation in the fcn anonymous function statement.

2 Comments

Thank you Star Strider
I think this is the solution i will be needing to.
Just, can you explain a little on this 2 lines (the meaning of the coding:
opts = optimset('MaxFunEvals',50000, 'MaxIter',10000);
B = fminsearch(OLS, rand(2,1), opts);
Thank you again.
The fminsearch function (and many others) have ‘default’ settings for their behaviour that work in most situations. The ‘options’ structures that are available allow the user to override some or all of those settings with settings that might be more appropriate to a particular problem.
opts = optimset('MaxFunEvals',50000, 'MaxIter',10000);
The optimset statement sets these for fminsearch, increasing the maximum number of function evaluations and the maximum number of iterations so that fminsearch will likely arrive at a solution before it reaches either of those limits. This helps in more difficult regression problems.
B = fminsearch(OLS, rand(2,1), opts);
The fminsearch function does not do curve fitting on its own. I created the OLS function to calculate the sum-of-squares between your data and the results your function returns, and fminsearch minimises it. The rand call sets the initial guess of your parameters to a (2x1) vector of random numbers. The opts argument passes the opts structure to fminsearch. The fitted parameters are returned in the B vector.

Sign in to comment.

@ Star strider
Can you look into a similar problem???
Here I have to take square of distance between two curves and based on that we get a new optimised position of arc.

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 25 Mar 2014

Answered:

on 15 Mar 2020

Community Treasure Hunt

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

Start Hunting!