Docsity

Numerical Analysis Using MATLAB and Spreadsheets - Steven T. Karris, Notas de estudo de Engenharia Elétrica

Pré-visualização parcial do texto

Baixe Numerical Analysis Using MATLAB and Spreadsheets - Steven T. Karris e outras Notas de estudo em PDF para Engenharia Elétrica, somente na Docsity!

Orchard Publications www.orchardpublications.com

Numerical

Analysis

Using MATLAB® and Spreadsheets

Steven T. Karris

Second Edition

Numerical Analysis Using MATLAB and Spreadsheets, Second Edition i

  • Chapter Table of Contents
  • Command Window....................................................................................................................... 1- Introduction to MATLAB
  • Roots of Polynomials..................................................................................................................... 1-
  • Polynomial Construction from Known Roots 1-
  • Evaluation of a Polynomial at Specified Values 1-
  • Rational Polynomials 1-
  • Using MATLAB to Make Plots.................................................................................................... 1-
  • Subplots....................................................................................................................................... 1-
  • Multiplication, Division and Exponentiation............................................................................. 1-
  • Script and Function Files............................................................................................................ 1-
  • Display Formats 1-
  • Summary 1-
  • Exercises...................................................................................................................................... 1-
  • Solutions to Exercises 1-
  • Chapter
  • Newton’s Method for Root Approximation 2- Root Approximations
  • Approximations with Spreadsheets 2-
  • The Bisection Method for Root Approximation........................................................................ 2-
  • Summary 2-
  • Exercises...................................................................................................................................... 2-
  • Solutions to Exercises 2-
  • Chapter
  • Alternating Voltages and Currents 3- Sinusoids and Phasors
  • Characteristics of Sinusoids 3-
  • Inverse Trigonometric Functions 3-
  • Phasors 3-
  • Addition and Subtraction of Phasors 3-
  • Multiplication of Phasors............................................................................................................ 3-
  • Division of Phasors 3-
  • Exponential and Polar Forms of Phasors ....................................................................................3- ii Numerical Analysis Using MATLAB and Spreadsheets, Second Edition
  • Summary......................................................................................................................................3-
  • Exercises ......................................................................................................................................3-
  • Solutions to Exercises..................................................................................................................3-
  • Chapter
  • Matrix Definition 4- Matrices and Determinants
  • Matrix Operations........................................................................................................................ 4-
  • Special Forms of Matrices 4-
  • Determinants................................................................................................................................ 4-
  • Minors and Cofactors................................................................................................................. 4-
  • Cramer’s Rule............................................................................................................................. 4-
  • Gaussian Elimination Method 4-
  • The Adjoint of a Matrix............................................................................................................. 4-
  • Singular and Non-Singular Matrices 4-
  • The Inverse of a Matrix.............................................................................................................. 4-
  • Solution of Simultaneous Equations with Matrices................................................................... 4-
  • Summary..................................................................................................................................... 4-
  • Exercises 4-
  • Solutions to Exercises................................................................................................................. 4-
  • Chapter
  • Simple Differential Equations .......................................................................................................5- Differential Equations, State Variables, and State Equations
  • Classification .................................................................................................................................5-
  • Solutions of Ordinary Differential Equations (ODE) ...................................................................5-
  • Solution of the Homogeneous ODE .............................................................................................5-
  • Using the Method of Undetermined Coefficients for the Forced Response...............................5-
  • Using the Method of Variation of Parameters for the Forced Response ....................................5-
  • Expressing Differential Equations in State Equation Form ........................................................5-
  • Solution of Single State Equations..............................................................................................5-
  • The State Transition Matrix.......................................................................................................5-
  • Computation of the State Transition Matrix..............................................................................5-
  • Eigenvectors ................................................................................................................................5-
  • Summary......................................................................................................................................5-
  • Exercises ......................................................................................................................................5-
  • Solutions to Exercises..................................................................................................................5-
  • Chapter Numerical Analysis Using MATLAB and Spreadsheets, Second Edition iii
  • Wave Analysis 6- Fourier, Taylor, and Maclaurin Series
  • Evaluation of the Coefficients 6-
  • Symmetry 6-
  • Waveforms in Trigonometric Form of Fourier Series................................................................. 6-
  • Alternate Forms of the Trigonometric Fourier Series 6-
  • The Exponential Form of the Fourier Series 6-
  • Line Spectra 6-
  • Numerical Evaluation of Fourier Coefficients............................................................................ 6-
  • Power Series Expansion of Functions 6-
  • Taylor and Maclaurin Series....................................................................................................... 6-
  • Summary 6-
  • Exercises...................................................................................................................................... 6-
  • Solutions to Exercises 6-
  • Chapter
  • Divided Differences 7- Finite Differences and Interpolation
  • Factorial Polynomials.................................................................................................................... 7-
  • Antidifferences............................................................................................................................ 7-
  • Newton’s Divided Difference Interpolation Method 7-
  • Lagrange’s Interpolation Method 7-
  • Gregory-Newton Forward Interpolation Method....................................................................... 7-
  • Gregory-Newton Backward Interpolation Method 7-
  • Interpolation with MATLAB 7-
  • Summary 7-
  • Exercises...................................................................................................................................... 7-
  • Solutions to Exercises 7-
  • Chapter
  • Curve Fitting................................................................................................................................. 8- Linear and Parabolic Regression
  • Linear Regression.......................................................................................................................... 8-
  • Parabolic Regression 8-
  • Regression with Power Series Approximations 8-
  • Summary 8-
  • Exercises 8- iv Numerical Analysis Using MATLAB and Spreadsheets, Second Edition
  • Solutions to Exercises................................................................................................................. 8-
  • Chapter
  • Taylor Series Method 9- Solution of Differential Equations by Numerical Methods
  • Runge-Kutta Method 9-
  • Adams’ Method.......................................................................................................................... 9-
  • Milne’s Method 9-
  • Summary..................................................................................................................................... 9-
  • Exercises 9-
  • Solutions to Exercises................................................................................................................. 9-
  • Chapter
  • The Trapezoidal Rule................................................................................................................. 10- Integration by Numerical Methods
  • Simpson’s Rule 10-
  • Summary................................................................................................................................... 10-
  • Exercises 10-
  • Solution to Exercises 10-
  • Chapter
  • Definition, Solutions, and Applications..................................................................................... 11- Difference Equations
  • Fibonacci Numbers 11-
  • Summary................................................................................................................................... 11-
  • Exercises 11-
  • Solutions to Exercises............................................................................................................... 11-
  • Chapter
  • Partial Fraction Expansion......................................................................................................... 12- Partial Fraction Expansion
  • Alternate Method of Partial Fraction Expansion 12-
  • Summary................................................................................................................................... 12-
  • Exercises 12-
  • Solutions to Exercises............................................................................................................... 12-
  • Chapter Numerical Analysis Using MATLAB and Spreadsheets, Second Edition v
  • The Gamma Function 13- The Gamma and Beta Functions and Distributions
  • The Gamma Distribution 13-
  • The Beta Function.................................................................................................................... 13-
  • The Beta Distribution............................................................................................................... 13-
  • Summary 13-
  • Exercises.................................................................................................................................... 13-
  • Solutions to Exercises 13-
  • Chapter
  • Orthogonal Functions ................................................................................................................14- Orthogonal Functions and Matrix Factorizations
  • Orthogonal Trajectories .............................................................................................................14-
  • Orthogonal Vectors....................................................................................................................14-
  • The Gram-Schmidt Orthogonalization Procedure .....................................................................14-
  • The LU Factorization.................................................................................................................14-
  • The Cholesky Factorization .....................................................................................................14-
  • The QR Factorization...............................................................................................................14-
  • Singular Value Decomposition ................................................................................................14-
  • Summary ..................................................................................................................................14-
  • Exercises ..................................................................................................................................14-
  • Solutions to Exercises ..............................................................................................................14-
  • Chapter
  • The Bessel Function 15- Bessel, Legendre, and Chebyshev Functions
  • Legendre Functions 15-
  • Laguerre Polynomials................................................................................................................ 15-
  • Chebyshev Polynomials 15-
  • Summary 15-
  • Exercises.................................................................................................................................... 15-
  • Solutions to Exercises 15-
  • Chapter vi Numerical Analysis Using MATLAB and Spreadsheets, Second Edition
  • Linear Programming................................................................................................................... 16- Optimization Methods
  • Dynamic Programming............................................................................................................... 16-
  • Network Analysis 16-
  • Summary................................................................................................................................... 16-
  • Exercises 16-
  • Solutions to Exercises............................................................................................................... 16-

