Dr. Milica Marković Applied Electromagnetics Laboratory page 1 EEE161 Applied Electromagnetics Laboratory 1 Instructor: Dr. Milica Marković Office: Riverside Hall 3028 Email: milica@csus.edu Web:http://gaia.ecs.csus.edu/ milica This laboratory exercise will introduce you to concepts in vectors and coordinate systems that will be used in electrostatics and magnetostatics. Introduction 1. Find the Matlab program by going to Start, All Programs, Matlab 2. When the Matlab window opens, you will see three windows: Command, Command History and Current Directory. Later, when we make graphs, you will also see the graphics window. Do not type the program commands directly into the Command window. Even though this is possible, you want to have the entire program in front of you, so that you can debug the program if necessary. Matlab has its own program editor. 3. To write your code in Matlab editor, select File, New, M-File from the Matlab pulldown menu. 4. Save the new file as yourname.m. Be careful when you name a matlab file: (a) Avoid all special characters. (b) Do not use space (c) Do not start the file name with a number. (d) When you start writing your code, do not name any variables the same as the name of your file. (e) Do not name it sin, cos, or any other function that is likely to already exist. 5. Start typing your code 6. It is a good practice to write comments for each line of your code. 7. The first line of code should have the explanation of what is the purpose of the program. 8. The second line of the code should be clear all command. This command clears workspace, command screen and all variables. 9. Matlab has excellent documentation. Look for Matlab help. 10. Laboratory report. Use Matlab to help you assemble the report! When you are done with one section of the lab, save the matlab file, then select File, Publish to, Word Document. (In some newer versions of Matlab, the default file type is HTML. You need to change the file type first under File...) Your matlab file, all the information from command window and all plots will be placed in the word document that has the same name as your m file. Be patient, as it takes about 30 seconds for matlab to open the word file. To submit your final report, you can edit this file and add comments, then put all the files that together that you worked on during the laboratory session.
Dr. Milica Marković Applied Electromagnetics Laboratory page 2 Using Matlab as a calculator Copy and paste the sample Matlab code below in the new file yourname.m you created in step above. The code below defines two constants a1 and a2 with values 8 and 4. Constants are then added, subtracted, multiplied, divided. Subsequently, exponent, logarithm, sine, cosine, tangent of numbers is found. Note that variable names MUST start with a letter. % Create two scalars and add/subtract/multiply/divide them a1=8 a2=4 a3=a1+a2 a4=a1-a2 a5=a1*a2 a6=a1/a2 % Exponent, Logarthm, Sine, Cosine of a number a7=a1^a2 %Natural Logarithm a8=log(a1) %Base 10 logarithm a9=log10(a1) %Sine of a number a10=sin(a1) %Cosine of a number a11=cos(a1) %Tangent of a number a12=tan(a1) Problem 1a. Write a code in Matlab to find y = cos e x if x=5. Create a new matlab file, write comments for each line of code. Problem 1b. Explore Matlab help, and see how Matlab differentiates between angles given in degrees and radians. Specifically, write a code to find cosine of an angle of 45 o, cos(x). Define a constant zdeg to be 45 o, then define another constant zrad to be π 4. The cosine value should be the same, whether you define an angle in degrees or radians. What did you have to do to make Matlab understand you defined an angle in degrees? Hint: Open Matlab help, by selecting Help, then Product Help from the main window. Help window should open. Select Search Results tab in Help window. Type degrees then radians in Search For:.
Dr. Milica Marković Applied Electromagnetics Laboratory page 3 Arrays In all of the above examples, MATLAB writes the solution of your equation in Command Window. Some calculations, such as intermediate steps, don t need to be displayed in the Command Window. Type semicolumn (;) at the end of the line of code, for which you do not want the result to be displayed. Matlab code below creates arrays b, c and d. We use arrays to display vectors in Matlab. Array b represents components of the vector b in 3-dimensions b = 3 a x + 4 a y + 5 a z. Note that unit vectors a x, a y or a z aren t writen anywhere explicitly. Just always write the x component of b first in the Matlab array, followed by y and z components. Subsequently, vector b is multiplied with a constant a1. Finally we can check which number is at a certain location in vector b. %Create array b b=[3, 4, 5] %Create two more arrays, these vectors could represent a 5D space. c=[6,2,1,9,7] d=[1,2,3,4,5] %Create a constant (scalar) a1 a1=5 %Multiply array b with a scalar a1 b1=a1*b %We can check which number is in the second location in vector d b(2) Problem 2. Write a code in Matlab to add vectors b = 3 x + 4 y and a = x + 2 y. Solve this problem first on paper. Create a new matlab file, write comments for each line of code. Remember that when adding and subtracting vectors, we add and subtract appropriate components. Plotting vectors in x-y plane A function in Matlab that plots vectors is called quiver. Syntax of quiver function is as follows: quiver(x, Y, U, V, scale) (1) In the above expression, (X, Y ) is the point of vector s beginning. U, V are the vector s components in x and y directions. Constant at the end of expression will scale the vector. If you want to see the vector in it s actual length, use scale=1 (or 0). Scale = 2 doubles their relative length, and scale = 0.5 halves the length. For example, to plot a position vector (vector) b = 3 x + 4 y in it s actual length, write: quiver(0,0,3,4,1)
Dr. Milica Marković Applied Electromagnetics Laboratory page 4 Problem 3a. Write a code in Matlab to plot vectors b = 3 x + 4 y and a = x + 2 y. Solve this problem first on paper. Create a new matlab file, write comments for each line of code. Problem 3b. Find the sum and difference of the vectors defined above. Plot the vectors and their sum, then vectors and their difference on two separate graphs. Plotting vectors in 3D An equivalent function to plot vectors in 3D is called quiver3. Syntax of quiver3 function is as follows: Quiver3(X, Y, Z, U, V, W, const) (2) In the above expression, (X, Y, Z) is the point of vector s origin (beginning). U, V, W are the vector s components in x, y and z directions. Constant at the end of expression will scale the vector when graphed. If you want to see the vector in it s actual length, use scale=1 (or 0). Scale = 2 doubles their relative length, and scale = 0.5 halves the length. Problem 4a. Write a code in Matlab to plot vectors b = 3 x + 4 y + 3 z and a = x + 2 y + 3 z. Solve this problem first on paper. Create a new matlab file, write comments for each line of code. Note that you should get a vector in 3-dimensions now. Problem 4b. Find the sum and difference of the vectors defined above. Plot the vectors and their sum, then vectors and their difference on two separate graphs. Creating Matrices Matrices are used extensively in electrical engineering. There are several ways to generate matrices in Matlab. In the example below several matrices are generated. The first matrix in the example Matlab file below is matrix g, followed by the explanation on how to generate matrices that contain all zeros. g = [ 1 2 3 4 5 6 %Here we generate a matrix $g$. Note that the new row is labeled by a semicolon sign g=[1 2 3; 4 5 6] ] %Here is two simple ways to generate a matrix 5x5 with all elements 0 aa=zeros(5) %We can also generate a 2x5 matrix of zeros aa1=zero(2,5) %OR generate a random values in a matrix aa2=rand(5)
a n1 x 1 + a n2 x 2 + + a nn x n = b n (4) Dr. Milica Marković Applied Electromagnetics Laboratory page 5 %If a matrix row (or any other command in matlab that is too long) has too many %elements, th h1=[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17] %we can type h2=[1 2 3 4 5 6 7 8,... 9 10 11 12 13 14 15 16; 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17] %We can check which number is in a certain location h2(2,2) %We can also change the value at a location h2(2,2)=10000 %We can also create a column vector from the matrix h2 using the colon operator x=h2(:,1) %or a row vector x=h2(1,:) Matrix multiplication Matrices are used to represent systems of linear equations. For example, a system of linear equations: a 11 x 1 + a 12 x 2 + + a 1n x n = b 1 (3) a 21 x 1 + a 22 x 2 + + a 2n x n = b 2... can be represented in matrix form as [A]{x} = {b} (5) where matrices [A], {X} and {B} are
Dr. Milica Marković Applied Electromagnetics Laboratory page 6 a 11 a 12 a 1n a 21 a 22 a 2n [A] =...... a n1 a n2 a nn x 1 x 2 {X} =. x n b 1 b 2 {B} =. b n (6) (7) (8) (9) In general, when two matrices [A] and [B] are multiplied, the result can be represented as n c ij = a ik b kj (10) In Matlab, we often define vectors. If you multiply two vectors {X} and {Y }, the result will be k=1 c = n a k b k (11) k=1 The other vector has to be a column vector and the first vector a row vector. If both of vectors are defined as row vectors you have to transpose one of them. In Matlab transpose operator is appostrophe. Matrix multiplication To multiply matrices, matrix dimensions must agree. For example, if we have two matrices A (2 x 3)and B (3 x 2) as given below: [ a11 a A = 12 a 13 a 21 a 22 a 23 ] B = Then the product of the two matrices C = AB is equal to b 11 b 12 b 21 b 22 b 31 b 32 [ a11 b C = 11 + a 12 b 21 + a 13 b 31 a 21 b 12 + a 22 b 22 + a 23 b 32 ] In Matlab the multiplication sign is used to multiply scalars, vectors or matrices.
Dr. Milica Marković Applied Electromagnetics Laboratory page 7 Problem 6. Multiply two matrices A and B given below, with normal matrix multiplication rules on paper. Then run the bottom Matlab program and compare results. %Define two matrices A and B A=[1 2 3; 4 5 6; 7 8 9] B=[10 11; 12 13; 14 15] %Multiply matrices C=A*B Multiplication of matrices element by element Often we want to multiply only element a 11 in matrix A with the element at the same position of matrix B b 11. In that case we have to write a dot in front of the multiplication sign.. The matrices have to be of the same size, for example 3x5. [ a11 a A = 12 a 13 a 21 a 22 a 23 ] [ b11 b B = 12 b 13 b 21 b 22 b 23 ] Then the product of two matrices element-by-element D = A. B will be [ a11 b D = 11 a 12 b 12 a 13 b 13 a 21 b 21 a 22 b 22 a 23 b 23 ] Problem 5 1. Use matrices to represent (on paper) the following system of equations 5x + 15y = 15 (12) 5x + 7y = 10 (13) Label clearly A, X and B matrices. Write code in Matlab to find the solution of this system using inverse matrix [A], X = [A] 1 B. Problem 7. Multiply two matrices A and B given below, with element-by-element matrix multiplication on paper. Then run the bottom Matlab program and compare results. %Define two matrices A and B A=[1 2 3; 4 5 6; 7 8 9]
Dr. Milica Marković Applied Electromagnetics Laboratory page 8 B=[10 11 12; 13 14 15; 16 17 18] %Multiply matrices D=A.*B Problem 8 Calculate the value of a function y = xcos(x) at points x = 0 : 0.1 : 100. Explain how will you do this in matlab? The first step is to generate the vector x. Then calculate the function y at all points x. Will you use matrix multiplication or dotted matrix multiplication? What do you get when you do matrix multiplication and what do you get when you use dotted matrix multiplication? Why? Making Plots It is often useful to represent data in graphical form. Data can be either the result of a simulation or actual data from the laboratory exercise. For example, you can measure voltage of a node in an electrical circuit over the range of frequencies. For example, you may get data that looks like this: Frequency [MHz] voltage [V] 1 10 2 5 3 1 4 0.5 It is very useful to see this data in a graphical form. If you specify the frequency as a vector b, and the voltage as a vector c, you can use the program below to plot voltage as a function of frequency. %%MAKING PLOTS %Plot vector c on y-axis as a function of b vector on x-axis %Define array of frequencies b=[1 2 3 4 ] %Define array of voltages c=[10 5 1 0.5] %Plot voltage vs. frequency plot(b,c) It is usually required that the plot is labeled: x-axis, y-axis, title etc. With the code below you can make the line width thicker, change the x-axis to log scale, add title etc. In addition, sometimes, it is easier to define a vector by defining start and end point and step. See how vector x is defined below. Start point of the vector is 0, end point is 2π and step is π/100. You can further explore Matlab by pressing F1 or selecting help from the bar, then select Demos tab, select Graphics and then 2-D plots. %Let s make the line of the plot thicker plot(b,c, LineWidth,4)
Dr. Milica Marković Applied Electromagnetics Laboratory page 9 %Let s change the line color plot(b,c, Color, red ) %Let s make a log scale on the x-axis semilogx(b,c) %Let s add a title to the plot and the labels for x and y axis and a grid plot(b,c), title( Experiment 1 ), xlabel( frequency ), ylabel( resistance ),grid %Plotting functions %first generate the vector of x-axis points from 0 to 2*pi with step pi/100 x=0:pi/100:2*pi %define a sin function of x y=sin(x) %plot the function plot(x,y) %Let s see some other interesting functions Matlab can make such as square %wave y1=square(x) plot(x,y1) %tangent y2=atan(x) plot(x,y2) %tangent in a better range of x-values x1=0:pi/100:pi/2-0.01*pi y3=tan(x1) plot(x1,y3) %What if we want to plot two functions on the same graph? plot(x,y) hold on plot(x,y1) %or to do two plots on the same graph we can write the following plot(x,y,x,y1) %above we can see that the matlab takes care of the graph colors
Dr. Milica Marković Applied Electromagnetics Laboratory page 10 Position Vector Position vector of a point (2,3,3) is shown in Figure 1, can be written in CCS as: A = A x a x + A y a y + A z a z (14) Figure 1: Position vector of point in Cartesian Coordinate System. Magnitude of the vector in Figure 1 is given as A = direction of A is given as: A 2 x + A 2 y + A 2 z, and unit vector a in the  = A A  = A x a x + A y a y + A z a z A 2 x + A 2 y + A 2 z (15) (16) Distance Vector A vector that represents a distance between two points r be in a Cartesian Coordinate system can be represented as a combination of two position vectors. The distance vector r be starts at point B and ends at point E, see Figure 2. Two position vectors can be assigned to these two points. As shown in Figure 2, the two position vectors are the position vector r b of the point B and the position vector r e of the end point E. Using these two position vectors, and vector subtraction, we can find the components of the vector r be. If the points B, E are given as B(b x, b y, b z ), E(e x, e y, e z ). Postion vectors r b and r e are r b = b x a x + b y a y + b z a z (17) r e = e x e x + e y a y + e z a z (18) (19)
Dr. Milica Marković Applied Electromagnetics Laboratory page 11 Vector r be is the difference between the two position vectors. r be = r e r b (20) r be = (e x b x ) e x + (e y b y ) a y + (e z b z ) a z (21) The unit vector and magnitude of the vector r be are r be = (e x b x ) 2 + (e y b y ) 2 + (e z b z ) 2 (22) rˆ be = (e x b x ) a x + (e y b y ) a y + (e z b z ) a z (23) (ex b x ) 2 + (e y b y ) 2 + (e z b z ) 2 Figure 2: Vector r be starts at a point B, and ends at a point E. Two position vectors r b and r e label the beginning and end of vector r be. Problem 9. Pick a point in Cartesian Coordinate System to represent the position of a charge q and another point away from the charge. For example: (1,2,3) and (1,4,5). Do not use these two points, pick your own points. Keep both points in the first quadrant, to simplify visualisation. Write Matlab code to find the distance vector R d between the charge q located at a point q(x 1, y 1, z 1 ), labeled with position vector R 1, and point P (x, y, z), labeled with a position vector P as shown in Figure 3. Use Matlab to plot the two position vectors R 1 and R and the distance vector R d between them. Subsequently, find the magnitude and unit vector in this direction, see Equations 22-23. Problem 10. Modify the code in the previous problem to show the electric field due to a single charge, positioned as shown in Figure 3. Assume that the charge is q 1 = 4 πɛ o C. Plot the position vectors, distance vectors and individual electric field vectors, than the total vector due to both charges. Use Equation 21 for distance vector. Use the first part of Equation 24 to plot the electric field. If you have to, scale the electric field vector in such a way that all vectors are visible. E = q 1 4πɛ 0 (x x1 ) 2 + (y y 1 ) 2 + (z z 1 ) 23 r a (24)
Dr. Milica Marković Applied Electromagnetics Laboratory page 12 Figure 3: Distance Vector. Problem 11. Modify the code in the previous problem to show the electric field due to two charges, positioned as shown in Figure 4. Assume that the charges are q 1 = 4 πɛ o C and q 2 = 8πɛ o. Pick the position of the second charge. Plot the position vectors, distance vectors and individual electric field vectors, than the total vector due to both charges, as shown in Figure 4. For two charges, as shown in Figure 4, the total field at a point is given by Equation 25. E = q 1 r q 2 4πɛ 0 (x x1 ) 2 + (y y 1 ) 2 + (z z 1 ) 23 a + r 4πɛ 0 (x x2 ) 2 + (y y 2 ) 2 + (z z 2 ) 23 b (25) Conclusion Figure 4: Electric Field due to two charges. Solve all of the above examples by hand and compare the results with the ones you found during the lab. In addition, for each of the problems, write what you have learned. Do not write an essay, just in a few sentences write what you have done in each problem, and specifically what you have learned from the work done. Due Date Submit the printout of the lab work and the conclusion by NEXT FRIDAY. LAB IS DUE BY noon (stamped) next Friday in the main office. No late labs. Do not bring previous lab writeup for the next Lab session.