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

Assignment 1

Uploaded by

pandagirl5871
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Assignment 1

Uploaded by

pandagirl5871
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Answer to the question no.

1(a)

import math

# Define the values of p and q


p = 3
q = 1

# Define the function f(x) and its derivatives


def f(x):
return p**x * math.exp(-math.sin(x**p))

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))))

# Define the function g(x) and its derivatives


def g(x):
return (q + math.tan(x)**p) / (1 + math.cos(x)**2)

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

# Evaluate the derivatives at x = pi/3 and x = 2pi/3


x_pi_3 = math.pi / 3
x_2pi_3 = 2 * math.pi / 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 the results


print("For f(x):")
print("f'(π/3) =", f_prime_pi_3)
print("f'(2π/3) =", f_prime_2pi_3)
print("f''(π/3) =", f_double_prime_pi_3)
print("f''(2π/3) =", f_double_prime_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

Answer to the question no. 1(b)


(a)

import sympy as sp

# Define symbols
x = sp.symbols('x')
p = 3
q = 1

# Define y and its derivatives


y = sp.sin(p*x)
y1 = sp.diff(y, x)
y2 = sp.diff(y1, x)

# Define f
f = y2 + 3*y1 - q*y
# Substitute p and q values
f = f.subs({p: 3, q: 1})

# Simplify the expression


f = sp.simplify(f)

print("Expression for f(x) is:", f)

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 y and its derivatives


p = 3
y = sp.sin(p*x)
y1 = sp.diff(y, x)
y2 = sp.diff(y1, x)

# Define f
f = y2 + 3*y1 - q*y

# Find critical points


critical_points = sp.solve(sp.Eq(y1, 0), x)

# Filter critical points within the given interval


valid_critical_points = [cp.evalf() for cp in critical_points if
-3*np.pi/2 <= cp <= 5*np.pi/2]

# Evaluate the sign of f'(x) around each critical point


extrema = []
for cp in valid_critical_points:
sign_before = np.sign(y1.subs(x, cp - 0.01))
sign_after = np.sign(y1.subs(x, cp + 0.01))
if sign_before != sign_after:
extrema.append(cp)
# Print extrema
print("Extrema in the interval [-3π/2, 5π/2]:", extrema)

# Plot f and f'


x_vals = np.linspace(-3*np.pi/2, 5*np.pi/2, 1000)
f_vals = np.array([f.subs(x, val).evalf() for val in x_vals])
f_prime_vals = np.array([y1.subs(x, val).evalf() for val in
x_vals])

plt.plot(x_vals, f_vals, label='f(x)')


plt.plot(x_vals, f_prime_vals, label="f'(x)")
plt.scatter(extrema, [f.subs(x, ext).evalf() for ext in extrema],
color='red', label='Extrema')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Plot of f(x) and f\'(x)')
plt.legend()
plt.grid(True)
plt.show()

Output
Extrema in the interval [-3π/2, 5π/2]: [0.523598775598299,
1.57079632679490]
Answer to the question no. 2(a)

import sympy as sp

# Define symbols and constants


x, y, z = sp.symbols('x y z')
a, b, c = sp.symbols('a b c')

# Define the function phi


phi = a*x**3 + b*x**2*y - c*z**3

# Compute the second partial derivatives


phi_xx = sp.diff(phi, x, x)
phi_yy = sp.diff(phi, y, y)
phi_zz = sp.diff(phi, z, z)

# Compute the Laplacian


laplacian_phi = phi_xx + phi_yy + phi_zz

# Evaluate the Laplacian at the point (2, -1, 1)


laplacian_phi_at_point = laplacian_phi.subs({x: 2, y: -1, z: 1})

print("Laplacian of phi at point (2, -1, 1) is:",


laplacian_phi_at_point)

Output
Laplacian of phi at point (2, -1, 1) is: 12*a - 2*b - 6*c

Answer to the question no. 2(b)

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

# Define symbols and constants


x = sp.symbols('x')
p = 3
q = 1

# Define the expression to be integrated


expression = 1 / (p**2 * sp.cos(x)**2 + (1 + q)**2 *
sp.sin(x)**2)

# Integrate the expression with respect to x


integral = sp.integrate(expression, (x, 0, sp.pi/2))

print("The value of the integral is:", integral)


Output
The result of the integration is: pi*d**2*q**2/8 +
pi**3*d**2*q**2/48 + d*q/2 + pi**2*d*q/8 + pi*p**2/4 + pi/4
Answer to the question no. 2(d)

import sympy as sp

# Define symbols and constants


x = sp.Symbol('x')

# Define the force function


F = -2*x - 0.1*x**3

# Define the initial and final positions


x_initial = 0
x_final = 2

# Integrate the force function over the displacement


work_done = sp.integrate(F, (x, x_initial, x_final))

# Print the result


print("The work done to move the object from x = 0 to x = 2 is:",
work_done.evalf())
Output
The work done to move the object from x = 0 to x = 2 is: -
4.40000000000000

You might also like