Sei sulla pagina 1di 19

CURVE FITTING

-Kunwar Gaurav Sahu(1705220023)


-Kush Pathak(1705220024)
-Lokendra Rathore(1705220025)
Curve Fitting
• Curve fitting is the process of constructing a curve, or mathematical function,
that has the best fit to a series of data points,possibly subject to constraints.

• Curve fitting examines the relationship between one or more predictors


(independent variables) and a response variable (dependent variable), with
the goal of defining a "best fit" model of the relationship.
Curve Fitting
Plotting a line of best fit in Matlab can be performed using either
a traditional least squares fit or a robust fitting method.
Robust Vs Least Squares Demo (robustdemo)

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

polyfit(X,Y,N) finds the coefficients of a polynomial


P(X) of degree N that fits the data
Uses least-square minimization
N = 1 (linear fit)

[P] = polyfit(X,Y,N) returns P, a matrix containing


the slope and the x intercept for a linear fit
[Y] = polyval(P,X) calculates the Y values for every
X point on the line of best fit
Curve Fitting
Example:

Draw a line of best fit using robust fit


approximation for the data in exercise 2

[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:

y = polyval(p,x) returns the value of a polynomial


of degree n evaluated at x. The input argument p
is a vector of length n+1 whose elements are the
coefficients in descending powers of the
polynomial to be evaluated.

x can be a matrix or a vector. In either case,


polyval evaluates p at each element of x.
Calculate a polynomial fit of order 2
2nd Order Polynomial Fit:
%read data
[var1, var2] =
textread(‘week8_testdata2.txt','%f%f','headerlines',1)
% Calculate 2nd order polynomial fit
P2 = polyfit(var1,var2,2);
Y2 = polyval(P2,var1);
%Plot fit
close all
figure(1)
hold on
plot(var1,var2,'ro')
[sortedvar1, sortind] = sort(var1)
plot(sortedvar1,Y2(sortind),'b*-')
2nd Order Polynomial Fit:
3

-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:

To access curve fitting directly from the figure window,


select ‘basic fitting’ from the ‘tools’ pulldown menu in
the figure window.

This is a quick and easy method to calculate and


visualize a variety of higher order functions including
interpolation
Higher Order Curve Fitting
Caution:
• Higher order polynomial fits should only be
used when a large number of data points
are available

• Higher order polynomial fitting functions


may fit more the data more accurately but
may not yield an interpretable model
Higher Order Curve Fitting

Method 3

• Curve Fitting Toolbox


• The curve fitting toolbox is accessible by
the ‘cftool’ command
• Very powerful tool for data smoothing,
curve fitting, and applying and evaluating
mathematical models to data points
What are Matlab Toolboxes ?

"Toolbox" is Mathwork's marketing name for a set of


functions designed for a related purpose and sold
as a package. As well as the implementation as a
folder, there are hooks into the licensing system
and the documentation system.

Some of the Matlab Toolboxes are:


-Symbolic Math Toolbox
-Curve Fitting Toolbox
-Optimization Toolbox
-Sim Power System
-Financial Toolbox
-Simscape
Curve Fitting Toolbox

• Fit curves and surfaces to data using regression,


interpolation, and smoothing

• Curve Fitting Toolbox provides an app and functions for


fitting curves and surfaces to data.

• The toolbox lets us perform exploratory data analysis.


We can conduct regression analysis using the library of
linear and nonlinear models provided or specify our own
custom equations.
Curve Fitting Toolbox

• The curve fitting toolbox is accessible by


the ‘cftool’ command

• Very powerful tool for data smoothing,


curve fitting, and applying and evaluating
mathematical models to data points
Curve Fitting Toolbox

Curve Fitting Toolbox software allows you to


work in two different environments:

-An interactive environment, with the Curve Fitting


app and the Spline Tool

-A programmatic environment that allows you to


write object-oriented MATLAB code using curve
and surface fitting methods
Interactive Curve Fitting
To interactively fit a curve, follow the steps in this simple example:

1. Load some data at the MATLAB® command line.

load hahn1
2. Open the Curve Fitting app. Enter:

cftool

3. In the Curve Fitting app, select X Data and Y Data.

4. Curve Fitting app creates a default interpolation fit to the data.

5. Choose a different model type using the fit category drop-down list, e.g., select
Polynomial.

6. Try different fit options for your chosen model type.

7. Select File > Generate Code.

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:

1.Load some data.


load hahn1
Create a fit using the fit function, specifying the variables and a model
type (in this case rat23 is the model type).
f = fit( temp, thermex, 'rat23' )

Plot your fit and the data.


plot( f, temp, thermex )
f( 600 )
Curve Fitting App
The Curve Fitting app provides a flexible interface where you can
interactively fit curves and surfaces to data and view plots

You can:

• Create, plot, and compare


multiple fits.

• Use linear or nonlinear regression,


interpolation, smoothing, and
custom equations.

• View goodness-of-fit statistics,


display confidence intervals and
residuals, remove outliers and
assess fits with validation data.

• Automatically generate code to fit


and plot curves and surfaces, or
export fits to the workspace for
further analysis.

Potrebbero piacerti anche