Numerical Analysis Using MATLAB and Spreadsheets, Second Edition 1-

Chapter 1

Introduction to MATLAB

his chapter is an introduction of the basic MATLAB commands and functions, procedures for naming and saving the user generated files, comment lines, access to MATLAB’s Editor/ Debugger, finding the roots of a polynomial, and making plots. Several examples are pro- vided with detailed explanations.

1.1 Command Window

To distinguish the screen displays from the user commands, important terms and MATLAB func- tions, we will use the following conventions: Click : Click the left button of the mouse Courier Font: Screen displays Helvetica Font: User inputs at MATLAB’s command window prompt EDU>>* Helvetica Bold: MATLAB functions Times Bold Italic: Important terms and facts, notes, and file names When we first start MATLAB, we see the toolbar on top of the command screen and the prompt EDU>>. This prompt is displayed also after execution of a command; MATLAB now waits for a new command from the user. We can use the Editor/Debugger to write our program, save it, and return to the command screen to execute the program as explained below. To use the Editor/Debugger:

  1. From the File menu on the toolbar, we choose New and click on M-File. This takes us to the Editor Window where we can type our code (list of statements) for a new file, or open a previ- ously saved file. We must save our program with a file name which starts with a letter. Impor- tant! MATLAB is case sensitive , that is, it distinguishes between upper- and lower-case letters. Thus, t and T are two different characters in MATLAB language. The files that we create are saved with the file name we use and the extension .m ; for example, myfile01.m. It is a good practice to save the code in a file name that is descriptive of our code content. For instance, if the code performs some matrix operations, we ought to name and save that file as matrices01.m or any other similar name. We should also use a separate disk to backup our files.

