Lab: Solving Equations

 


Introduction

The purpose of this lab is to become familiar with the tools for solving different types of equations, using the optimize module from the SciPy library, and the linalg module of the NumPy library for Python.



Solutions of Equations in a Single Variable

In this section, we will learn how to solve equations with a single variable, by using the fsolve command from the scipy.optimize module. To use this command, import it as from scipy.optimize import fsolve. Since fsolve is the only command we are using from this module, it makes sense just to import it by itself; we don't need the entire module.
  1. In the Editor in Spyder, create a new file for the functions you will write in this lab. Then save the new file with a name representative of this lab.
  2. To solve the equation 3x + 3 = 7 for x, we can find the solution of the equation 3x - 4 = 0. Create a function f(x), to represent 3x - 4.

  3. To use the fsolve command, we pass in the function we would like to solve, as well as an initial guess for the root of that function. So to solve 3x -4 = 0, we would write the statement:
    x = fsolve(f, 2),
    where 2 is our initial guess for the solution. We could then print the solution, and the result of checking our solution, as in:
              x = fsolve(f, 2)
              print("x =", x)       # print the solution
              print("f(x) =",f(x))  # print a "check" that this soln makes f 0
              
    When we do this, we should expect to see either 0, or a value really close to 0 for f(x). Copy this code and test this solution.

  4. Write the code to solve the equaton 3x2 + 7x = 5. Remember, this is a quadratic equation, so unless the function is of the form ax2, it will have two solutions. You should find them both. Your code should include checks that your solutions are correct.
    Stop and Think: How do you figure out what to use for an initial guess? One way to do it is to graph the function and look where it crosses the x-axis. Go ahead and plot this new function to get an idea for the initial guesses. You can use this graphic calculator if you don't want to use pyplot

Solving Systems of Equations

In this section, we will learn how to solve systems of equations. We will see how to use the solve and inv commands from the numpy.linalg module and the dot command from numpy.

Systems of Linear Equations

  1. Let's first consider a system of 2 linear equations with 2 unknowns, such as 3x - 9y = -42, and 2x + 4y = 2. If we were solving this by hand, we might rewrite the second equation as 2x = 2 - 4y, which gives us x = 1 - 2y. We could then substitute for x in the first equation, to get:
            3(1 - 2y) - 9y = -42
            3 - 6y - 9y = -42
            3 - 15y = -42
            -15y = -45
            y = 3
        
    Since we have found y = 3, we can then find x = 1 - 2(3) = -5.

  2. We could use a method like this to solve any system of two equations in two unknowns, but we could also turn to linear algebra to do this. We can rewrite these two equations in terms of matrices:
    \[\begin{bmatrix} 3 & -9\\ 2 & 4 \end{bmatrix}\] \[\begin{bmatrix} x \\ y \end{bmatrix}\]   = \[\begin{bmatrix} -42 \\ 2 \end{bmatrix}\]
    where
    A = \[\begin{bmatrix} 3 & -9\\ 2 & 4 \end{bmatrix}\]
    Z = \[\begin{bmatrix} x \\ y \end{bmatrix}\]
    and B = \[\begin{bmatrix} -42 \\ 2 \end{bmatrix}\]

  3. To find a solution to this system, we could use the following code:
        A = np.array([[3, -9], [2,4]])
        B = np.array([-42, 2])
        Z = np.linalg.solve(A, B)
        
    Then, to check our work, we would write:
        print("Z =", Z)
        print(np.dot(A,Z))   # The result here should be B
        
    Alternatively, we could solve this system by computing Z = A-1*B, as in:
        Z = np.dot(np.linalg.inv(A), B)
        print(Z)
        
    This alternative method uses the dot command, which is used to multiply two matrices together. The inv command finds the multiplicative inverse of a matrix.

  4. Write code to find a solution to the following system of linear equations:
        x - 2y - z = 6
        2x + 2y = z + 1
        2z - 1 = y + x
        

Systems of non-linear equations

  1. Now let's consider solving a system of 2 non-linear equations, such as x + 2y3 = 0 and sin(x)/y = 0.

  2. Define functions, say h(x,y), and j(x,y) for each of these.

  3. Then define a function F2 that takes a list of 2 params as input, and then returns a list of the 2 values, i.e., the two functions evaluated at the specified parameters. The code should look like the following:
        def F2(params):
            x, y = params
            return h(x, y), j(x, y)
        

  4. Now we can use fsolve as above, to solve this system. As before, we pass in the function we want to solve, F2, and the initial guesses. So, our code would look like:
        soln = fsolve(F2,(1.0,1.0))
        

  5. Print out soln, the value of F2 at soln, as well as the individual values of the functions h and j at evaluated at soln.

  6. Write code to solve the following system of equations:
        x + y2 = 4
        ex + xy = 3
        

Submit