find the values of polynomial to use polyfit
2 次查看(过去 30 天)
显示 更早的评论
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,
where this data is fitted in the form of
So, let's define population as p
Since ,
And log of p is
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.
采纳的回答
Cris LaPierre
2020-3-26
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 个评论
Zeynep Toprak
2020-3-26
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.
Cris LaPierre
2020-3-26
编辑:Cris LaPierre
2020-3-26
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
2020-3-26
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
2020-3-26
编辑:Cris LaPierre
2020-3-26
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
2020-3-27
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?
Zeynep Toprak
2020-3-27
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
2020-3-27
编辑:Cris LaPierre
2020-3-27
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
2020-3-27
I get cdate and pop when I load census. And I directly put them into the polyfit, I get this result;
Are these three values c=[c1, c2, c3] ? Right?
Cris LaPierre
2020-3-27
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
2020-3-27
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
2020-3-27
b = 1.0e+03* (-2.2120), which is error estimate. And,
ln m = 1.0e+03* 0.0012. Right?
Cris LaPierre
2020-3-27
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
2020-3-27
编辑:Cris LaPierre
2020-3-27
Yes, your m and b are correct for the data you used.
However, they are not yet c2 and c3.
Zeynep Toprak
2020-3-27
I dont use population variable directly, since I take its log, I should use log. Now, is it okay?
Cris LaPierre
2020-3-27
编辑:Cris LaPierre
2020-3-27
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
2020-3-27
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
2020-3-27
since this gives log(c2), in order purely get c2, I will take its exponential, i.e.
Cris LaPierre
2020-3-27
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?
Cris LaPierre
2020-3-27
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
2020-3-27
wow, perfect thank you so much for your great effort!! You're the best! Take care! :)
Zeynep Toprak
2020-3-28
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. :)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polynomials 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)