0% found this document useful (0 votes)
17 views

Slip 21

The document contains Python code examples for numerical methods including generating multiplication tables, Newton's method, interpolation, and the regula falsi method. Code is provided to calculate eigenvalues and eigenvectors of a matrix. Student name and birthdate data is stored in a list. NumPy is used to generate identity and zero matrices.

Uploaded by

Dipesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Slip 21

The document contains Python code examples for numerical methods including generating multiplication tables, Newton's method, interpolation, and the regula falsi method. Code is provided to calculate eigenvalues and eigenvectors of a matrix. Student name and birthdate data is stored in a list. NumPy is used to generate identity and zero matrices.

Uploaded by

Dipesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Q1

1.write python code to display multiplication tables of number 20 to 30.


print(“\t\t\t\Multiplication Tables")
for i in range(20,31):
print(i,end="\t")
print()
print("____________________________________________________________")
for j in range(20,31):
for k in range(1,11):
print(j * k, end="\t")
print("\n")
3. Name of student and birthdate
list=list()
print("Enter names of 5 students in the list...")
for i in range(5):
list.append(input("Student: "))
for j in range(5):
list.append(input("birthdate:"))
print(“Name:”,list)
Q2.
1.python construct
import numpy as np
m1=np.ones([5,6])
print(m1)
m2=np.zeros([27,33])
print(m2)
m3=np.identity(5)
print(m3)

3.eigenvalues and eigenvectors


import numpy as np
A=np.array([[2,-1,-1,0],[-1,3,-1,-1],[-1,-1,3,-1],[-1,-1,-1,2]])
print(np.transpose(A))
print(np.linalg.det(A))
print(np.linalg.inv(A))

Q3.
a.
1.netwon raphson method
def f(x):
return x**5+3*x+1
def g(x):
return 5*x+3
def newtonRaphson(x0,e,N):
print("\n\n***NEWTON RAPHSON METHOD IMPLEMENTATION ***")
step=1
flag=1
condition=True
while condition:
if g(x0) == 0.0:
print("Divide by zero error!")
break
x1 = x0-f(x0)/g(x0)
print("Iteration-%d,x1=%0.6f and f(x1)=%0.6f"%(step,x1,f(x1)))
x0=x1
step=step+1
if step>N:
flag=0
break
condition=abs(f(x1))>e
if flag==1:
print("\nRequired root is: %0.8f" % x1)
else:
print("\nNot convergent.")
x0=input("enter guess: ")
e=input("Tolerable Error: ")
N=input("Maximum step: ")
N=int(N)
newtonRaphson(x0,e,N)

2. Interpolate
from scipy.interpolate import interp1d

X = [1,2,3,4,] # random x values


Y = [11,22,33,66] # random y values

# test value
interpolate_x = 3

# Finding the interpolation


y_interp = interp1d(X, Y)
print("Value of Y at x = {} is".format(interpolate_x),
y_interp(interpolate_x))

Q.3 (1) Newton raphson method


# Defining Function
def f(x):
return x**5 + 3*x + 1

# Defining derivative of function


def g(x):
return 5*x**4 + 3

# Implementing Newton Raphson Method

def newtonRaphson(x0,e,N):
print('\n\n*** NEWTON RAPHSON METHOD IMPLEMENTATION ***')
step = 1
flag = 1
condition = True
while condition:
if g(x0) == 0.0:
print('Divide by zero error!')
break

x1 = x0 - f(x0)/g(x0)
print('Iteration-%d, x1 = %0.6f and f(x1) = %0.6f' % (step, x1, f(x1)))
x0 = x1
step = step + 1

if step > N:
flag = 0
break

condition = abs(f(x1)) > e

if flag==1:
print('\nRequired root is: %0.8f' % x1)
else:
print('\nNot Convergent.')

x0 = input('Enter Guess: ')


e = input('Tolerable Error: ')
N = input('Maximum Step: ')

x0 = float(x0)
e = float(e)
N = int(N)

newtonRaphson(x0,e,N)

#Q.3 (b) Regula falsi method


# Defining Function
import math as mt
def f(x):
return x*mt.sin(x)+x*mt.cos(x)

# Implementing False Position Method


def falsePosition(x0,x1,e):
step = 1
print('\n\n*** FALSE POSITION METHOD IMPLEMENTATION ***')
condition = True
while condition:
x2 = x0 - (x1-x0) * f(x0)/( f(x1) - f(x0) )
print('Iteration-%d, x2 = %0.6f and f(x2) = %0.6f' % (step, x2, f(x2)))

if f(x0) * f(x2) < 0:


x1 = x2
else:
x0 = x2

step = step + 1
condition = abs(f(x2)) > e

print('\nRequired root is: %0.8f' % x2)

x0 = input('First Guess: ')


x1 = input('Second Guess: ')
e = input('Tolerable Error: ')

x0 = float(x0)
x1 = float(x1)
e = float(e)

if f(x0) * f(x1) > 0.0:


print('Given guess values do not bracket the root.')
print('Try Again with different guess values.')
else:
falsePosition(x0,x1,e)

You might also like