- Example 8: Let’s try with the Toad Data and a polynomial of degree 3!!!
- Note: The Import Wizard feature that follows only works in MATLAB – for Octave create two vectors, Year and Area with the appropriate toad data as entries – we will see a way to do this in a couple slides!.
- From our class web page, save the toaddata.txt file to your Desktop.
- Within this file, rename the second column Area.
- Next, in MATLAB, from the File menu, choose File-> Import Data and select the toaddata.txt file.
- In the Import Wizard window that appears, click on comma for the Column Separator.
- Click on Next.
Fitting Polynomials to Data Fitting Polynomials to Data - Another way to import data from a text file is via “load”.
- Remove the headings “Year” and “Area” from the first line of the toaddata.txt file.
- Then use the commands
- load(‘toaddata.txt’)
- Year = toaddata(:,1)’
- Area = toaddata(:,2)’
Fitting Polynomials to Data - Example 8 (cont.)
- Fit our cubic polynomial to the toad data with polyfit:
- p = polyfit(Year, Area, 3)
- To show more decimal places for our coefficients, use
- Plot the toad data along with the polynomial!
- plot(Year,Area,'r*',Year, polyval(p,Year),'b')
- Compare to Mathematica’s Fit command or Excel’s trendline for a cubic polynomial.
- We should get the same coefficients!
References
- Using MATLAB in Calculus by Gary Jenson
Do'stlaringiz bilan baham: |