필터 지우기
필터 지우기

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

조회 수: 11 (최근 30일)
umairzulkefli
umairzulkefli 2014년 3월 25일
답변: Praveen Patnaik 2020년 3월 15일
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.

답변 (4개)

Walter Roberson
Walter Roberson 2014년 3월 25일
Use the "\" operator or use polyfit()

Andrei Bobrov
Andrei Bobrov 2014년 3월 25일
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
umairzulkefli
umairzulkefli 2014년 3월 31일
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.

댓글을 달려면 로그인하십시오.


Star Strider
Star Strider 2014년 3월 31일
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
umairzulkefli
umairzulkefli 2014년 4월 2일
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.
Star Strider
Star Strider 2014년 4월 2일
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.

댓글을 달려면 로그인하십시오.


Praveen Patnaik
Praveen Patnaik 2020년 3월 15일
@ 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.

카테고리

Help CenterFile Exchange에서 Least Squares에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by