Chapter 14 Quardartic Equations

Example 14_1 pgno:168

In [6]:
#x^2-x-1=0
import numpy

x=('x')
y=numpy.array([1, -1, -1])# y=0
print"the solution is \n"

x=numpy.roots(y)
print x
the solution is 

[ 1.61803399 -0.61803399]

Example 14_2 pgno:168

In [7]:
#3*x^2-5*x+1=0
import numpy
x=('x');
y=([3, -5, 1]);# y=0
print"the solution is \n"

x=numpy.roots(y)

print x
the solution is 

[ 1.43425855  0.23240812]

Example 14_3 pgno:168

In [9]:
import numpy
x=('x')
p1='1/(x-1)';
p2='1/(x+2)';
y='p1-p2';
y1=1./16.;
a=numer(y)*denom(y1);
b=numer(y1)*denom(y);
r=a-b;
print"the solution is \n"
x=numpy.roots([-1, -1, 8])
print 2*x
the solution is 

[-6.74456265  4.74456265]

Example 14_4 pgno:171

In [10]:
#x^2-2*x-15=0

import numpy
x=('x');
y=([1, -2, -15]);# y=0
print"the solution  is  \n"
print("x=,",numpy.roots(y))
the solution  is  

('x=,', array([ 5., -3.]))

Example 14_5 pgno:171

In [11]:
#9*x*(x+1)=4

import numpy
x=('x')
y=[9, 9, -4]#9*x*(x+1)-4; #y=0
print"the solution  is  \n"
print("x=, \n",numpy.roots(y))
the solution  is  

('x=, \n', array([-1.33333333,  0.33333333]))

Example 14_6 pgno:173

In [12]:
#5*x**2+9*x-2=0

from numpy import sqrt
x=('x');
y='5*x**2+9*x-2';
a=5;b=9;c=-2;#from equation we get these values
#using the formula - solution of quadratic equation ax**2+bx+c=0
x=(-b+sqrt(b**2-4*a*c))/(2*a);
print("\t x= \n  \n or  ",x)
x=(-b-sqrt(b**2-4*a*c))/(2*a);
print(" x=",x)

 
('\t x= \n  \n or  ', 0.20000000000000001)
(' x=', -2.0)

Example 14_7 pgno:174

In [14]:
from math import sqrt

x=('x');
p1='1/(x-1)';
p2=2./3.;
p3='2/(x-3)';
p='(p1+p2-p3)';
p=3*numer(p);#As p=0 and to remove fractions, multiply by 3
a=2;b=-11;c=3;#from equation we get these values
#using the formula - solution of quadratic equation ax**2+bx+c=0
print("the solution is")

x=(-b+sqrt(b**2-4*a*c))/(2*a)
print x
print("or \n")
x=(-b-sqrt(b**2-4*a*c))/(2*a)
print round(x,3)
the solution is
5.21221445045
or 

0.288

Example 14_8 pgno:

In [15]:
#given u=160,g=10,h=240

from math import sqrt
#using the formulae "h=u*t-(g*t**2)/2"
u=160;
g=10;
h=240;
t=('t');
r='(240-u*t+(g*t**2)/2)'#u*t-(g*t**2)/2-h=0
a=5;b=-160;c=240;#from equation we get these values
#using the formulae - solution of quadratic equation ax**2+bx+c=0
t=(-b+sqrt(b**2-4*a*c))/(2*a);
t1=(-b-sqrt(b**2-4*a*c))/(2*a);
print("\n the solution  is  t= or t=\n",round(t,3),round(t1,3))#the answer given in textbook is wrong
('\n the solution  is  t= or t=\n', 30.422, 1.578)

Example 14_9 pgno:176

In [17]:
import numpy
#let x km/hr is avg. speed for 1st journey
#as velocity=distance/time, time for 1st journey is 84/x hrs
#speed for return journey is 84/(x+4).from given data, this is <1/2 hr than the 1st time 
x=('x');
#In algebraic form,(84/x)-(84/(x+4))=1/2
y='(84/x)-(84/(x+4))-1/2'; #y=0. so, numerator=0
x=numpy.roots([-1, -4, 8])
#x=roots(numer(y));
#velocity can't be in negatives.take +ve root
print("avg. speed for 1st journey is x=24km/h")
distance=84;#given
velocity=24;#found
time=distance/velocity;#time for 1st journey
time1=distance/(velocity+4);#time for 2nd journey
print("total_timefhours",time+time1)
avg. speed for 1st journey is x=24km/h
('total_timefhours', 6)

Example 14_10 pgno:179

In [19]:
#x+y=1, 38x^2-x*y+y^2=37

x=('x');
y='1-x';
#substitute y=1-x in equ. 38x^2-x*y+y^2=37
Y='3*x**2-x*(1-x)+(1-x)**2-37';
x=roots(Y);
#y=1-x;
print('the solutions are: \n')
print(x,y)
the solutions are: 

({-12/5: 1, 3: 1}, '1-x')

Example 14_11 pgno:180

In [20]:
#x+y=19, xy=84

x=('x');
#substitute y=19-x in xy=84
Y='x*(19-x)-84';
x=roots(Y);

print('the solutions are: \n')
print("(x,y)=() \n",x)
print 'y=19-x'
the solutions are: 

('(x,y)=() \n', {12: 1, 7: 1})
y=19-x

Example 14_12 pgno:181

In [21]:
#x**2+y**2=89, xy=40

import numpy
x=('x');
#substitute y=40/x in x**2+y**2=89
Y='x**2+(40/x)**2-89';
x=numpy.roots([1, 0, -89, 0, -1600]);#Y=0, numerator=0
y=roots(89-x**2);
print('the solutions of (x,y) are: \n')
print [x,y]
the solutions of (x,y) are: 

[array([ -1.02144599e+01+0.j        ,   1.02144599e+01+0.j        ,
         1.11022302e-16+3.91601715j,   1.11022302e-16-3.91601715j]), {}]