hw6_solns

hw6_solns - Mathematics of Engineering - ME17 Spring 2007...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Mathematics of Engineering - ME17 Spring 2007 Homework #6 - Solutions 1. (15 points total) (a) (2 pts) We need to plot the following data as circles (x1 , y1 ) = (3.2, 22.0), (x2 , y2 ) = (2.7, 17.8), (x3 , y3 ) = (1.0, 14.2), (x4 , y4 ) = (4.8, 38.3), see Matlab program below. (x5 , y5 ) = (5.6, 51.7). (b) (4 pts) For this problem, we need to plot from x = 0 to x = 6 a cubic polynomial which goes through the first four data points on the same graph as for (a). This can be accomplished by using the polyfit and polyval functions in Matlab. The p1 = polyfit(x(1:4),y(1:4),3) command finds the coefficients of a polynomial p1(x) of degree 3 that fits the data y best in a least-squares sense. To plot the data, we use plot(xplot,polyval(p1,xplot),'k-'), where the polyval returns the value of a polynomial p1 evaluated at xplot. Note that we use x(1:4) and y(1:4) since we want to only consider the first four points. For the complete code, see the Matlab program below. (c) (4 pts) This part is exactly like part (b) but we now need to plot the cubic polynomial which best fits the five data points in the least-squares sense. The resulting plot from parts (a), (b), and (c) is given below. Problem 1 60 55 50 45 40 35 30 25 20 15 10 0 1 2 3 x 4 5 6 y data pts 4-pts fit least-squares fit Figure 1: Cubic polynomial fits. 1 (d) (5 pts) To calculate the sum of the squared errors for the polynomials found in parts (b) and (c) we use the sum function in Matlab which gives us the following results: Sum of squared errors: (b) e1 = 42.938187 (c) e2 = 0.625096 The errors show that the least-squares polynomial minimizes the squared errors even though the other polynomial passes exactly through four of the five data points. % % % Problem 1( a ) % % % x = [3 .2 2.7 1.0 4.8 5 . 6 ] ; y = [22.0 17.8 14.2 38.3 5 1 . 7 ] ; hold on ; figure (1) plot ( x , y , ' ko ' ) % Input given data % Hold c u r r e n t p l o t % P l o t s d a t a p o i n t s as c i r c l e s % % % Problem 1( b ) % % % xplot = 0 : 0 . 1 : 6; p1 = p o l y f i t ( x ( 1 : 4 ) , y ( 1 : 4 ) , 3 ) ; plot ( x p l o t , polyval ( p1 , x p l o t ) , ' k ' ) % x-domain % Cubic p o l y n o m i a l g o i n g t h r u f i r s t 4 p t s . % Plots cubic polynomial % % % Problem 1( c ) % % % p2 = p o l y f i t ( x , y , 3 ) ; % Least squares cubic polynomial plot ( x p l o t , polyval ( p2 , x p l o t ) , ' k -. ' ) % P l o t s c u b i c p o l y n o m i a l t i t l e ( ' Problem 1 ' ) ; % P l o t t i t l e , l e g e n d , and a x i s l a b e l s legend ( ' data p t s ' , '4- p t s f i t ' , ' l e a s t -s q u a r e s f i t ' , ' L o c a t i o n ' , ' NorthWest ' ) xlabel ( ' x ' ) ylabel ( ' y ' ) % % % Problem 1( d ) % % % e1 = sum( ( y-polyval ( p1 , x ) ) . ^ 2 ) ; e2 = sum( ( y-polyval ( p2 , x ) ) . ^ 2 ) ; f p r i n t f ( 'Sum o f s q u a r e d e r r o r s : \ n ' ) ; f p r i n t f ( ' ( b ) e1 = %f \n ' , e1 ) ; f p r i n t f ( ' ( c ) e2 = %f \n ' , e2 ) ; % Find t h e sum o f t h e s q u a r e o f t h e e r r o r 2 2. (10 points total) We need to plot the data given by (x1 , y1 ) = (0.3, 0.25), (x2 , y2 ) = (1, 3.03), (x3 , y3 ) = (1.5, 6.8), (x4 , y4 ) = (2.2, 14.48), (x5 , y5 ) = (2.7, 22). To determine the power law relationship between x and y, we need to do a least squares fit for (log10 (xi ), log10 (yi )) using the polyfit function in Matlab as shown in the program below. Note that in Matlab, log is not the same as log10, in other words,the Matlab function log corresponds to the natural logarithm. The general ideal is to used the polyfit function to give us the following equation: log10 y = b log10 x + c, (1) where b and c are the constants returned by polyfit. We can extract the desired equation from the returned values b and c as follows: Raise 10 to both sides of the equation to get rid of the log10 10log10 y = 10b log10 x+c b y = 10log10 x +c b = 10log10 x 10c = xb 10c Now let a = 10c , and we have an equation of the desired form: y = axb (2) In the following, we plot the data points and the least-squares fit in the first plot and the (log10 (xi ), log10 (yi )) along with the equation for the fit in the appropriate form in the second plot. Running the program, shown below, will give us the following output: The polynomial that fits the data is: p = 2.036471 0.469102 a = b = 2.945115 2.036471 The resulting power law relationship between x and y for this data set it: y = 2.9451x2.0365 (3) 3 Problem 2 30 1.5 Problem 2 (a) 25 1 0.5 20 log10(y) (b) 0 -0.5 -1 15 y 10 5 -1.5 -2 -1 0 0 0.5 1 1.5 x 2 2.5 3 -0.5 log10(x) 0 0.5 Figure 2: (a) Data and fit, (b) log10 - log10 plot. % % % Problem 2 % % % x = [0 .3 1 1.5 2.2 2 . 7 ] ; y = [ 0 . 2 5 3.03 6.8 14.48 2 2 ] ; p = p o l y f i t ( log10 ( x ) , log10 ( y ) , 1 ) ; b = p(1); a = 10^ p ( 2 ) ; xplot = 0.1 : 0.1 : 3; yplot = a xplot . ^ b ; hold on ; figure (1) plot ( x , y , ' ko ' ) ; plot ( x p l o t , y p l o t , ' k ' ) ; t i t l e ( ' Problem 2 ' ) ; xlabel ( ' x ' ) ; ylabel ( ' y ' ) ; % Input given data % Least-s q u a r e s f i t % x-domain % Power law r e l a t i o n s h i p e q u a t i o n % Holds c u r r e n t p l o t % Plot data p o i n t s % Plot f i t in Figure 1 % P l o t t i t l e and a x i s l a b e l s figure (2) % log10 plot plot ( log10 ( x ) , log10 ( y ) , ' ko ' ) ; plot ( log10 ( x p l o t ) , polyval ( p , log10 ( x p l o t ) ) , ' k ' ) ; t i t l e ( ' Problem 2 ' ) ; xlabel ( ' l o g 1 0 ( x ) ' ) ; ylabel ( ' l o g 1 0 ( y ) ' ) ; % Display r e s u l t s f p r i n t f ( ' The p o l y n o m i a l t h a t f i t s t h e data i s : \ np = \ t %f \ t %f \n ' , p ( 1 ) , p ( 2 ) ) ; f p r i n t f ( ' \ na = \ t %f \n ' , a ) ; f p r i n t f ( ' b = \ t %f \n ' , b ) ; 4 3. (5 points total) For the data points {(x1 , f1 ), (x2 , f2 )}, the Newton interpolating polynomial is f2 - f1 fN (x) = f1 + (x - x1 ), (4) x2 - x 1 and the Lagrange interpolating polynomial is fL (x) = x - x2 x - x1 f1 + f2 . x1 - x2 x2 - x1 (5) To show that fN (x) = fL (x), we follow the following procedure Starting with (4), we separate f1 and f2 fN (x) = f1 + = Then, fN (x) = x2 - x1 - x + x1 x - x1 f1 + f2 x1 - x2 x2 - x1 x - x2 x - x1 = f1 + f2 x 1 - x2 x2 - x1 f2 - f1 (x - x1 ) x2 - x1 x - x1 x - x1 f2 1- f1 + x2 - x1 x2 - x1 therefore, from (5) we see that indeed fN (x) = fL (x) (6) 5 ...
View Full Document

This homework help was uploaded on 04/07/2008 for the course ME 17 taught by Professor Milstein during the Spring '07 term at UCSB.

Ask a homework question - tutors are online