* EDU>> is the MATLAB prompt in the Student Version.

T

Numerical Analysis Using MATLAB and Spreadsheets, Second Edition 1-

Roots of Polynomials

In the example above, a multiplication () sign between and was not necessary because the complex number consists of numerical constants. However, if the imaginary part is a function or variable such as , we must use the multiplication sign, that is, we must type cos(x)j or j*cos(x).

1.2 Roots of Polynomials

In MATLAB, a polynomial is expressed as a row vector of the form. The ele- ments of this vector are the coefficients of the polynomial in descending order. We must include terms whose coefficients are zero. We can find the roots of any polynomial with the roots(p) function where p is a row vector con- taining the polynomial coefficients in descending order.

Example 1.

Find the roots of the polynomial

(1.1) Solution: The roots are found with the following two statements. We have denoted the polynomial as p1, and the roots as roots_ p1. p1=[1 −10 35 −50 24] % Specify the coefficients of p1(x) p1 = 1 - 10 35 - 50 24 roots_ p1=roots(p1) % Find the roots of p1(x) roots_p1 =

We observe that MATLAB displays the polynomial coefficients as a row vector, and the roots as a column vector.

Example 1.

Find the roots of the polynomial

(1.2)

4 j

cos ( ) x

[ a (^) n a (^) n – 1 a 2 a 1 a 0 ] a (^) i

p 1 ( ) x = x 4 – 10x 3 + 35x 2 – 50x+ 24

p 2 ( ) x = x 5 – 7x 4 + 16x 2 + 25x+ 52

Chapter 1 Introduction to MATLAB

1-4 Numerical Analysis Using MATLAB and Spreadsheets, Second Edition

Solution:

There is no cube term; therefore, we must enter zero as its coefficient. The roots are found with the statements below where we have defined the polynomial as p2, and the roots of this polynomial as roots_ p2. The result indicates that this polynomial has three real roots, and two complex roots. Of

course, complex roots always occur in complex conjugate *^ pairs.

p2=[1 − 7 0 16 25 52]

p2 = 1 -7 0 16 25 52

roots_ p2=roots(p2)

roots_ p2 =

-1. -0.3366 + 1.3202i -0.3366 - 1.3202i

1.3 Polynomial Construction from Known Roots

We can compute the coefficients of a polynomial from a given set of roots with the poly(r) func- tion where r is a row vector containing the roots.

Example 1.

It is known that the roots of a polynomial are. Compute the coefficients of this poly- nomial.

Solution:

We first define a row vector, say , with the given roots as elements of this vector; then, we find the coefficients with the poly(r) function as shown below.

r3=[1 2 3 4] % Specify the roots of the polynomial

r3 = 1 2 3 4

poly_r3=poly(r3) % Find the polynomial coefficients

poly_r3 = 1 -10 35 -50 24

* By definition, the conjugate of a complex number A = a + jbisA ∗^ = a – jb

1 2 3 , , , and 4

r

Chapter 1 Introduction to MATLAB

1-6 Numerical Analysis Using MATLAB and Spreadsheets, Second Edition

val_minus3 = 1280

Other MATLAB functions used with polynomials are the following:

conv(a,b) − multiplies two polynomials a and b

[q,r]=deconv(c,d) −divides polynomial c by polynomial d and displays the quotient q and remain- der r.

polyder(p) − produces the coefficients of the derivative of a polynomial p.

