How to minimize a function with a summation of a set of data?
8 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
回答 (4 件)
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
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 件のコメント
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
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.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!