0% found this document useful (0 votes)
44 views

Curve Fitting

The document discusses regression analysis and curve fitting, which involves using functions and polynomials to fit data points and create a mathematical model. It covers using linear, polynomial, power, exponential, logarithmic, and reciprocal functions to fit data by determining the coefficients that best model the relationship between the variables. Examples are provided on how to use the polyfit function in MATLAB/Octave to perform curve fitting with different functions.

Uploaded by

Minhal-Kukda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Curve Fitting

The document discusses regression analysis and curve fitting, which involves using functions and polynomials to fit data points and create a mathematical model. It covers using linear, polynomial, power, exponential, logarithmic, and reciprocal functions to fit data by determining the coefficients that best model the relationship between the variables. Examples are provided on how to use the polyfit function in MATLAB/Octave to perform curve fitting with different functions.

Uploaded by

Minhal-Kukda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

LAB #01

REGRESSION ANALYSIS
OR
CURVE FITTING

Bushra Fatima
NED University of Engineering & Technology August 19, 2020
Regression Analysis or Curve Fitting
•Curve fitting, also called regression analysis, is a
process of fitting a function to a set of data points.
•The function can be used as a mathematical
model of the data.
•Since there are many types of functions (linear,
polynomial, power, exponential, etc.), curve fitting
can be a complicated process.
Curve Fitting with polynomials
Polynomials can be used to fit data points in two ways.

• The polynomial passes through all the data points,


• The polynomial does not pass through any of the
points but overall gives a good approximation of the
data.
Curve Fitting with polynomials (1)
The polynomial passes through all the data points

• When n points (xi, yi) are given  polynomial of degree (n-1) that passes
through all the points.

• 2 points  linear equation 𝑦 = 𝑚𝑥 + 𝑏.


• 3 points  quadratic equation 𝑦 = 𝑎𝑥 2 + 𝑏𝑥 + 𝑐
• n points  polynomial equation 𝑦 = 𝑎𝑛−1 𝑥 𝑛−1 + 𝑎𝑛−2 𝑥 𝑛−2 + ⋯ . +𝑎1 𝑥 + 𝑎0

• The coefficients of the polynomial are determined by substituting each point


in the polynomial and then solving the n equations for the coefficients.
Curve Fitting with polynomials (2)
Polynomials that do not necessarily pass through any of the points:
Curve Fitting with polynomials (2)
Polynomials that do not necessarily pass through any of the points:

• 2 equations with 2 unknowns, 𝑎0 & 𝑎1 .


polyfit function

!!! polyfit function does NOT plot the graph. It only gives the coefficients
A set of seven points is given by (0.9, 0.9), (1.5, 1.5), (3, 2.5), (4, 5.1),(6, 4.5), (8, 4.9),
and (9.5, 6.3).

x=[0.9 1.5 3 4 6 8 9.5];


y=[0.9 1.5 2.5 5.1 4.5 4.9 6.3];
p=polyfit(x,y,3) %p=ax^3+bx^2+cx+d --coefficients only
xp=0.9:0.1:9.5;
yp=polyval(p,xp); %determines value of the polynomial
at each xp
plot(x,y,'o') %plots the scatter plot of data points
hold on
plot(xp,yp) %plots the polynomial on the same plot
xlabel('x'); ylabel('y')
Curve Fitting with functions other than polynomials
Function General Linear Form polyfit function form
Equation
Power 𝑦 = 𝑏𝑥 𝑚 ln 𝑦 = 𝑚 ln 𝑥 + ln 𝑏 p=polyfit(log(x),log(y),1)
Exponential 𝑦 = 𝑏𝑒 𝑚𝑥 ln 𝑦 = 𝑚𝑥 + ln 𝑏 p=polyfit(x,log(y),1)
𝑦 = 𝑏10𝑚𝑥 log 𝑦 = 𝑚𝑥 + log 𝑏 p=polyfit(x,log10(y),1)
logarithmic 𝑦 = 𝑚 ln 𝑥 + 𝑏 𝑦 = 𝑚 ln 𝑥 + 𝑏 p=polyfit(log(x),y,1)
𝑦 = 𝑚 log 𝑥 + 𝑏 𝑦 = 𝑚 log 𝑥 + 𝑏 p=polyfit(log10(x),y,1)
reciprocal 1 1 = 𝑚𝑥 + 𝑏 p=polyfit(x,1./y,1)
𝑦= 𝑦
𝑚𝑥 + 𝑏
Curve Fitting with functions other than polynomials
Functio Linear Form polyfit function form coefficients
n
Power ln 𝑦 = 𝑚 ln 𝑥 + ln 𝑏 p=polyfit(log(x),log(y),1) m=p(1) ; b=exp(p(2));
Exp ln 𝑦 = 𝑚𝑥 + ln 𝑏 p=polyfit(x,log(y),1) m=p(1) ; b=exp(p(2));
log 𝑦 = 𝑚𝑥 + log 𝑏 p=polyfit(x,log10(y),1)
log 𝑦 = 𝑚 ln 𝑥 + 𝑏 p=polyfit(log(x),y,1) m=p(1) ; b=p(2)
𝑦 = 𝑚 log 𝑥 + 𝑏 p=polyfit(log10(x),y,1)
reciproc 1 = 𝑚𝑥 + 𝑏 p=polyfit(x,1./y,1) m=p(1) ; b=p(2)
𝑦
al
Curve Fitting with functions other than polynomials

ln 𝑦 = 𝑚 ln 𝑥 + ln 𝑏

l𝑜𝑔 𝑦 = 𝑚𝑥 + l𝑜𝑔 𝑏
Curve Fitting with functions other than polynomials
Curve Fitting with functions other than polynomials
Solution: Scatter Plot (linear axes)

1. Linear function
2. Logarithmic function
3. power function
4. Exponential function
5. Reciprocal function
Solution: Scatter Plot (semi log axis)
Checking for Exponential function
Solution: Scatter Plot (linear axes)
Checking for reciprocal function
Solution: selected plot:
Function chosen: Exponential function

t=0:0.5:5;
w=[6 4.83 3.7 3.15 2.41 1.83 1.49 1.21 0.96 0.73 0.64];
p=polyfit(t,log(w),1);
m=p(1)
b=exp(p(2))
tp=0:0.1:5;
wp=b*exp(m*tp);
plot(t,w,'o',tp,wp)
xlabel('t'), ylabel('w')
Solution: selected plot:
Function chosen: Exponential function

You might also like