Excel Definitions


Fitting Polynomials to Data


Download 479.24 Kb.
bet4/4
Sana04.04.2023
Hajmi479.24 Kb.
#1326569
1   2   3   4
Bog'liq
8-lecture

Fitting Polynomials to Data

  • 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

  • Example 8 (cont):
  • Click on Create vectors from each column using column names.
  • Click on Finish.
  • Two new vectors have been created – Year and Area.

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
    • format long
  • 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

Download 479.24 Kb.

Do'stlaringiz bilan baham:
1   2   3   4




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling