- You will see updates in your activity feed.
- You may receive emails, depending on your notification preferences.

6 views (last 30 days)

Show older comments

Zeynep Toprak
on 26 Mar 2020

Commented: Zeynep Toprak
on 28 Mar 2020

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
on 26 Mar 2020

Zeynep Toprak
on 26 Mar 2020

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

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

Cris LaPierre
on 26 Mar 2020

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?

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

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;

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

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

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

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

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

Zeynep Toprak
on 27 Mar 2020

since this gives log(c2), in order purely get c2, I will take its exponential, i.e.

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?

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

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

Start Hunting!Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

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: .

Select web siteYou can also select a web site from the following list:

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

- 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)