Curve Fitting Matlab
Curve Fitting Matlab
12
10
6
Least squares
4 Robust
-2
1 2 3 4 5 6 7 8 9 10
Curve Fitting
A least squares linear fit minimizes the square of
the distance between every data point and the line
of best fit
[var1, var2] =
textread('testdata2.txt','%f%f','headerlines',1
) 3
P = robustfit(var1,var2,1); 2
Y = polyval([P(2),P(1)],var1);
1
close all
figure(1) 0
hold on -1
plot(var1,var2,'ro')
plot(var1,Y) -2
-3
-2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5
Higher Order Curve Fitting
Method 1
Polyfit can be used to calculate any polynomial
fitting function of the form:
-1
-2
-3
-2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5
Higher Order Curve Fitting
Method 2
Curve Fitting Tools can be accessed directly from the
figure window:
Method 3
load hahn1
2. Open the Curve Fitting app. Enter:
cftool
5. Choose a different model type using the fit category drop-down list, e.g., select
Polynomial.
Curve Fitting app creates a file in the Editor containing MATLAB code to recreate all
fits and plots in your interactive session.
Programmatic Curve Fitting
To programmatically fit a curve, follow the steps in this simple example:
You can: