ch 7 Applications APPLICATIONS Polynomials The coefficients of a polynomial can be represented as a row vector and using the linspace command the polynomial can be evaluated for each element of the row vector with the command polyval. For example the polynomial y =x4 -2x3 +4x2 -8x+10 can be evaluated at 6 points from x = 0 to x = 10 with the statements P = [1 –2 4 –8 10]; x = linspace(0,10,6); polyval(P,x) ans 10 10 170 970 3274 8330 The roots of a polynomial can be found using the roots command. Consider the poynomial (x – 3)(x + 2) = x2 – x – 6 which has the roots x = 3 and x – 2. The Matlab commands to evaluate the roots are P = [1 –1 –6] roots(P) 3 2 Another example in which the roots are not real is for the polynomial y =x4 -2x3 +4x2 -8x+10 . The roots are complex where i 1 . Matlab statements P = [1 –2 4 –8 10] -0.4199 + 1.8588i roots(P) -0.4199 - 1.8588i 1.4199 + 0.8588i 1.4199 - 0.8588i Move to later chapter POLYVAL Evaluate polynomial. Y = POLYVAL(P,X), when P is a vector of length N+1 whose elements are the coefficients of a polynomial, is the value of the polynomial evaluated at X. Y = P(1)*X^N + P(2)*X^(N-1) + ... + P(N)*X + P(N+1) If X is a matrix or vector, the polynomial is evaluated at all points in X. See also POLYVALM for evaluation in a matrix sense. Y = POLYVAL(P,X,[],MU) uses XHAT = (X-MU(1))/MU(2) in place of X. The centering and scaling parameters MU are optional output computed by POLYFIT. [Y,DELTA] = POLYVAL(P,X,S) or [Y,DELTA] = POLYVAL(P,X,S,MU) uses the optional output structure S provided by POLYFIT to generate error estimates, Y +/- delta. If the errors in the data input to POLYFIT are independent normal with constant variance, Y +/- DELTA contains at least 50% of the predictions. See also POLYFIT, POLYVALM. POLYFIT Fit polynomial to data. POLYFIT(X,Y,N) finds the coefficients of a polynomial P(X) of degree N that fits the data, P(X(I))~=Y(I), in a least-squares sense. [P,S] = POLYFIT(X,Y,N) returns the polynomial coefficients P and a structure S for use with POLYVAL to obtain error estimates on predictions. If the errors in the data, Y, are independent normal with constant variance, POLYVAL will produce error bounds which contain at least 50% of the predictions. The structure S contains the Cholesky factor of the Vandermonde matrix (R), the degrees of freedom (df), and the norm of the residuals (normr) as fields. [P,S,MU] = POLYFIT(X,Y,N) finds the coefficients of a polynomial in XHAT = (X-MU(1))/MU(2) where MU(1) = mean(X) and MU(2) = std(X). This centering and scaling transformation improves the numerical properties of both the polynomial and the fitting algorithm. Warning messages result if N is >= length(X), if X has repeated, or nearly repeated, points, or if X might need centering and scaling. See also POLY, POLYVAL, ROOTS. ROOTS Find polynomial roots. ROOTS(C) computes the roots of the polynomial whose coefficients are the elements of the vector C. If C has N+1 components, the polynomial is C(1)*X^N + ... + C(N)*X + C(N+1). See also POLY, RESIDUE, FZERO. POLYVALM Evaluate polynomial with matrix argument. Y = POLYVAL(P,X), when P is a vector of length N+1 whose elements are the coefficients of a polynomial, is the value of the polynomial evaluated with matrix argument X. X must be a square matrix. Y = P(1)*X^N + P(2)*X^(N-1) + ... + P(N)*X + P(N+1)*I See also POLYVAL, POLYFIT. FZERO Scalar nonlinear zero finding. X = FZERO(FUN,X0) tries to find a zero of the function FUN near X0. FUN accepts real scalar input X and returns a real scalar function value F evaluated at X. The value X returned by FZERO is near a point where FUN changes sign (if FUN is continuous), or NaN if the search fails. X = FZERO(FUN,X0), where X is a vector of length 2, assumes X0 is an interval where the sign of FUN(X0(1)) differs from the sign of FUN(X0(2)). An error occurs if this is not true. Calling FZERO with an interval guarantees FZERO will return a value near a point where FUN changes sign. X = FZERO(FUN,X0), where X0 is a scalar value, uses X0 as a starting guess. FZERO looks for an interval containing a sign change for FUN and containing X0. If no such interval is found, NaN is returned. In this case, the search terminates when the search interval is expanded until an Inf, NaN, or complex value is found. X = FZERO(FUN,X0,OPTIONS) minimizes with the default optimization parameters replaced by values in the structure OPTIONS, an argument created with the OPTIMSET function. See OPTIMSET for details. Used options are Display and TolX. Use OPTIONS = [] as a place holder if no options are set. X = FZERO(FUN,X0,OPTIONS,P1,P2,...) allows for additional arguments which are passed to the function, F=feval(FUN,X,P1,P2,...). Pass an empty matrix for OPTIONS to use the default values. [X,FVAL]= FZERO(FUN,...) returns the value of the objective function, described in FUN, at X. [X,FVAL,EXITFLAG] = FZERO(...) returns a string EXITFLAG that describes the exit condition of FZERO. If EXITFLAG is: > 0 then FZERO found a zero X < 0 then no interval was found with a sign change, or NaN or Inf function value was encountered during search for an interval containing a sign change, or a complex function value was encountered during search for an interval containing a sign change. [X,FVAL,EXITFLAG,OUTPUT] = FZERO(...) returns a structure OUTPUT with the number of iterations taken in OUTPUT.iterations. Examples FUN can be specified using @: X = fzero(@sin,3) returns pi. X = fzero(@sin, 3, optimset('disp','iter')) returns pi, uses the default tolerance and displays iteration information. FUN can also be an inline object: X = fzero(inline('sin(3*x)'),2); Limitations X = fzero(inline('abs(x)+1'), 1) returns NaN since this function does not change sign anywhere on the real axis (and does not have a zero as well). X = fzero(@tan,2) returns X near 1.5708 because the discontinuity of this function near the point X gives the appearance (numerically) that the function changes sign at X. See also ROOTS, FMINBND, @, INLINE. examples x = [1.1 2.3 3.9 5.1] y = [3.887 4.276 4.651 2.117] a = polyfit(x,y,length(x)-1) plot(x,y,'r',x,polyval(a,x),'o') r = roots(a) x= 1.1000 2.3000 3.9000 5.1000 4.2760 4.6510 2.1170 1.4385 -2.7477 5.4370 y= 3.8870 a= -0.2015 r= 5.5608 0.7900 + 2.0565i 0.7900 - 2.0565i inverse and simultaneous equations – determinants and eigenvalues determinatns Linear Simultaneous equations Solving sets of linear simultaneous equations is a very important problem and can be solved easily in Matlab in a number of convenient ways. A set of simultaneous equations can be expressed as Ax=b where x and b are column vectors of dimension m and A is a square matrix A with dimensions mm. The goal is find the values for x Using the \ operator x=A\b Using the inverse matrix operation x = A-1 b For example, consider the system of simultaneous equations that represent the current values in a multi-loop circuit 6.1 i1 + 6.8 i2 + 2.4 i3 = 36.5 4.0 i1 + 2.8 i2 - 1.2 i3 = 5.9 3.2 i1 + 0.6 i2 + 3.2 i3 = 12.5 6.1 6.8 2.4 A = [6.1 6.8 2.4 ; 4 2.8 -1.2 ; 3.2 0.6 3.2] 4.0 2.8 1.2 3.2 0.6 36.5 b = [36.5; 5.9; 12.5] 5.9 3.2 0.6791 x=A/b 12.5 -0.1386 0.2909 0.2130 inv(A) 0.2382 -0.1695 -0.2422 0.0939 -0.2591 0.1449 4.6673 3.7102 0.6791 x = inv(K) * b 4.6673 3.7102 i1 = -0.6791, i2 = 4.6673, and i3 = 3.7102 Interpolation The curve fitting methods described in Chapter 6 can be used for interpolation. Give an example for curve fitting of data from Kaye and Laby eg resistance of a tungsten filament with temperature Interpolation with Spline Functions ref Kharab & Guenther p153 Numerical Differentiation Often the derivation of a function has to be estimated from a set of tabulated data. We will consider techniques for estimating the first and second derivatives. There are three formula commonly used for estimating the first derivative of a function f(x) Numerical Integration In many cases is impossible to evaluate a definite integral analytically b I f ( x) dx a The process of numerical quadrature can be used to find an approximate value for I by replacing f(x) by an interpolating polynomial and then integrating the polynomial between the limits a to b. integration Bibliography A. Kharab and R.B. Guenther, An Introduction to Numerical Methods – A Matlab Approach, (Chapman & Hall/CRC, Boca, 2002) An introduction to theory and applications in numerical methods for researchers and professionals with a background only in calculus and basic programming. CD-ROM contains the Matlab code for many algorithms