Assignment 1
Assignment 1
1(a)
import math
def f_prime(x):
return f(x) * (math.log(p) - p * math.cos(x**p) * x**(p-1))
def f_double_prime(x):
return f(x) * ((math.log(p))**2 - p**2 * x**(2*(p-1)) *
math.sin(x**p) - p * math.cos(x**p) * (1 - p * x**(2*(p-1))))
def g_prime(x):
return (p * (1 / math.cos(x))**2 * math.tan(x)**(p-1) - 2 *
math.cos(x) * math.sin(x)) / (1 + math.cos(x)**2)**2
def g_double_prime(x):
return (2 * math.tan(x)**(p-1) * (1 / math.cos(x))**4 * (p-1)
+ 2 * math.tan(x)**(p-2) * (1 / math.cos(x))**4 * p * math.sin(x)
- 4 * math.sin(x) * math.cos(x) * math.tan(x)**(p-1)) / (1 +
math.cos(x)**2)**3
f_prime_pi_3 = f_prime(x_pi_3)
f_prime_2pi_3 = f_prime(x_2pi_3)
f_double_prime_pi_3 = f_double_prime(x_pi_3)
f_double_prime_2pi_3 = f_double_prime(x_2pi_3)
g_prime_pi_3 = g_prime(x_pi_3)
g_prime_2pi_3 = g_prime(x_2pi_3)
g_double_prime_pi_3 = g_double_prime(x_pi_3)
g_double_prime_2pi_3 = g_double_prime(x_2pi_3)
print("\nFor g(x):")
print("g'(π/3) =", g_prime_pi_3)
print("g'(2π/3) =", g_prime_2pi_3)
print("g''(π/3) =", g_double_prime_pi_3)
print("g''(2π/3) =", g_double_prime_2pi_3)
Output
For f(x):
f'(π/3) = -0.317442881548246
f'(2π/3) = 109.55765035646829
f''(π/3) = -6.926725711642238
f''(2π/3) = -1616.8669199407718
For g(x):
g'(π/3) = 22.485743741577934
g'(2π/3) = 23.5942562584221
g''(π/3) = 169.37156995957397
g''(2π/3) = 27.236430040425915
import sympy as sp
# Define symbols
x = sp.symbols('x')
p = 3
q = 1
# Define f
f = y2 + 3*y1 - q*y
# Substitute p and q values
f = f.subs({p: 3, q: 1})
Output
Expression for f(x) is: -10*sin(3*x) + 9*cos(3*x)
(b)
import numpy as np
import matplotlib.pyplot as plt
# Define symbols
x = sp.symbols('x')
# Define f
f = y2 + 3*y1 - q*y
Output
Extrema in the interval [-3π/2, 5π/2]: [0.523598775598299,
1.57079632679490]
Answer to the question no. 2(a)
import sympy as sp
Output
Laplacian of phi at point (2, -1, 1) is: 12*a - 2*b - 6*c
import scipy.special
result = scipy.special.gamma(13.7 + 1)
print("Factorial of 13.7 is approximately:", result)
Output
Factorial of 13.7 is: 39203858337.20441
Answer to the question no. 2(c)
import sympy as sp
import sympy as sp