Estimating two coefficients out of three in quadratic function

Hi Everybody, I need to estimate coefficients of this equation aN^2 + bN. It is standard quadratic function but coefficient c=0. My data is a time series, where:
x= [ 0, 5 ,9, 10,15, 16]
y= [715,901, 1139, 1224,1553,1712]
F=polyfit(x,y,3)
plot(F)
As you can see I am using polyfit, which gives me 3 coefficients: a=1.9029 b=30.1841 c=712.7646. I need c to be 0. Do you know any way to calculate only a and b, setting c as 0? I will much appreciate your help, as i am stuck a bit. Thank you :)

 Accepted Answer

Try this:
x= [ 0, 5 ,9, 10,15, 16];
y= [715,901, 1139, 1224,1553,1712];
F = [x(:).^2 x(:)]\y(:);
eqnstr = sprintf('F(x) = %.2f\\cdotx^2 + %.2f\\cdotx', F)
xv = linspace(min(x), max(x));
figure(1)
plot(x, y, 'pg')
hold on
plot(xv, polyval([F(:)' 0],xv), '-r')
hold off
grid
text(3, 1300, eqnstr)

10 Comments

Thank you very very much for this quick response. To be sure, you used least square method? If I type below part
polyval([F(:)' 0]
would I get values of fitted curve in my points x ? Sorry again for my lack of knowledg....
As always, my pleasure.
‘To be sure, you used least square method?’
Yes. See the documentation on mldivide,\ (link) for a full explanation.
‘... would I get values of fitted curve in my points x ?’
Yes. I did that as a ‘shortcut’ rather than calculate the function fit from the matrix equation.
:) I calculated it and I got huge numbers like e.g a*715*715 +b*715 , its almost 12,5 mln I guess I have put x and y wrong in time 0 the number of population is 715 the equation is a*715^2 + b*715 and this goes for my 6 values. After estimating my coefficients I should get number in 0 time kinda similar to 715. I have tried to change x with y in code but plot axes are crazy. Do you think this code needs a bit of modification? the whole point of this equation is to estimate nr of population in time. When I calculate a*x*x+b*x it make more sense in number e.g for 1712 i get 1640,48 . But in 0 time when my value is 715, estimation will be 0 :/
I have no idea what your data are. If they are matrices, to use my code, you would have to use a loop to select each (x,y) vector from your matrices. If you want to regress the entire matrix, rather than individual vectors, I would use one of the linear approximation functions such as fitlm in the Statistics and Machine Learning Toolbox, since you will also get a number of useful statistics.
I have tried starting from 1, and It is still not working for first value. :) x and y are ok It estimates perfectly for every value except first one. I think the code is missing soething to work for first value, here 0
I have no idea what you are doing or what your data are. My code works correctly for the vector data you supplied.
If you have data where x is a vector of zeros, y is a vector of zeros, or both are, you will get an indeterminate result.
Yes, sorry I get that now Actually It was my data fault, sorry for that :) Can I ask you one more question I have changed a data to correct one. Now on my plot It wont show the function , could you tell me what I should type below this function to get F(x) with coefficients.
No worries!
I do not know what the problem may be with the text call, although it may be that the location in my code is specific to the data you posted. See if replacing the one-line text call with these three lines works in your code, and displays the function:
textx = min(xlim) + 0.15*diff(xlim);
texty = min(ylim) + 0.85*diff(ylim);
text(textx, texty, eqnstr)
The ‘textx’ and ‘texty’ variables scale it to your axis limits. Experiment with it to get the result you want, so that it does not cover up your data or plot.
It works too :) I deal with it by putting text(800,100,..) now its visible, becouse this point is actually on my plot. After I changed data the whole plot moved. :)
As long as it works in your code, it’s correct!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!