Part 1-1: Line Curve Fitting
Problem
Predict the pressure if the temperature is increased to 150C based on Charles's law for ideal gasP=kT, where k is a constant.
Tutorial: Matlab
Copy T=[ 30 40 50 60 70 80 ];
P=[ 1.05 1.07 1.09 1.14 1.17 1.21 ];
% MATLAB function of curvefitting
% z=[a1, a0];
Z=polyfit(T,P, 1 )
% yopt=Z(1).*x+Z(2);
x= 30 : 10 : 150 ;
yopt=polyval(Z,x);
Exercise: MATLAB
Download the tutorial source file
Fill-in the blanks to create function [a0,a1] = linearFit(X, Y)
Exercise: C
Download the tutorial source code
Fill-in the blanks to create functions that calculates coefficients of least squares regression (line)
void linearFit(double vecZ[], double vecX[], double vecY[]);
If you choose to use Matrix structure
linearFit_mat(Matrix _X, Matrix _Y);
Copy double T [] = { 30 , 40 , 50 , 60 , 70 , 80 };
double P [] = { 1.05 , 1.07 , 1.09 , 1.14 , 1.17 , 1.21 };
double Z_Q1[ 2 ] = { 0 };
int n = 1 ; // nth order
int m_Q1 = 6 ; // length of dataset
// Option 1: using 1D array
n = 1 ;
polyFit (Z_Q1 , T , P , n);
// Option 2
// Delete the below if you selected Option 1
Matrix matT = arr2Mat (T , m_Q1 , 1 );
Matrix matP = arr2Mat (P , m_Q1 , 1 );
Matrix vecZ_Q1 = polyFit_mat (matT , matP , n);
printMat (vecZ_Q1 , "Z_Q1" );
Part 1-2: Higher order polynomial curve fitting
Problem
Find the optimal higher-order polynomial to fit the given dataset. assume the model has n=4 order polynomial form
Exercise: MATLAB
Copy
Xdata = 0 : 0.4 : 6 ;
Ydata = [ 0 , 3 , 4.5 , 5.8 , 5.9 , 5.8 , 6.2 , 7.4 , 9.6 , 15.6 , 20.7 , 26.7 , 31.1 , 35.6 , 39.3 , 41.5 ];
% MATLAB function of curvefitting
% z=[an,..., a1, a0];
% polynomial order n=4
n= 4 ;
Z=polyfit(Xdata,Ydata,n)
% yhat on the fitted curve:
% yopt=Z(5).*(x.^4) +...+ +Z(0);
Yopt=polyval(Z,Xdata);
figure
plot(Xdata,Ydata, '*r' )
hold on
plot(Xdata,Yopt, '-b' )
xlabel( 'strain' , 'fontsize' , 15 )
ylabel( 'stress' , 'fontsize' , 15 )
title( 'Polyfit' )
Exercise: C
Fill-in the blanks to create functions that calculates coefficients of least squares regression of Nth order polynomial
void polyFit(double vecZ[], double vecX[], double vecY[], int n);
If you choose to use Matrix structure
Matrix polyFit_mat(Matrix _vecX, Matrix _vecY, int n);