nakul bahmania 2013ch70163 wa2

3
Lab 2 Dividing Polynomials August 13, 2014 Nakul Bahmania 2013ch70163 group 5 Contents 1 Aim 1 2 Algorithm 1 3 Computer Code 1 4 Reading the input and output 3 5 Working with python3 3 1 Aim Divide a polynomial by (x-r1) and (x-r2) where r1,r2 are the roots of the poly- nomial. 2 Algorithm In this context, we are first taking a polynomial and multiplying it by (x-r1) and (x-r2) and then getting a polynomial with roots r1 and r2. This polynomial is what we want to divide with our quadratic equation. So, applying the method shown in the notes we get the list of coefficients of the outcome. 3 Computer Code roots=eval(input("list of roots")) ## you have to give the two roots in a list 1

Upload: nakul-bahmania

Post on 07-Feb-2016

6 views

Category:

Documents


0 download

DESCRIPTION

kinetics

TRANSCRIPT

Page 1: Nakul Bahmania 2013ch70163 WA2

Lab 2

Dividing Polynomials

August 13, 2014

Nakul Bahmania2013ch70163group 5

Contents

1 Aim 1

2 Algorithm 1

3 Computer Code 1

4 Reading the input and output 3

5 Working with python3 3

1 Aim

Divide a polynomial by (x-r1) and (x-r2) where r1,r2 are the roots of the poly-nomial.

2 Algorithm

In this context, we are first taking a polynomial and multiplying it by (x-r1) and(x-r2) and then getting a polynomial with roots r1 and r2. This polynomial iswhat we want to divide with our quadratic equation. So, applying the methodshown in the notes we get the list of coefficients of the outcome.

3 Computer Code

roots=eval(input("list of roots"))

## you have to give the two roots in a list

1

Page 2: Nakul Bahmania 2013ch70163 WA2

input="lab2in.txt"

fl=open(input,’r’)

data=fl.readline()

if data[-1]=="\n":

data=data[:-1]

data=data.split(",")

coeff=[int(i) for i in data]

def mult(a,b):

c=[]

for i in range(len(b)):

c.append(a*b[i])

return c

#print(mult(2,coeff))

def sub(a,b):

for i in range(len(a)):

b[i] = a[i]-b[i]

for i in range(len(a),len(b)):

b[i] = -b[i]

return b

#print(sub([1,2,3],[1,2,3,4]))

##multiplying part

d=coeff

for i in range(len(roots)):

e=[0]+mult(roots[i],d)

#print(e)

d=sub(d,e)

#print(d)

print(’multiplied polinomial:’,d)

##dividing part

e=[d[0]]

for i in range(len(roots)):

for j in range(len(d)-2):

v = d[j+1]+roots[i]*e[j]

#print(v)

e.append(v)

#print(e)

if i!=len(roots)-1:

d=e

e=[d[0]]

print(’divided polinomial:’,e)

2

Page 3: Nakul Bahmania 2013ch70163 WA2

out=’lab2out.txt’

fl=open(out,’w’)

fl.write(str(e))

fl.close()

4 Reading the input and output

First, the input file contains a list of coefficients in which power of x decrease aswe move ahead in the list. Means that if the list is [1,2,3], then it means yourinput is x2 + 2x + 3.

Second, the input for the roots of quadratic equation is given by user. Andif you give a list [1,1], then it means the roots are 1 and 1.

5 Working with python3

Where you save the programme file, you must create a txt file named ’lab2in.txt’in which you write some coefficients like 1,2,3,4,5 . and in the terminal, opendirectory and write python3 < filename >.it automatically creates an output file named ’lab2out’, in which the answer iswritten.

3