Estimating two coefficients out of three in quadratic function
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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
Star Strider
on 19 Nov 2017
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
MariapL
on 19 Nov 2017
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....
Star Strider
on 19 Nov 2017
As always, my pleasure.
‘To be sure, you used least square method?’
‘... 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.
MariapL
on 19 Nov 2017
:) 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 :/
Star Strider
on 19 Nov 2017
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
Star Strider
on 19 Nov 2017
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.
MariapL
on 19 Nov 2017
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.
Star Strider
on 19 Nov 2017
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.
MariapL
on 19 Nov 2017
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. :)
Star Strider
on 19 Nov 2017
As long as it works in your code, it’s correct!
More Answers (0)
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)