Engineering and Scientific Computations Using MATLAB phần 4 ppt

23 334 0
Engineering and Scientific Computations Using MATLAB phần 4 ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter 3: MATLAB and Problem Solving 58 because the AA is an active variable. To remove AA from active variables, we type Chapter 3: MATLAB and Problem Solving 59 The size and length functions return the size and length of a vector or matrix. For example, >> E=magic(4); SE=size(E), LE=length(E) SE = LE = 4 4 4 Chapter 3: MTLAB and Problem Solving 60 Selective Indexing. The user may need to perform operations with certain elements of vectors and matrices. For example, let us change all negative elements of the vector or matrix to be positive. To perform this, we have Thus, y=-4andz=4.5. The system of equations y+2~=5 3y+4~=6 can be solved graphically. In particular, we have the following MATLAB statement: >> y=-10: z= (-y+5) /2 ; plot (y ho z= 1-3 Figure 3.10 illustrates that the solutions are y = - 4 and z = 4.5. Chapter 4 covers two- and three- dimensional plotting and graphics. The graphical solution is given just to illustrate and verifL the results. y+2~=5 3y+ 42 = 6 Figure 3.10. Graphical solution of the system of linear equations Chapter 3: MATLAB and Problem Solving 61 As another example, using MATLAB, let us solve the system of the third-order linear algebraic equations given by Ax=B,whereA= Our goal is to find x(xl, x2, and x3). We have Chapter 3: MATLAB and Problem Solving 62 Many numerical problems involve the application of inverse matrices. Finding inverse matrices in MATLAB is straightforward using the inv function. For matrix A= 0 4 0 , find the inverse matrix, calculate the eigenvalues, derive [: 1 :i B = 10A3A-', and find the determinant of B. Using the inv, eig, and det functions, we have ans = >> A=[l 2 3;O 4 0;5 6 71 ; B=lO*(AA *(-l); inv(A1, B, eig(B), det(B) -8.7500e-001 -1.2500e-001 3.7500 0 2.5000e-001 6.2500e-001 -1.2500e-001 -1.2500e-00 1.6000e+002 2.8000e+002 2.4000e+002 0 1.6000e+002 0 4.0000e+002 7.6000e+002 6.4000e+002 8.0816e+000 7.9192e+002 1.6000e+002 1.0240e+006 B= ans = ans = Chapter 3: MATLAB and Problem Solving 63 -0.875 -0.125 0.375 1, [160 280 2401 0.625 -0.125 -0.125 400 760 640 0.25 B = 0 160 0 , the eigenvalues of the matrix B are 8.1, 791.9, 160, and the determinant is 1024000. We performed the matrix multiplications. The entry-by-entry multiplication, instead of the usual matrix multiplication, can be performed using a dot before the multiplication operator; for example, a . * b . As an illustration, let us perform the entry-by-entry multiplication of two three-by-three matrices A= 4 5 6 and B= 100 1 100 .Wehave 123 10 10 10 [78d [ill] The random matrices, arrays, and number generation are accomplished using rand. Different sequences of random matrices, arrays, and numbers are produced for each execution. The complete description of the rand function is available as follows: >> help rand RAND Unif RAND('state',O) resets the generat RAND('state',J), for integer J, re RAND ( 'state', sum( 100*clock) ) resets it his generator can generate all the floa numbers in the MATLAB Version 4.x used random number generators with a single seed. Chapter 3: WTLAB and Problem Solving 64 RAND ( 'seed' , 0) and RAND ( 'seed', J) cause the MATLAB 4 generator to be used. RAND('seed') returns the current seed of the MATLAB 4 uniform generator. RAND('state',J) and RAND('state',S) cause the MATLAB 5 generator to be used. See also RANDN, SPRAND, SPRANDN, RANDPERM. The range of values generated by the rand function can be different from what is needed. R = Shifting + Scaling*rand ( ) , Therefore, scaling is necessary. The general expression for scaling and shifting is and the following example illustrates the application of the above formula: >> R=ones (5) +O. l*rand ('5) R= 1.0583 1.0226 1.0209 1.0568 1.0415 1.0423 1.0580 1.0380 1.0794 1.0305 1.0516 1.0760 1.0783 1.0059 1.0874 1.0334 1.0530 1.0681 1.0603 1.0015 1.0433 1.0641 1.0461 1.0050 1.0768 The average (mean) value can be found using the mean function. For vectors, mean(x) is the mean value of the elements in x. For matrices, mean()() is a row vector containing the mean value of each column. In contrast, for arrays, mean(x) is the mean value of the elements along the first non- singleton dimension of x. The median value is found by making use of the median function. For vectors, median(x) is the median value of the elements in x. For matrices, median()() is a row vector containing the median value of each column. For arrays, median(x) is the median value of the elements along the first nonsingleton dimension of x. >> R=rand (5), Rmean=mean (R) , Rmedian=median (R) , R= To illustrate the mean and median functions, the following example is introduced: 0.6756 0.1210 0.2548 0.2319 0.1909 0.6992 0.4508 0.8656 0.2393 0.8439 0.7275 0.7159 0.2324 0.0498 0.1739 0.4784 0.8928 0.8049 0.0784 0.1708 0.5548 0.2731 0.9084 0.6408 0.9943 0.6271 0.4907 0.6132 0.2480 0.4747 0.6756 0.4508 0.8049 0.2319 0.1909 mean = Rmedian = Thus, using rand, we generated the random 5 x 5 matrix R. Then, applying the mean and median functions, we found the mean and median values of each column of R. MATLAB has functions to round floating point numbers to integers. These functions are round, fix, ceil, and floor. The following illustrates the application of these functions: Chapter 3: A& TUB and Problem Solving 65 Symbols and Punctuation. The standard notations are used in MATLAB. To practice, type the examples given. The answers and comments are given in Table 3.1. Table 3.1. MATLAB Problems Problems with MATLAB syntaxes >> a=2+3 >> 2+3 >> a=2*3 >> 2*3 >> a=sqrt (5*5) >> sqrt(5*5) >> a=1+2j; b=3+4j; c=a*b >> a=[O 2 4 6 8 101 >> a=[0:2:10] >> a=0:2:10 >> a(:)' >> a=[0:2:10]; >> b=a(:)*a(:)' >> b(3,4) >> b(3,:) >> c=b(:,4:5) Answers a=5 ans = 5 a=6 ans = 6 a=5 ans = 5 c = -5.0000 +lO.OOOOi a=O 2 4 6 8 10 a=O 2 4 6 8 10 a=O 2 4 6 8 10 a=O 2 4 6 8 10 c= 0 0 0 0 0 0 0 4 8 12 16 20 0 8 16 24 32 40 0 12 24 36 48 60 0 16 32 48 64 80 0 20 40 60 80 100 ans = 24 ans = 0 8 16 24 32 40 c= 0 0 12 16 24 32 36 48 48 64 60 80 ans = 220 Comments MATLAB arithmetic Complex variables Vectors Forming matrix b Element (3,4) Third row Forming a new matrix MATLAB Operators, Characters, Relations, and Logics. It was demonstrated how to use summation, subtractions, multiplications, etc. We can use the relational and logical operators. In particular, we can apply 1 to symbolize "true" and 0 to symbolize "false." The MATLAB operators and special characters are listed below. Chapter 3: MATLAB and Problem Solving 66 For example, the MATLAB operators &, 1, - stand for "logical AND", "logical OR", and "logical NOT". Chapter 3: MTLAB and Problem Solving 67 The operators == and -= check for equality. Let us illustrate the application of == using two [...]... +61x3 + 8 2 x 2 + 6 7 ~ + 4 0 Chapter 3: MATLAB Problem Solving 70 and at the points 0, 3,6,9, and 12 This problem has a straightforward solution using the polyval function We have >> x= [ O :3 :121 ; y=polyval (p3, x) Y = 148 57 40 49 6090 46 247 71 23591212 Thus, the values of p3(x)at x = 0,3,9, and 12 are 40 , 148 57 ,49 6090 ,46 247 7 1, and 2359 1212 As illustrated, MATLAB enables different operations with... that p4(x) = 3 6x6 + 1 9x5 + 40 x4 + 61x3+ 82x2+ 67x + 40 = 6x2 + 7 x + 8 = p,(x) P I x4 +2x3+3x2+ 4 x + 5 This can be verified using the deconv function In particular, we have >> p4=deconv(p3, pl) p4 = 6 7 8 Comprehensive analysis can be performed using the MATLABpolynomial algebra and numerics For example, let us evaluate the polynomial P , ( X ) = 6 x 6 +19x5 + 4 0 x 4 +61x3 + 8 2 x 2 + 6 7 ~ + 4 0... motion device Weuse i = [ O 0.2 0 .4 0.6 0.8 1 1.2 1 .4 1.6 1.8 21 and 0 =[0 0.022 0. 044 0.065 0.0 84 0.1 0.113 0.123 0.131 0.136 0.1381 Chapter 3: MATLAB Problem Solving 72 and Using MATLAB, curve fitting, interpolation, and approximation can be performed (see the script and the results) MATLASScriDt Chapter 3: MATLAB Problem Solving and 73 Figure 3.12 plots the data, and the variations of the inductance... conv and deconv perform convolution and deconvolution (polynomial multiplication and division) Consider two polynomials p , ( x ) = x 4 + 2 x 3 + 3 x 2 + 4 ~ + and p 2 ( x ) = 6 x 2+ 7 ~ + 8 5 To findp3(x) = pl(x)p2(x) we use the conv function In particular, >> pl=[l 2 3 4 5 ; p2=[6 7 81; p3=conv(pl,p2) 1 p3 = 6 19 40 61 82 67 40 Thus, we find p , ( x ) = 6 ~ ~ + 1 9 ~ ~ 6+1 4 30+~ 2 x 2+ 6 7 ~ + 4 0... p=polyfit(x,y,3) P = 0.0010 -0.1 040 0. 847 8 1.2028 Thus, one obtains p(x) = 0.001x3- 0 104x2+ 0. 847 8~ 1.2028 + The resulting plots, plotted using the following MATLAB statement, >> plot(x,y,' - i , x , p o l y v a l ( p , x ) , ' : I ) of the data and its approximation by the polynomial are illustrated in Figure 3.1 1 161 4 ~ 121 0 1 2 3 4 5 6 7 6 9 Figure3.1l.Plotsofthexydata(x=[O 1 2 3 4 5 6 7 8 9 ] a n d y... interpolation, and approximation 3.5 Conditions and Loops The logical operators in MATLAB , =, == (logical equals), and -= (not equal) These are are binary operators which return the values 0 and 1 (for scalar arguments) To illustrate them, we have Chapter 3: MATLAB Problem Solving 74 and These logical operators have limited features, and therefore, loops, conditions, control statements, and control...Chapter 3: MATLAB Problem Solving 68 and 69 Chapter 3: MATLAB Problem Solving and Polynomial Analysis Polynomial analysis, curve fitting, and interpolation are easily performed We consider a polynomial p ( x ) = 1 OX" + 9x9 + 8x8 + 7 x 7+ 6x4 + 5x5 + 4x4 + 3x3 + 2x2 + IX + 0.5 To find the roots, we use the roots function: >> p=[10 9 8 7 6 5 4 3 2 1 0.51; roots(p) ans = 0.6192... 0.2166i -0.7159 - 0.2166i -0 .46 89 + 0.55973 -0 .46 89 - 0.5597i 0.2298 + 0.689Oi 0.2298 - 0.68903 -0.1 142 + 0.6913i -0.1 142 - 0.6913i As illustrated, different formats are supported by MATLAB. The numerical values are displayed in 15-digit fixed and 15-digit floating-point formats if format long and format long e are used, respectively Five digits are displayed if format short and format short e are assigned... 0 + + and its approximation by p(x) = 0 0 0 1 ~ ~ - 104x2 0. 847 8~ 1.2028 Interpolation (estimation of values that lie between known data points) is important in signal and image processing MATLAB implements a number of interpolation methods that balance the smoothness of the data fit with the execution speed and memory usage The one-, two- and three-dimensional interpolations are performed by using. .. is positive (and, if a>O, x=a3) ,and if a is not positive, it checks whether a is zero (if this is true, x = j = f i ) Then, if a is not zero, it does the else clause, and if aO x=a" 3 : e l s e i f a==O , x=j: else x=-a "4 : end: x gives x = 8 Chapter 3: MATLAB Problem Solving 78 and while a=O; i f a>O x=a"3; e l s e i f a==O, x=j; else end; x x=-a "4; results in . 0 .45 08 0.8656 0.2393 0. 843 9 0.7275 0.7159 0.23 24 0. 049 8 0.1739 0 .47 84 0.8928 0.8 049 0.07 84 0.1708 0.5 548 0.2731 0.90 84 0. 640 8 0.9 943 0.6271 0 .49 07 0.6132 0. 248 0 0 .47 47 0.6756 0 .45 08 0.8 049 . 0 0 4 8 12 16 20 0 8 16 24 32 40 0 12 24 36 48 60 0 16 32 48 64 80 0 20 40 60 80 100 ans = 24 ans = 0 8 16 24 32 40 c= 0 0 12 16 24 32 36 48 48 64 60. l*rand ('5) R= 1.0583 1.0226 1.0209 1.0568 1. 041 5 1. 042 3 1.0580 1.0380 1.07 94 1.0305 1.0516 1.0760 1.0783 1.0059 1.08 74 1.03 34 1.0530 1.0681 1.0603 1.0015 1. 043 3 1.0 641 1. 046 1

Ngày đăng: 14/08/2014, 06:22

Từ khóa liên quan

Mục lục

  • page_58.pdf

  • page_59.pdf

  • page_60.pdf

  • page_61.pdf

  • page_62.pdf

  • page_63.pdf

  • page_64.pdf

  • page_65.pdf

  • page_66.pdf

  • page_67.pdf

  • page_68.pdf

  • page_69.pdf

  • page_70.pdf

  • page_71.pdf

  • page_72.pdf

  • page_73.pdf

  • page_74.pdf

  • page_75.pdf

  • page_76.pdf

  • page_77.pdf

Tài liệu cùng người dùng

Tài liệu liên quan