Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

In [1]:

#Q1a) sinx
import matplotlib.pyplot as plt
import sympy as sy
import scipy as sc
import numpy as np
x = np.arange(-2*np.pi,2*np.pi,0.1) # start,stop,step
y = np.sin(x)
plt.plot(x,y)
plt.xlabel('$x$')
plt.ylabel('$sinx$')
print("The graph is as follows:")

The graph is as follows:

In [2]:
#Q1b) tanx
x = np.linspace(-2 * np.pi, 2 * np.pi, 1000)
plt.plot(x, np.tan(x))
plt.ylim(-10, 10)
plt.xlabel('$x$')
plt.ylabel('$tanx$')
print("The graph is as follows:")

The graph is as follows:

In [3]:
#Q1c) coshx
in_array = np.linspace(-np.pi, np.pi, 1000)
out_array = np.cosh(in_array)
plt.plot(in_array, out_array)
plt.xlim(-10, 10)
plt.xlabel('$x$')
plt.ylabel('$coshx$')
print("The graph is as follows:")

The graph is as follows:

In [4]:
#Q1d) e^(-x^2)
x = np.linspace(-3, 3, 1000)
plt.plot(x, np.exp(-x**2))
plt.xlabel('$x$')
plt.ylabel('$-\exp(-x^2)$')
print("The graph is as follows:")

The graph is as follows:

In [5]:
#Q2a)
x = np.linspace(-4, 6, 1000)
fx = []
for i in range(len(x)):
fx.append(x[i]**3 - 4*x[i]**2 + 1)
plt.ylim(-10, 10)
plt.plot(x,fx)
plt.grid()
plt.axhline()
print("The graph is as follows:")

The graph is as follows:

In [7]:
#Q2b)
import sympy
from sympy import *
x = sympy.Symbol("x")
X = x*x*x -4*x*x + 1
k = [i.n(4, chop=True) for i in sympy.solveset(X)]
print("The roots are:")
k

The roots are:


Out[7]: [3.935, 0.5374, -0.4728]

In [8]:
#Q2c)
import sympy as sp
x = sp.Symbol('x')
l = sp.diff(x**3 - 4*x**2 + 1)
x = sp.Symbol("x")
X = l
k = [i.n(4, chop=True) for i in sp.solveset(X)]
print("Function has relative maximum at",k[0])
print("Function has relative minimum at",k[1])
print("Function is decreasing from -infinity to",k[0],"and from",k[1],"to infinity")
print("Function is decreasing from between",k[0],"and",k[1])

Function has relative maximum at 0


Function has relative minimum at 2.667
Function is decreasing from -infinity to 0 and from 2.667 to infinity
Function is decreasing from between 0 and 2.667

In [9]:
#Q3a)
from math import *
from sympy import *
from sympy import limit, Symbol
x = Symbol('x')
y= (x**2 + 3*x -4)/(x - 1)
print("The limit is:")
limit(y, x, 1)

The limit is:


Out[9]: 5

In [10]:
#Q3b)
from math import *
from sympy import *
from sympy import limit, Symbol
x = Symbol('x')
y= sin(x)/x
print("The limit is:")
limit(y, x, 0)

The limit is:


Out[10]: 1

In [11]:
#Q3c)
from math import *
from sympy import *
from sympy import limit, Symbol
x = Symbol('x')
y= x*ln(x)
print("The limit is:")
limit(y, x, 0)

The limit is:


Out[11]: 0

In [12]:
#Q4a)
from sympy import *
x = symbols('x')
expr = (x**2 + 5*x - 1)**100
print("Expression : {} ".format(expr))
# Use sympy.Derivative() method
expr_diff = Derivative(expr, x)
print("Derivative of expression with respect to x : {}".format(expr_diff))
print("Value of the derivative : {} ".format(expr_diff.doit()))

Expression : (x**2 + 5*x - 1)**100


Derivative of expression with respect to x : Derivative((x**2 + 5*x - 1)**100, x)
Value of the derivative : (200*x + 500)*(x**2 + 5*x - 1)**99

In [13]:
#Q4b)
from sympy import *
x = symbols('x')
expr = ((x**2)*exp(x) - 1)/(x**2 + 2)
print("Expression : {} ".format(expr))
# Use sympy.Derivative() method
expr_diff = Derivative(expr, x)
print("Derivative of expression with respect to x : {}".format(expr_diff))
print("Value of the derivative : {} ".format(expr_diff.doit()))

Expression : (x**2*exp(x) - 1)/(x**2 + 2)


Derivative of expression with respect to x : Derivative((x**2*exp(x) - 1)/(x**2 + 2), x)
Value of the derivative : -2*x*(x**2*exp(x) - 1)/(x**2 + 2)**2 + (x**2*exp(x) + 2*x*exp(x))/(x**2 + 2)

In [14]:
#Q4c)
from sympy import *
import numpy as np
x = symbols('x')
expr = (sin(x)**5)*(cos(x)**3)
print("Expression : {} ".format(expr))
# Use sympy.Derivative() method
expr_diff = Derivative(expr, x)
print("Derivative of expression with respect to x : {}".format(expr_diff))
print("Value of the derivative : {} ".format(expr_diff.doit()))

Expression : sin(x)**5*cos(x)**3
Derivative of expression with respect to x : Derivative(sin(x)**5*cos(x)**3, x)
Value of the derivative : -3*sin(x)**6*cos(x)**2 + 5*sin(x)**4*cos(x)**4

In [15]:
#Q4d)
from math import *
from sympy import *
import numpy as np
x = symbols('x')
expr = (atan(exp(x)))
print("Expression : {} ".format(expr))
# Use sympy.Derivative() method
expr_diff = Derivative(expr, x)
print("Derivative of expression with respect to x : {}".format(expr_diff))
print("Value of the derivative : {} ".format(expr_diff.doit()))

Expression : atan(exp(x))
Derivative of expression with respect to x : Derivative(atan(exp(x)), x)
Value of the derivative : exp(x)/(exp(2*x) + 1)

In [16]:
#Q5a)
from math import *
from sympy import *
init_printing(use_unicode=False, wrap_line=False)
x = Symbol('x')
integrate(((x**2) + 5*x - 1)**10*(2*x + 5),x)

Out[16]: x
22
38773235x
11
21 20 19 18 17 16 15 14 13 12 10 9 8 7
+ 5x + 124x + 1825x + 17630x + 116475x + 529485x + 1608150x + 2961405x + 2255425x − 1890542x − + 1890542x + 2255425x − 2961405x + 1608150x
11 11

6 5 4 3 2
− 529485x + 116475x − 17630x + 1825x − 124x + 5x

In [17]:
#Q5b)
from math import *
from sympy import *
init_printing(use_unicode=False, wrap_line=False)
x = Symbol('x')
integrate((x**5)*sqrt((1-x**2))**3,(x, 0, 1))

Out[17]: 8

315

In [18]:
#Q5c)
from scipy.integrate import quad
def f(x):
return (sin(x**3))
y = quad(f, 0, 1)
y[0]

Out[18]: 0.233845245593817

In [ ]:

You might also like