Download presentation
Presentation is loading. Please wait.
1
Curve Fitting in Matlab
2
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
3
Linear Regression We want to find the equation of a line that minimizes the sum of squared residuals
4
Linear Regression cont.
We need to find a slope: and an intercept:
5
Linear Regression cont.
We can also find r and r2: More information online Wikipedia Simple Linear Regression
6
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 p1xn+p2xn pn+1
7
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
8
Polyfit Example
9
Fit Highly customizable
Takes a set of x values, a set of corresponding y values, and a fitType fitobject = fit(X, Y, fitType) A fitType describes the type of curve ‘poly1’, ‘exp1’, ‘sin1’, … Define custom functions with expressions or anonymous functions
10
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, fitType) There are several other possible input patterns fitoptions specifies bounds, start point, fitting method
11
Fit Example
12
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
13
Randomness Example
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.