DO MY ASSIGNMENT SUBMIT
Sample: Python - Numeric Methods
Problem 1
from scipy.sparse import csr_matrix,lil_matrix
def printMatrix(A):
"""
Printing the Matrix
"""
print 'MATRIX'
for i in A:
row='|'
for j in i:
row += str(j).rjust(3)
print row+'|'
print
# Original Matrix
A=[[11,0,0,0,0,0,14], [0,0,0,0,23,0,0], [0,0,0,0,33,34,0], [0,15,0,0,43,44,0], [0,0,0,0,0,54,0],
[0,0,62,0,0,0,0], [0,0,72,0,0,0,0]]
#Printing the Matrix
printMatrix(A)
#Convert Matrix to csr format
mat = csr_matrix(A)
#Getting crs rows
IA = mat.indptr
JA = mat.indices
AA =mat.data
#output crs rows
print 'Crs representation rows'
print 'IA -',str(IA).rjust(3)
print 'JA -',str(JA).rjust(3)
print 'AA -',str(AA).rjust(3)
print
#Convert Matrix to lil format
lil_mat = mat.tolil()
#output lil rows
print 'LIL representation rows'
print lil_mat.data
print lil_mat.rows
W W W . A S S I G N M E N T E X P E R T. C O M
DO MY ASSIGNMENT SUBMIT
OUTPUT
MATRIX
| 11 0 0 0 0 0 14|
| 0 0 0 0 23 0 0|
| 0 0 0 0 33 34 0|
| 0 15 0 0 43 44 0|
| 0 0 0 0 0 54 0|
| 0 0 62 0 0 0 0|
| 0 0 72 0 0 0 0|
Crs representation rows
IA - [ 0 2 3 5 8 9 10 11]
JA - [0 6 4 4 5 1 4 5 5 2 2]
AA - [11 14 23 33 34 15 43 44 54 62 72]
LIL representation rows
[[11, 14] [23] [33, 34] [15, 43, 44] [54] [62] [72]]
[[0, 6] [4] [4, 5] [1, 4, 5] [5] [2] [2]]
Problem 2
a) Y=(5-x)*exp(x)-5
Code in function.py
W W W . A S S I G N M E N T E X P E R T. C O M
DO MY ASSIGNMENT SUBMIT
b) 4.96511423174
Code in secant.py
c) 4.96511423176
Code in newton.py
d)
Code in secant.py
Code in newton.py
W W W . A S S I G N M E N T E X P E R T. C O M
DO MY ASSIGNMENT SUBMIT
Problem 3
Here is the code
from math import log
def f(x):
return log((1+x)*1.0/(1-x*x))
def bisection(f,a,b):
eps = 10**(-8) # accuracy
max_steps = 100000
k=0
while abs(b-a)>eps:
x = (a+b)/2 #next guess
if( f(a)*f(x)>0 ):
a=x
else:
b=x
#k += 1 #if needed to restrict the number of iterations uncomment this line
if(k>max_steps):
break
return x
print bisection(f,-3.0/4,1.0/4)
Answer -7.45058059692e-09 (almost 0)
W W W . A S S I G N M E N T E X P E R T. C O M