engr 0012 (04-1) lecnotes 10-01

15
ngr 0012 (04-1) LecNotes 10-01 working toward a script that will help analyze data ig” steps in accomplishing goal Load data from file (have already created functio 2. Display options 1. linear fit with equation and error 2. semi-log fit with equation and error 3. log-log fit with equation and error 4. linear display with polynomial-fit equation a 5. linear display with spline-fit 3. Quantitative output to screen

Upload: deana

Post on 15-Jan-2016

34 views

Category:

Documents


0 download

DESCRIPTION

working toward a script that will help analyze data. “Big” steps in accomplishing goal. 1. Load data from file (have already created function). 2. Display options. 1. linear fit with equation and error. 2. semi-log fit with equation and error. 3. log-log fit with equation and error. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Engr 0012 (04-1) LecNotes 10-01

Engr 0012 (04-1) LecNotes 10-01

working toward a script that will help analyze data

“Big” steps in accomplishing goal

1. Load data from file (have already created function)

2. Display options

1. linear fit with equation and error

2. semi-log fit with equation and error

3. log-log fit with equation and error

4. linear display with polynomial-fit equation and error

5. linear display with spline-fit

3. Quantitative output to screen

Page 2: Engr 0012 (04-1) LecNotes 10-01

Engr 0012 (04-1) LecNotes 10-02

PolynomialsMonomial

A single term of the form anxn where an is a constant and n is either zero (0) or a positive integer

examples

2.37x3 1.0 (3/2)x7

which of these are monomials?

2x sin(x)X sin(3/2)x4 (3-x)x2X

Page 3: Engr 0012 (04-1) LecNotes 10-01

Engr 0012 (04-1) LecNotes 10-03

PolynomialsPolynomial

A function that is the sum of a finite number of monomials.

examples

f(x) = x3 - 5x +7 (z) = (3/2)z7

which of these are polynomials?

d(t) = ½at2 + v0t + d0

y = 3x3 - 2x2 + x f(x) = 2x2 - 3tan(x)

g(z) = (1-z)2 h(t) = 3t2 - 2t-1

XX

Page 4: Engr 0012 (04-1) LecNotes 10-01

Engr 0012 (04-1) LecNotes 10-04

PolynomialsGeneral Form

y = f(x) = anxn + an-1xn-1 + … + a1x + a0

MATLAB

P = [an, an-1, …, a1, a0]Coefficient vector

If P is known, polynomial is completely defined

y = f(x) = P(1)xn + P(2)xn-1 + … + P(n)x + P(n+1)

(n+1) elements

1 + a0x0

frequently omitted

Page 5: Engr 0012 (04-1) LecNotes 10-01

Engr 0012 (04-1) LecNotes 10-05

PolynomialsMATLAB implementation

>> P = 1:4P = 1 2 3 4

>> P(1)ans = 1

can access individualelements of vectors

Evaluate y = f(x) at x = 2 using P vector just defined

y = f(x) = P(1)xn + P(2)xn-1 + … + P(n)x + P(n+1)

>> x=2;

>> fatx = 0;>> for (i=1:1:4) fatx = fatx + P(i)*x^(4-i);end % for loop

first introduction to a loopinitializationtype

index

counting limits/step size

loop body

this loop sums a series of numbers>> fatxfatx = 26

>> fatx2 = polyval(P,x)fatx2 = 26

MATLAB function that executesloop to evaluate a polynomial

Page 6: Engr 0012 (04-1) LecNotes 10-01

Engr 0012 (04-1) LecNotes 10-06

Data analysis

>> load ca10a.dat>> xdata = ca10a(:,1);>> ydata = ca10a(:,2);

>> plot(xdata,ydata,'r*')

MATLAB - data “fitting” - looking for “best” fit

polycoeff = polyfit(x,y,n)

x x data vector

y y data vector

n polynomial “degree”

polycoeff P (coefficient vector)

X Y1 3.03 4.36 6.39 8.314 11.617 13.624 18.3

ca10a.datcontents

Page 7: Engr 0012 (04-1) LecNotes 10-01

Engr 0012 (04-1) LecNotes 10-07

Data analysis>> lincoef = polyfit(xdata,ydata,1)lincoef = 0.6651 2.3121

==> equation for linear data fit y = 0.67x + 2.31

>> xfit = linspace(xdata(1),xdata(length(xdata)),200)>> yfit = polyval(lincoef,xfit)

length(x) tells how many elements are in vector x

created a set of (x,y) points according to the “fit”

>> hold on>> plot(xfit,yfit,'r-')>> hold off

hold affects current graph

hold on maintains current graphfor additional plottinghold off erases current graph when additional plotting is done

plotted a red line with (xfit,yfit) data points

Page 8: Engr 0012 (04-1) LecNotes 10-01

Engr 0012 (04-1) LecNotes 10-08

Data analysis

created a linearplot with:original data points shown as asteriskslinear fit shown as a line

Page 9: Engr 0012 (04-1) LecNotes 10-01

Engr 0012 (04-1) LecNotes 10-09

Data analysis

>> clear>> load ca10b.dat>> xdata = ca10b(:,1)';>> ydata = ca10b(:,2)';

>> plot(xdata,ydata,'r*')

X Y1 0.73 4.36 14.09 27.914 59.217 82.424 148.0

ca10b.datcontents

Page 10: Engr 0012 (04-1) LecNotes 10-01

Engr 0012 (04-1) LecNotes 10-10

Data analysis>> semilogy(xdata,ydata,'r*')

>> loglog(xdata,ydata,'r*')

Page 11: Engr 0012 (04-1) LecNotes 10-01

Engr 0012 (04-1) LecNotes 10-11

Data analysislooks like the data fit the equation y = AxB

how do we find A and B?

transform equation (take ln of each side) ln(y) = ln(A) + Bln(x)

can we use polyfit?

>> loglogcoef = polyfit( )log(xpts),

log = natural log in MATLABlog10 = base 10 log

log(ypts),1

loglogcoef = 1.6875 -0.3748

B ln(A)

>> B = loglogcoef(1);>> A = exp(loglogcoef(2));

>> xfit = linspace(xdata(1),xdata(length(xdata)),200);>> yfit = A*xfit^B;

Page 12: Engr 0012 (04-1) LecNotes 10-01

Engr 0012 (04-1) LecNotes 10-12

Data analysis >> hold on>> loglog(xfit,yfit,'r-')>> hold off

created a log-logplot with:original data points shown as asteriskslog-log fit shown as a line

Page 13: Engr 0012 (04-1) LecNotes 10-01

Engr 0012 (04-1) LecNotes 10-13

Data analysisLooks good - but we took log of data to createWhat if any of the data are negative?

Exponential fit

BxAey Take logarithms

)ln()ln( ABxy

Need to delete any negative y-data and associated x-data

% filter data for semi-log fit newx = [ ]; newy = [ ]; for i = 1:1:length(xdata) if ( ydata(i) > 0 ) newx = [newx xdata(i)]; newy = [newy ydata(i)]; end end

Easier to identify data to keep!!!

Page 14: Engr 0012 (04-1) LecNotes 10-01

Engr 0012 (04-1) LecNotes 10-14

Data analysisto use the extracted data set: polyfit

% add line fit to graphical display semilogy(xnew,ynew,'r*', xfit,yfit,'r-')

% determine fit to data semilogcoef = polyfit(xnew,log(ynew),1); B = semilogcoef(1); A = exp(semilogcoef(2));

% create (xfit,yfit) for line plot xfit = linspace(xnew(1),xnew(length(xnew)),200); yfit = A*exp(B*xfit);

% add line fit to graphical display semilogy(xnew,ynew,'r*') hold on semilogy(xfit,yfit,'r-') hold off

OR

Page 15: Engr 0012 (04-1) LecNotes 10-01

Engr 0012 (04-1) LecNotes 10-15

general strategy for graphical display% filter data set (if necessary)

% find linear regression coefficients

% create (xfit,yfit) for line plot

% plot data, plot fit

% do error analysis