EEE 244 7 Curve Fitting Need for curve









- Slides: 9

EEE 244 -7: Curve Fitting

Need for curve fitting • Engineering projects involve collection of data, such as line voltage, cellular signal power • Curve fitting provides a smooth fit to the data by an approximating function • Data can be approximated by polynomial functions and splines

Polynomial functions • Approximating curve yc represented by an mth order polynomial: • Polynomial coefficients c 1, c 2 ……cm+1 values are obtained from data points • Linear or straight line fit: m = 1 Nonlinear fit : m > 1 3

Matlab/Python functions for polynomial curve fitting • The coefficient matrix C = [c 1, c 2 ……cm+1] can be calculated by the polyfit command: [x y] is the data set m is the order of the polynomial Matlab clear x=[…. . ] y =[…. ] C = polyfit(x, y, m) Python import numpy as np x = np. array([…. . ]) y = np. array([………]) C = np. polyfit(x, y, m) 4

Matlab/Python functions for interpolation • The command polyval (C, xi) gives the values of the polynomial at the vector xi Matlab clear yi=polyval (C, xi) plot(x, y, 'o', xi, yi) Python import numpy as np import matplotlib. pyplot as plt yi=np. polyval (C, xi) plt. plot(x, y, “o”, xi, yi) plt. show() 5

Example of polynomial curve fitting 6

Cubic splines • Polynomial approximation can produce points that are not allowed • For example, if the data is for absolute voltage, polynomial can have negative and positive values • Splines are piecewise approximating cubic functions that can overcome polynomial problems 7

Matlab/Python command for cubic splines • The Matlab/Python commands interp 1/interp 1 d create cubic spline set • Given data set [x y], yi is the value at the point xi • The function is obtained by giving a range of [xi yi[ Matlab clear yi = interp 1(x, y, xi, ‘spline’) plot(x, y, 'o', xi, yi) Python import numpy as np from scipy import interpolate import matplotlib. pyplot as plt splines = interpolate. splrep(x, y) yi=interpolate. splev(xi, splines) plt. plot(x, y, “o”, xi, yi) plt. show() 8

Example of curve fitting Given the following data set: Write a Matlab/Python program to fit a curve using: • Polynomial function of order 3 • Cubic spline fit In both cases, sketch the approximating function 9