Python-Old-Version/C3/Solving-Equations /English

From Script | Spoken-Tutorial
Jump to: navigation, search

Welcome.

In this tutorial we shall look at solving linear equations, obtaining roots of polynomial and non-linear equations. In the process, we shall look at defining functions as well.

We would be using concepts related to arrays which we have covered in a previous tutorial.

Let's begin with solving linear equations. {show a slide of the equations} Consider the set of equations,

3x + 2y -z = 1
2x-2y + 4z = -2
-x+ 1/2 y-z = 0.

We shall use the solve function, to solve the given system of linear equations. Solve requires the coefficients and the constants to be in the form of matrices of the form Ax = b to solve the system of linear equations.

Lets start

$ ipython -pylab 

interpreter.

We begin by entering the coefficients and the constants as matrices.

A = array([[3,2,-1], 
           [2,-2,4],
           [-1, 0.5, -1]])

A is a 3X3 matrix of the coefficients of x, y and z

b = array([1, -2, 0])

Now, we can use the solve function to solve the given system.

x = solve(A, b)

Type x, to look at the solution obtained.

Equation is of the form Ax = b, so we verify the solution by obtaining a matrix product of A and x, and comparing it with b. As we have covered earlier that we should use the dot function here, and not the * operator.

Ax = dot(A, x)
Ax

The result Ax, doesn't look exactly like b, but if we carefully observe, we will see that it is the same as b. To save ourselves from all this trouble, we can use the allclose function.

allclose checks if two matrices are close enough to each other (with-in the specified tolerance level). Here we shall use the default tolerance level of the function.

allclose(Ax, b)

The function returns True, which implies that the product of A & x is very close to the value of b. This validates our solution x.

Let's move to finding the roots of a polynomial. We shall use the roots function for this.

The function requires an array of the coefficients of the polynomial in the descending order of powers. Consider the polynomial x^2-5x+6 = 0

coeffs = [1, -5, 6]
roots(coeffs)

As we can see, roots returns the result in an array. It even works for polynomials with imaginary roots.

roots([1, 1, 1])

As you can see, the roots of that equation are of the form a + bj

What if I want the solution of non linear equations? For that we use the fsolve function. In this tutorial, we shall use the equation sin(x)+cos^2(x). fsolve is not part of the pylab package which we imported at the beginning, so we will have to import it. It is part of scipy package. Let's import it using.

from scipy.optimize import fsolve

Now, let's look at the documentation of fsolve by typing fsolve?

fsolve?

As mentioned in documentation the first argument, func, is a python function that takes atleast one argument. So, we should now define a python function for the given mathematical expression sin(x)+cos^2(x).

The second argument, x0, is the initial estimate of the roots of the function. Based on this initial guess, fsolve returns a root.

Before, going ahead to get a root of the given expression, we shall first learn how to define a function in python. Let's define a function called f, which returns values of the given mathematical expression (sin(x)+cos^2(x)) for a each input.

def f(x):
    return sin(x)+cos(x)*cos(x)


hit the enter key to come out of function definition.

def, is a key word in python that tells the interpreter that a function definition is beginning. f, here, is the name of the function and x is the lone argument of the function. The whole definition of the function is done with in an indented block similar to the loops and conditional statements we have used in our earlier tutorials. Our function f has just one line in it's definition.

We can test our function, by calling it with an argument for which the output value is known, say x = 0. We can see that sin(x) + cos^2(x) has a value of 1, when x = 0.

Let's check our function definition, by calling it with 0 as an argument.

f(0)

We can see that the output is as expected.

Now, that we have our function, we can use fsolve to obtain a root of the expression sin(x)+cos^2(x). Recall that fsolve takes another argument, the initial guess. Let's use 0 as our initial guess.

fsolve(f, 0)

fsolve has returned a root of sin(x)+cos^2(x) that is close to 0.

That brings us to the end of this tutorial. We have covered solution of linear equations, finding roots of polynomials and non-linear equations. We have also learnt how to define functions and call them.

Thank you!

Contributors and Content Editors

Chandrika