Skip to main content
留学咨询

辅导案例-MATH 152

By May 15, 2020No Comments

MATH 152 Computer Lab 3 Instructions 1. Login to matlab.mathworks.com (or another MATLAB development environment). 2. Follow the examples and enter the commands listed below. The examples give you the tools you need to complete the exercises. 3. There are 2 files to submit to Canvas for this lab: (a) Submit a MATLAB .mat file called assignment3.mat containing variables: OneAnum, OneBmat, OneCnum and TwoBnum. (b) Submit a MATLAB .m file called parallel_circuit.m containing a MATLAB script which solves Exercise 2(a). 4. Visit the MATLAB Help Center for extra help. Learning Goals After completing this lab, you should be able to: ◦ Open, edit and run a MATLAB script. ◦ Create a new MATLAB script. ◦ Write a for loop to iterate MATLAB commands. ◦ Solve a linear system of equations representing loop currents in a circuit analysis. MATLAB Scripts and M-Files We quickly discover that entering commands one at a time in the Command Window is not very efficient especially when we need several commands to complete a task. To save and run many commands at the same time, we create a MATLAB script which is a sequence of MATLAB com- mands saved in an m-file that we can run all at once. To create a new script, click “New Script” in the menu bar, or type edit in the Command Window. For example, let’s create a MATLAB script called vector_angle which computes the angle between vectors v = (1, 0, 1, 2) and w = (3, 1,−1, 4): >> edit vector_angle.m A new editor window pops open and we type the commands in the file: 1 v = [1,0,1,2] w = [3,1,-1,4] % The dot product equations is v.w = |v| |w| cos(theta) theta = acos(dot(v,w)/(norm(v)*norm(w))) Note that we can write comments in the script to describe what the code is doing. This makes our code easier for others to read. MATLAB ignores all the lines that begin with the percent symbol %. Click “Save” in the Editor menu to save your work. Click “Run” in the Editor menu to execute all the commands in the file. Or enter the file name vector_angle in the Command Window: >> vector_angle v = 1 0 1 2 w = 3 1 -1 4 theta = 0.6670 Exercise 1 Consider the following resistor network R1 R2 V2 R5 V1 V3i3 i4 i1 i2 R3 R4 2 The loop current equations are R1i1 − V1 + R3(i1 − i3) = 0 R2i2 + R4(i2 − i4) + V1 = 0 R3(i3 − i1) + R5(i3 − i4) + V2 = 0 R4(i4 − i2)− V3 + R5(i4 − i3) = 0 and in matrix form R1 + R3 0 −R3 0 0 R2 + R4 0 −R4 −R3 0 R3 + R5 −R5 0 −R4 −R5 R4 + R5   i1 i2 i3 i4  =  V1 −V1 −V2 V3  (a) Our instructors have created a MATLAB script called network.m which computes the loop currents of the network. Download the MATLAB script from Canvas, upload/save the script to your MATLAB development environment and open the script. Change the values of the components in the script to R1 = R2 = 5, R3 = R4 = R5 = 7, V1 = V2 = 12 and V3 = 10. Run the script and save the value of the loop current i3 as OneAnum. (b) Modify the script again by using the digits of your student number to set the values of the components. For example, if your student number is 12345678 then set R1 = 1, R2 = 2, R3 = 3, R4 = 4, R5 = 5, V1 = 6, V2 = 7 and V3 = 8. Run the script and save the reduced row echelon form of the augmented matrix as OneBmat. (c) Set the values of the components to R1 = 1, R2 = 2, R3 = 3, R4 = 4, R5 = 5, V2 = 7 and V3 = 8. Use trial and error to find a value for the component V1 such all the loop currents are positive and save the value as OneCnum. for Loops Suppose we want to perform row operations on a matrix to make each entry in the first column equal to 1. We do this by dividing each row by its first entry (assuming that all these entries are nonzero). In other words, we want to run the commands: A(1,:) = A(1,:)/A(1,1) A(2,:) = A(2,:)/A(2,1) … … A(n,:) = A(n,:)/A(n,1) Here n is the number of rows in A. For example, let’s write a script which creates the matrix A =  2 2 3 4−1 3 1 3 3 1 1 2  and divides each row by the entry in the first column: 3 A = [2 2 3 4; -1 3 1 3; 3 1 1 2] A(1,:) = A(1,:)/A(1,1) A(2,:) = A(2,:)/A(2,1) A(3,:) = A(3,:)/A(3,1) Notice how the operations are repetitive. If the matrix A had 1000 rows, would we have to copy and paste 1000 commands? No, we should use a for loop to iterate the operation for each row. Let’s update the script to perform the same operations as the code above except now we implement a for loop: A = [2 2 3 4; -1 3 1 3; 3 1 1 2] for i = [1 2 3] A(i,:) = A(i,:)/A(i,1) end The for loop executes the command A(i,:) = A(i,:)/A(i,1) for i equal to each value in the vector [1 2 3]. We would like our code to work with any matrix and so we don’t want to assume that the matrix has 3 rows. Let’s modify our script to work with any size matrix. We begin by determining the number of rows and columns of A using the built-in function size. The command [n m] = size(A) finds the number of rows and columns of A has and assigns those numbers to n and m respectively. For example: >> [n,m] = size(A) n = 3 m = 4 Now that n is the number of rows of A, we can modify our script: A = [2 2 3 4; -1 3 1 3; 3 1 1 2] [n,m] = size(A) for i = [1:n] A(i,:) = A(i,:)/A(i,1) end Notice how the for loop works. The variable i is set equal to the first element of the vector [1:n] and the commands between the for statement and the end statement are executed with that value of i. After an iteration, i is set to the next value in the vector [1:n] and the same commands are 4 executed again with this updated i. This process is repeated until i has gone through all the values in the vector [1:n]. The end statement is crucial to end this loop and MATLAB will generate an error if it is missing. Run the script once more with a different matrix: A = [3 2 4; 5 3 2; 7 8 3; 9 3 6; 9 8 5; 5 3 4; 2 9 8] [n,m] = size(A) for i = [1:n] A(i,:) = A(i,:)/A(i,1) end Exercise 2 Consider the parallel circuit with N loop currents i1, . . . , iN , N identical voltage sources V and N + 1 identical resistors R V V V V R i1 R i2 R i3 R R iN R The loop current equations yield the following augmented matrix with N rows and N + 1 columns A =  2R −R 0 0 · · · 0 0 0 V −R 2R −R 0 0 0 0 V 0 −R 2R −R · · · 0 0 0 V … … . . . … … 0 0 0 0 · · · −R 2R −R V 0 0 0 0 · · · 0 −R 2R V  (a) Write a MATLAB script called parallel_circuit.m which performs the following tasks for the values N = 50, R = 1 and V = 1: i. Use the built-in MATLAB function zeros to create a matrix A of zeros with N rows and N + 1 columns. ii. Fill in the first and last rows of the matrix A with the values R and V described above. iii. Write a for loop: for each row i from 2 to N − 1, modify row i such that: 5 A(i,i-1) = -R; A(i,i) = 2*R; A(i,i+1) = -R; A(i,end) = V; iv. Use the built-in MATLAB function rref to compute the reduced row echelon form of A. v. Save the last column of rref(A) as currents. If written correctly, we can use our script to compute the loop currents and plot each value in a bar plot. The following commands should create the figure below: >> parallel_circuit >> bar(currents); xlabel(‘Loop’); ylabel(‘Current’); (b) Create a new copy of your script and modify the values N = 30 and V = 12. Use trial and error to find a value of the resistor R such that the maximum loop current in the circuit is 100. Save the value as TwoBnum. (Note: the built-in MATLAB function max computes the maximum value in a vector.) 6

admin

Author admin

More posts by admin