02.03.2016 Views

MATLAB by rudra pratap

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

146<br />

Applications<br />

% Fit a 1st order polynomial through (tbar , pbar)<br />

a = polyfit (tbar ,pbar,l) ; % the output is a = [a1 aO]<br />

% Evaluate constants pO and tau<br />

pO = exp(a(2) );<br />

% since a(2) = aO = log(pO)<br />

tau = -1/a(i) ; % since al = - 1/tau<br />

% (a) Plot the new curve and the data on l inear s cale<br />

tnew = linspace (0,20,20) ; % create more refined t<br />

pnew = pO*exp(-tnew/tau) ; % evaluate p at new t<br />

plot (t,p, 'o' ,tnew ,pnew) , grid<br />

xlabel('Time (sec) '), ylabel ('Pressure (torr) ')<br />

% (b) Plot the new curve and the data on semilog scale<br />

lpnew = exp (polyval (a,tnew) );<br />

semilogy(t,p, 'o' ,tnew, lpnew) ,grid<br />

xlabel ('Time (sec) '), ylabel('Pressure (torr) ')<br />

% Note: you only need one plot , you can select (a) or (b) .<br />

(a) (b)<br />

Figure 5.6: Exponential curve fit: (a) linear scale plot, (b) semilog scale plot.<br />

There is yet another way to fit a complicated function through your data in the<br />

least squares sense. For example, let us say that you have time (t) and displacement<br />

(y) data from a spring-mass system experiment and you think that the data should<br />

follow<br />

y = a0 cos(t) + a1tsin(t)<br />

Here the unknowns are only ao and a1 . The equation is nonlinear in t but linear- in<br />

the parameters a0 and a1 . Therefore, you can set up a matrix equation using each

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!