Curve Fitting
Curve Fitting
m 1 of 2
%% ******************
% CURVE FITTING
% *****************
% Two steps:
% 1. find out all n+1 a's coefficeint
% y = a(n)*x^n+a(n-1)*x^(n-1)+.......a(0); "polyfit"
% 2. generate the new data point using these a's
coefficients: "polyval"
%% part A
clear all;
% x=[1 2 3 4 5 6 7 8 ];
% y=[1 2 3.5 4.5 5 5.7 6.8 8];
% a=polyfit(x,y,1);
% xi=linspace(1,8,100);
% yi=polyval(a,xi);
% plot(x,y,'o',xi,yi,'r')
% legend('exp data point','fitted curve')
%% part B
% Expected function for data P(t)=P(0)exp(-t/tau); find out
the coefficient P(0)
t=[0 0.50 1.0 5.0 10.0 20.0];
p=[760 625 528 85 14 0.16];
pbar=log(p);
tbar=t;
a=polyfit(tbar,pbar,1);
tau=-1/(a(1));
p0=exp(a(2));
disp(['coeff: p0 =',num2str(p0), ' and tau =', num2str
(tau)]);
tnew=linspace(0,20,100); % generate new time data
pnew=p0.*exp(-tnew/tau); % generate Pressure data using
given formula
figure
plot(t,p,'o',tnew,pnew)
15/11/21 7:52 PM I:\...\curve_fit.m 2 of 2