Example 1.

Let

(1.5)

Compute the product with the conv(a,b) function.

Solution:

p1=[1 − 3 0 5 7 9]; p2=[2 0 −8 0 4 10 12]; p1p2=conv(p1,p2)

p1p2 = 2 -6 -8 34 18 -24 -74 -88 78 166 174 108

Therefore,

We can write MATLAB statements in one line if we separate them by commas or semicolons. Commas will display the results whereas semicolons will suppress the display.

Example 1.

Let

(1.6)

Compute the quotient using the deconv(p,q) function.

Solution:

p3=[1 0 − 3 0 5 7 9]; p4=[2 − 8 0 0 4 10 12]; [q,r]=deconv(p3,p4)

p 1 = x 5 – 3x 4 + 5x 2 + 7x + 9

p 2 = 2x 6 – 8x 4 + 4x 2 + 10x + 12

p 1 ⋅ p (^2)

p 1 ⋅ p 2 2x 11 6x 10 8x 9

  • – 34x 8 18x 7 24x 6 = + + –
  • 74x 5 – 88x 4 + 78x 3 + 166x 2 + 174x + 108

p 3 = x 7 – 3x 5 + 5x 3 + 7x + 9

p 4 = 2x 6 – 8x 5 + 4x 2 + 10x + 12

p 3 ⁄ p (^4)

Numerical Analysis Using MATLAB and Spreadsheets, Second Edition 1-

Rational Polynomials

q =

r = 0 4 -3 0 3 2 3

Therefore, the quotient and remainder are

Example 1. Let (1.7)

Compute the derivative using the polyder(p) function.

Solution: p5=[2 0 − 8 0 4 10 12]; der_p5=polyder(p5) der_p5 = 12 0 -32 0 8 10 Therefore,

1.5 Rational Polynomials

Rational Polynomials are those which can be expressed in ratio form, that is, as

where some of the terms in the numerator and/or denominator may be zero. We can find the roots of the numerator and denominator with the roots(p) function as before.

Example 1. Let

(1.9)

Express the numerator and denominator in factored form, using the roots(p) function. Solution: num=[1 −3 0 5 7 9]; den=[2 0 −8 0 4 10 12];% Do not display num and den coefficients

q x ( ) r x ( )

q x ( ) = 0.5 r x ( ) = 4x 5 – 3x 4 + 3x 2 + 2x + 3

p 5 = 2x 6 – 8x 4 + 4x 2 + 10x + 12

dp 5 ⁄ dx

dp 5 ⁄ dx = 12x 5 – 32x 3 + 4x 2 + 8x + 10

R x ( ) Num x ( ) Den x ( )


b (^) n x n^ + b (^) n – 1 x n^ –^^1 + b (^) n – 2 x n^ –^^2 + … + b 1 x + b (^0) a (^) m x m^ + a (^) m – 1 x m^ –^^1 + a (^) m – 2 x m^ –^^2 + … + a 1 x + a (^0)

= = -------------------------------------------------------------------------------------------------------------------

R x ( )

p (^) num p (^) den

------------ x^

(^5) – 3x 4 + 5x 2 + 7x + 9

2x 6 – 8x 4 + 4x 2 + 10x + 12

= = -----------------------------------------------------------------

Numerical Analysis Using MATLAB and Spreadsheets, Second Edition 1-

Using MATLAB to Make Plots

1.6 Using MATLAB to Make Plots

Quite often, we want to plot a set of ordered pairs. This is a very easy task with the MATLAB plot(x,y) command which plots versus_._ Here, is the horizontal axis (abscissa) and is the vertical axis (ordinate).

Example 1.

Consider the electric circuit of Figure 1.1, where the radian frequency (radians/second) of the applied voltage was varied from to in steps of radians/second, while the amplitude was held constant. The ammeter readings were then recorded for each frequency. The magnitude of the impedance was computed as and the data were tabulated in Table 1.1.

Figure 1.1. Electric circuit for Example 1.

Plot the magnitude of the impedance, that is, versus radian frequency.

TABLE 1.1 Table for Example 1.

(rads/s) Ohms (rads/s) Ohms 300 39.339 1700 90. 400 52.589 1800 81. 500 71.184 1900 73. 600 97.665 2000 67. 700 140.437 2100 62. 800 222.182 2200 58. 900 436.056 2300 54. 1000 1014.938 2400 51. 1100 469.83 2500 48. 1200 266.032 2600 46. 1300 187.052 2700 44. 1400 145.751 2800 42. 1500 120.353 2900 40. 1600 103.111 3000 38.

