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.
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.
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.f(x)
, to represent
3x - 4.
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 0When 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.
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
solve
and inv
commands from the
numpy.linalg
module and the dot
command from
numpy.
3(1 - 2y) - 9y = -42 3 - 6y - 9y = -42 3 - 15y = -42 -15y = -45 y = 3Since we have found y = 3, we can then find x = 1 - 2(3) = -5.
where
\[\begin{bmatrix} 3 & -9\\ 2 & 4 \end{bmatrix}\] \[\begin{bmatrix} x \\ y \end{bmatrix}\] = \[\begin{bmatrix} -42 \\ 2 \end{bmatrix}\]
A = \[\begin{bmatrix} 3 & -9\\ 2 & 4 \end{bmatrix}\] Z = \[\begin{bmatrix} x \\ y \end{bmatrix}\] and B = \[\begin{bmatrix} -42 \\ 2 \end{bmatrix}\]
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 BAlternatively, 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.
x - 2y - z = 6 2x + 2y = z + 1 2z - 1 = y + x
h(x,y)
, and
j(x,y)
for each of these.
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)
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))
soln
, the value of F2
at
soln
,
as well as the individual values of the functions h
and
j
at evaluated at soln
.
x + y2 = 4 ex + xy = 3