Curve Fitting in Matlab Goals Write your own

Curve Fitting in Matlab

Goals ●Write your own simple linear regression ●Learn what the inputs and outputs are for two Matlab curve fitting functions: ○ polyfit ○ fit ●Practice using these functions ●Learn some basic tools for generating random values

Linear Regression ●We want to find the equation of a line that minimizes the sum of squared residuals http: //www. rossmanchance. com/wsf/solutions/inclass/010 -1 n. gif

Linear Regression cont. ●We need to find a slope: ●and an intercept:

Linear Regression cont. ●We can also find r and r 2: ●More information online ○ Wikipedia Simple Linear Regression

Polyfit ●Takes a set of x values, a set of corresponding y values, and a degree ○ p = polyfit(X, Y, n) ●Returns a list of coefficients for the fit ○ Ordered by descending power ○ p 1 xn+p 2 xn-1+. . . +pn+1
![Polyfit cont. ●Additional outputs: ●[p, S] = polyfit(X, Y, n) ○ S can be Polyfit cont. ●Additional outputs: ●[p, S] = polyfit(X, Y, n) ○ S can be](http://slidetodoc.com/presentation_image_h/8ad5ba91185067a512167daa24ed5893/image-7.jpg)
Polyfit cont. ●Additional outputs: ●[p, S] = polyfit(X, Y, n) ○ S can be used with polyval to get error estimates ●[p, S, mu] = polyfit(X, Y, n) ○ mu = [mean(x) std(x)] ○ This performs a transformation first so p and S will not be the same as before

Polyfit Example

Fit ●Highly customizable ●Takes a set of x values, a set of corresponding y values, and a fit. Type ○ fitobject = fit(X, Y, fit. Type) ● A fit. Type describes the type of curve ○ ‘poly 1’, ‘exp 1’, ‘sin 1’, … ○ Define custom functions with expressions or anonymous functions

Fit cont. ●The output is a fitobject which can be used in plotting ●fit can also output goodness of fit statistics ○ [fitobject, gof] = fit(X, Y, fit. Type) ●There are several other possible input patterns ○ fitoptions specifies bounds, start point, fitting method

Fit Example

Randomness ●The ability to generate random values is extremely important ○ We have to settle for pseudorandom numbers ●We would also like reproducibility ○ Seed random value generators ○ Use the rng function in Matlab

Randomness Example
- Slides: 13