y x x y

ω 300 3000 100

Z Z = V ⁄ A

` F

A <

1 V

R

L

C

ω Z ω Z

Z ω

Chapter 1 Introduction to MATLAB

1-10 Numerical Analysis Using MATLAB and Spreadsheets, Second Edition

Solution:

We cannot type (omega) in the MATLAB command window, so we will use the English letter w instead.

If a statement, or a row vector is too long to fit in one line, it can be continued to the next line by typing three or more periods, then pressing to start a new line, and continue to enter data. This is illustrated below for the data of w and z. Also, as mentioned before, we use the semi- colon (;) to suppress the display of numbers which we do not care to see on the screen.

The data are entered as follows:

w=[300 400 500 600 700 800 900 1000 1100 1200 1300 1400.... % Use 4 periods to continue 1500 1600 1700 1800 1900 2000 2100 2200 2300 2400 2500.... 2600 2700 2800 2900 3000]; % Use semicolon to suppress display of these numbers % z=[39.339 52.789 71.104 97.665 140.437 222.182 436.056.... 1014.938 469.830 266.032 187.052 145.751 120.353 103.111.... 90.603 81.088 73.588 67.513 62.481 58.240 54.611 51.468.... 48.717 46.286 44.122 42.182 40.432 38.845];

Of course, if we want to see the values of w or z or both, we simply type w or z, and we press .

To plot z ( ) versus w ( ), we use the plot(x,y) command. For this example, we use plot(w,z). When this command is executed, MATLAB displays the plot on MATLAB’s graph screen. This plot is shown in Figure 1.2.

Figure 1.2. Plot of impedance versus frequency for Example 1.

This plot is referred to as the amplitude frequency response of the circuit.

ω

y – axis x – axis

z ω

Chapter 1 Introduction to MATLAB

1-12 Numerical Analysis Using MATLAB and Spreadsheets, Second Edition

Figure 1.3. Modified frequency response plot of Figure 1.2.

By definition,

Therefore,

represents a power ratio of represents a power ratio of

It is very useful to remember that:

represents a power ratio of represents a power ratio of represents a power ratio of

Also,

represents a power ratio of approximately represents a power ratio of approximately represents a power ratio of approximately

From these, we can estimate other values. For instance,

and since and then,

dB 10 P (^) out P (^) in

= log ----------

10 dB 10 10n dB 10 n

20 dB 100 30 dB 1 000 , 60 dB 1 000 000 , ,

1 dB 1. 3 dB 2 7 dB 5

4 dB = 3 dB + 1 dB 3 dB ≅ power ratio of 2 1 dB ≅ power ratio of 1.

4 dB ≅ ratio of ( 2 × 1.25 )= ratio of 2.

Numerical Analysis Using MATLAB and Spreadsheets, Second Edition 1-

Using MATLAB to Make Plots

Likewise, and this is equivalent to a power ratio of approximately

Using the relations

and

if we let , the dB values for voltage and current ratios become

and

(1.12)

To display the voltage in a dB scale on the , we add the relation dB=20log10(v), and we replace the semilogx(w,z) command with semilogx(w,dB). The command gtext(‘string’) switches to the current Figure Window, and displays a cross-hair which can be moved around with the mouse. For instance, we can use the command gtext(‘Impedance |Z| versus Frequency’), and this will place a cross-hair in the Figure window. Then, using the mouse, we can move the cross-hair to the position where we want our label to begin, and we press . The command text(x,y,’string’ ) is similar to gtext(‘string’). It places a label on a plot in some specific location specified by x and y , and string is the label which we want to place at that loca- tion. We will illustrate its use with the following example which plots a 3-phase sinusoidal wave- form. The first line of the code below has the form linspace(first_value, last_value, number_of_values) This command specifies the number of data points but not the increments between data points. An alternate command uses the colon notation and has the format x=first: increment: last This format specifies the increments between points but not the number of data points. The code for the 3-phase plot is as follows: x=linspace(0, 2pi, 60); % pi is a built-in function in MATLAB; % we could have used x=0:0.02pi:2pi or x = (0: 0.02: 2)pi instead; y=sin(x); u=sin(x+2pi/3); v=sin(x+4*pi/3);

27 dB = 20 dB + 7 dB 100 × 5 = 500

y = log x 2 = 2 logx

P V^

2 Z

= ------ = I 2 Z

Z = 1

dB (^) v 10 V^ out V (^) in


2 log 20 V^ out V (^) in

= = log ----------

dB (^) i 10 I^ out I (^) in


2 log 20 I^ out I (^) in

= = log --------

v y – axis