physics

profileKalthabahi
f15_num_71.pdf

Numerical Problem Set 7

Physics 204B

October 26, 2015 Paul Arpin

1 Solving linear equations in Python

1.1 Objectives

• Learn how to use arrays to solve systems of equations using a numerical tool

1.2 Introduction

In this assignment your task is to use a numerical tool to solve for the currents in a circuit similar to one we saw in the written homework. I will provide directions for solving with the Numpy package in Python, but you are welcome to use any resource you would like (outside of your graphing calculator for this assignment). Please upload your pertinent file. If you use software that I do not have access to on my computer please provide a screen shot of your work and of your results. (In addition to Python I have access to Excel and Matlab.)

1.3 Matrices in Python

Using Numpy in Python we build matrices as arrays. For example to make this matrix:

A =

  1 1 −14 0 4

0 4 4

  (1)

I would simply type:

1 from numpy import ∗

3 A = array ([[1 ,1 , −1] , [ 4 , 0 , 4 ] ,

5 [ 0 , 4 , 4 ] ] )

Each set of numbers enclosed by one pair of brackets: [ ] represents one row in the matrix and rows are separated by a comma.

In order to build a horizontal vector (e.g. ~bh = (

0 −3 0 ) ) we only need one row:

1 from numpy import ∗

3 bhorizontal = array ( [ [ 0 , − 3 , 0 ] ] )

1

If I want a vertical vector (e.g.:

~b =

  0−3

0

  , (2)

which will generally be more useful in this assignment. I have two options that will give me identical answers – type out the entire vector or type out a horizontal vector and transpose it:

1 from numpy import ∗

3 b1 = array ( [ [ 0 ] , [ −3] ,

5 [ 0 ] ] )

7 print ’ b1 =’ , b1

9 bhorizontal = array ( [ [ 0 , − 3 , 0 ] ] )

11 b2 = transpose ( bhorizontal )

13 print ’ b2 =’ , b2

The final tool we need to solve systems of equations is the ability to invert a matrix. In order to do this with Python we have to load ‘inv’ from the Numpy linear algebra package. This can be done with: from numpy.linalg import inv

1 from numpy import ∗ from numpy . l i n a l g import inv

3

A = array ([[1 ,1 , −1] , 5 [ 4 , 0 , 4 ] ,

[ 0 , 4 , 4 ] ] ) 7

Ainv = inv (A) 9

print ’ Ainv =’ , Ainv

1.4 Solving Systems of Linear Equations

As discussed in class, we can solve a system of linear equations using matrices. In order to do this, we carefully lay out our set of equations to rewrite as a matrix multiplication problem. For example consider the following set of equations (you may recognize from class):

x1 + x2 − x3 = 0 (3) 4x1 + 4x3 = −3 (4)

4x2 + 4x3 = 0 (5)

2

I can rewrite this as a matrix multiplication problem A~x = ~b, where

A =

  1 1 −14 0 4

0 4 4

  , (6)

~x =

  x1x2

x3

  , and (7)

~b =

  0−3

0

  . (8)

As long as A is invertible, the solution is given by ~x = A−1~b. Matrix multiplication is handled in Numpy with dot(matrix1,matrix2):

from numpy import ∗ 2 from numpy . l i n a l g import inv

4 A = array ([[1 ,1 , −1] , [ 4 , 0 , 4 ] ,

6 [ 0 , 4 , 4 ] ] )

8 Ainv = inv (A)

10 bhorizontal = array ( [ [ 0 , − 3 , 0 ] ] )

12 b = transpose ( bhorizontal )

14 x = dot ( Ainv , b)

16 print ’x=’ ,x , ’Amps ’

3

2 Assignment

Please upload your document to Blackboard (using the naming scheme we have used all semester). You are welcome this week to work in Excel or Matlab as an alternative to Python. (Other numerical tools are an option, please submit screen shots of your work and results if you use software that I cannot open.)

Consider the following circuit:

R 1 R2

R 3

R 4 R5

A

B

C

loop 1

L2

L3

i0

E

Assume current i0 flows out of the battery, i1 through resistor R1, i2 in R2 and so on with the directions indicated in red.

Apply Kirchoff’s current law at junctions A, B, and C and Kircchoff’s Voltage Law at loops: loop 1, L2, and L3 to obtain 6 equations for the 6 unknown currents. Find the 6 currents assuming:

R1 = 100 Ω (9)

R2 = 300 Ω (10)

R3 = 500 Ω (11)

R4 = 200 Ω (12)

R5 = 400 Ω (13)

E = 10 V. (14)

1. Please include your name at the top of your document.

2. What are the 6 currents in the circuit listed in order?

3. If you are using Python please include raw input() as your last line.

4

  • Solving linear equations in Python
    • Objectives
    • Introduction
    • Matrices in Python
    • Solving Systems of Linear Equations
  • Assignment