You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
find the values of polynomial to use polyfit
3 views (last 30 days)
Show older comments
By using the census population data, Show that the relationship results between t and the log of the population by setting
. To find
and
, you use the code of polyfit,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/279840/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/279843/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/279844/image.png)
where this data is fitted in the form of ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/279838/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/279838/image.png)
So, let's define population as p
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/279839/image.png)
Since
,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/279840/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/279841/image.png)
And log of p is
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/279842/image.png)
load census
t = (cdate - 1790)/10;
lnp = @(c) log(c(2)) + c(3)*t;
polyfit( lnp, t)
I write this code to find c(2) and c(3). But this code does not work. I get error.
Accepted Answer
Cris LaPierre
on 26 Mar 2020
With polyfit, you supply it the X and Y data and it returns the equation for you. Well, the coefficients of the polynomial of the order you specify. The error message you are getting is becaue you are trying to pass in an anonymous function for X.
28 Comments
Zeynep Toprak
on 26 Mar 2020
yes, I take a look at this website. But I cannot find a way to correct error. How can I correct according to given question? Please help me. If you want, I can re-state what I want in that question. Many thanks and I will be happy if you help me.
Zeynep Toprak
on 26 Mar 2020
by the way, thanks for editting. I dont know how to write here properly.
Cris LaPierre
on 26 Mar 2020
Edited: Cris LaPierre
on 26 Mar 2020
I've fixed your LaTex expressions.
Using polyfit with 2 coefficients (polyfit(x,y,1)) will fit the equation of a straight line (y=m*x+b) to your data. See if you can make your equation for log(p) look like m*x+b.
Then figure out what x and y for your equation are, and put those into polyfit. What polyfit will return are the values of the coefficients (m and b, or whatever variable those map to in your equation).
Zeynep Toprak
on 26 Mar 2020
Thanks for your great explanation. Here what are my variables x and y? y is lnp and x is t. right? But in this case again I face with different size error.
Cris LaPierre
on 26 Mar 2020
Edited: Cris LaPierre
on 26 Mar 2020
The problem is lnp is an anonymous function. You don't need it. Instead, make it the y vector of numbers you need using the data you loaded from census.
Cris LaPierre
on 27 Mar 2020
Sure you can. Your instructions and derivation tell you exactly how to do it.
Maybe approach this from a different angle. You are solving for the coefficients
and
. That means you don't know them. So what do you know? Well, at this stage, the only information you have is the information you loaded from census. What variables did you load? Where do they fit into your solution?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/279879/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/279880/image.png)
Zeynep Toprak
on 27 Mar 2020
Hmm.. for that, I create a matrix to fit them into the solution like A = [ ones(size(t)) t ].
In fact, my function is like that f = @(t) [ ones(size(t)) t ] * c where c = [ d c_3]' with d = log(c2) and from here, I can find it by c_3 = exp(d).
Is it right? If yes, how can I continue ? by the way, many thanks for your great teaching and supports:)
Cris LaPierre
on 27 Mar 2020
Edited: Cris LaPierre
on 27 Mar 2020
Stop trying to use an anonymous function. That is not how polyfit works.
What variables do you get when you run the load census command?
Also, I would recommend treating c1,c2 and c3 as separate variables instead of the vector c you are trying to use.
Zeynep Toprak
on 27 Mar 2020
I get cdate and pop when I load census. And I directly put them into the polyfit, I get this result;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/279989/image.png)
Are these three values c=[c1, c2, c3] ? Right?
Cris LaPierre
on 27 Mar 2020
Closer, but not yet.
First, use 1 instead of 2 (this number represents the highest polynomial power. In m*x+b, it's x^1). You should be getting two coefficients out, m and b. Read the doc to figure out which is m and which is b.
Second, look at your derivations. You did something to the original equation to make it so that you could do a linear fit to the data. Right now, you are not fitting the transformed data, so the estimates of c2 and c3 are wrong.
Zeynep Toprak
on 27 Mar 2020
you explain really in a good way. But I don't understand and know how to apply for them. I am not computer student. I just try to follow some lectures on the youtube and try to do some questions by myself. Please can you show their applications.
Zeynep Toprak
on 27 Mar 2020
b = 1.0e+03* (-2.2120), which is error estimate. And,
ln m = 1.0e+03* 0.0012. Right?
Cris LaPierre
on 27 Mar 2020
Closer. Now go back to the derivation you shared in the beginning. Reread it carefully. Look at the final equation. Compare what is shown there to what you are passing into polyfit. See any discrepancies?
It might help to write the equation using the variables to spot the differences.
Cris LaPierre
on 27 Mar 2020
Edited: Cris LaPierre
on 27 Mar 2020
Yes, your m and b are correct for the data you used.
However, they are not yet c2 and c3.
Zeynep Toprak
on 27 Mar 2020
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/280015/image.png)
I dont use population variable directly, since I take its log, I should use log. Now, is it okay?
Cris LaPierre
on 27 Mar 2020
Edited: Cris LaPierre
on 27 Mar 2020
There you go. Yes, you need to fit to the natural log of pop (In MATLAB, log(pop)).
Now look at that final equation again (log(p)=...). When you fit to log(p), what will your coefficients be? Hint - they are written in your derived equation.
Cris LaPierre
on 27 Mar 2020
Also, note that you fit to cdate and not t. You can use either, as the fit is the same. However, it will affect the value of b since your axis will be scaled differently.
Zeynep Toprak
on 27 Mar 2020
since this gives log(c2), in order purely get c2, I will take its exponential, i.e. ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/280032/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/280032/image.png)
Cris LaPierre
on 27 Mar 2020
You can capture the coefficients in a variable:
p = polyfit(cdate,log(pop),1)
p = 1×2
0.0208 -35.4457
Is c2 the m or b in the m*x+b equation?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/280039/image.png)
Zeynep Toprak
on 27 Mar 2020
b = log(c2) = - 35.4457, thus c2 = exp(-35.4457)
and m = c3 = 0.0208.
Cris LaPierre
on 27 Mar 2020
I knew you could do it! Nice job.
Once you have the coefficients captured in p, you can use polyval to compare the fit to the original.
load census
p = polyfit(cdate,log(pop),1)
% generate fit line
yfit=polyval(p,cdate);
% visualize
plot(cdate,log(pop),'-ro',cdate,yfit,'--')
legend({'ln(pop)','fit'},'Location',"best")
Zeynep Toprak
on 27 Mar 2020
wow, perfect thank you so much for your great effort!! You're the best! Take care! :)
Zeynep Toprak
on 28 Mar 2020
Hello professor again, yesterday, you explained this question perfectly. these are not assigments. since classes are closed down, on these leisure times, I try to improve my matlab skills by myself by following videos on youtube and solving some exercises. i have one more question. i solved it mostly. but at the end i need to solve two nonlinear equation simutanously. bu i could not here. Or can i do question with fminsearch ? My question is here if you 're going to help me one more, i will be glad. :)
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)