이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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개)
참고 항목
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 (한국어)