Download presentation
Presentation is loading. Please wait.
Published byLaurence Hutchinson Modified over 9 years ago
1
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
2
Engr 0012 (04-1) LecNotes 10-02 Polynomials Monomial A single term of the form a n x n where a n is a constant and n is either zero (0) or a positive integer examples 2.37x 3 1.0 (3 /2)x 7 which of these are monomials? 2xsin(x) X sin(3 /2)x 4 (3-x)x 2 X
3
Engr 0012 (04-1) LecNotes 10-03 Polynomials Polynomial A function that is the sum of a finite number of monomials. examples f(x) = x 3 - 5x +7 (z) = (3 /2)z 7 which of these are polynomials? d(t) = ½at 2 + v 0 t + d 0 y = 3x 3 - 2x 2 + xf(x) = 2x 2 - 3tan(x) g(z) = (1-z) 2 h(t) = 3 t 2 - 2t -1 X X
4
Engr 0012 (04-1) LecNotes 10-04 Polynomials General Form y = f(x) = a n x n + a n-1 x n-1 + … + a 1 x + a 0 MATLAB P = [a n, a n-1, …, a 1, a 0 ] Coefficient vector If P is known, polynomial is completely defined y = f(x) = P(1)x n + P(2)x n-1 + … + P(n)x + P(n+1) (n+1) elements 1 + a 0 x 0 frequently omitted
5
Engr 0012 (04-1) LecNotes 10-05 Polynomials MATLAB implementation >> P = 1:4 P = 1 2 3 4 >> P(1) ans = 1 can access individual elements of vectors Evaluate y = f(x) at x = 2 using P vector just defined y = f(x) = P(1)x n + P(2)x n-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 loop initialization type index counting limits/step size loop body this loop sums a series of numbers >> fatx fatx = 26 >> fatx2 = polyval(P,x) fatx2 = 26 MATLAB function that executes loop to evaluate a polynomial
6
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)
7
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 graph for additional plotting hold off erases current graph when additional plotting is done plotted a red line with (xfit,yfit) data points
8
Engr 0012 (04-1) LecNotes 10-08 Data analysis created a linear plot with: original data points shown as asterisks linear fit shown as a line
9
Engr 0012 (04-1) LecNotes 10-09 Data analysis >> clear >> load ca10b.dat >> xdata = ca10b(:,1)'; >> ydata = ca10b(:,2)'; >> plot(xdata,ydata,'r*')
10
Engr 0012 (04-1) LecNotes 10-10 Data analysis >> semilogy(xdata,ydata,'r*') >> loglog(xdata,ydata,'r*')
11
Engr 0012 (04-1) LecNotes 10-11 Data analysis looks like the data fit the equation y = Ax B 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 MATLAB log10 = 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;
12
Engr 0012 (04-1) LecNotes 10-12 Data analysis >> hold on >> loglog(xfit,yfit,'r-') >> hold off created a log-log plot with: original data points shown as asterisks log-log fit shown as a line
13
Engr 0012 (04-1) LecNotes 10-13 Data analysis Looks good - but we took log of data to create What if any of the data are negative? Exponential fitTake logarithms 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 Easier to identify data to keep!!!
14
Engr 0012 (04-1) LecNotes 10-14 Data analysis to 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
15
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.