0% found this document useful (0 votes)
8 views5 pages

Python Code

The document contains Python code for calculating and comparing mathematical functions including the sum of squares S(N), the function F(x,N), and G(x). It includes examples of usage, comparisons with logarithmic functions, and visualizations using matplotlib. The code is structured to demonstrate the implementation of these functions and their graphical representation over specified ranges.

Uploaded by

analysiscbda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Python Code

The document contains Python code for calculating and comparing mathematical functions including the sum of squares S(N), the function F(x,N), and G(x). It includes examples of usage, comparisons with logarithmic functions, and visualizations using matplotlib. The code is structured to demonstrate the implementation of these functions and their graphical representation over specified ranges.

Uploaded by

analysiscbda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Code to calculate the sum S(N)=12+22+⋯+N2S(N) = 1^2 + 2^2 + \dots + N^2S(N)=12+22+⋯+N2

def sum_of_squares(N):

return sum(n**2 for n in range(1, N+1))

# Example usage to find S(10) and S(30)

S_10 = sum_of_squares(10)

S_30 = sum_of_squares(30)

print(f"S(10) = {S_10}")

print(f"S(30) = {S_30}")

2. a. Code to calculate F(x,N)F(x, N)F(x,N)

The given equation for F(x,N)F(x, N)F(x,N) is:

F(x,N)=∑n=1N(−1)n+1nxnF(x, N) = \sum_{n=1}^{N} \frac{(-1)^{n+1}}{n}


x^nF(x,N)=n=1∑Nn(−1)n+1xn

This can be implemented as:

import math

def F_x_N(x, N):

return sum((-1)**(n+1) * (x**n) / n for n in range(1, N+1))

# Example usage to find F(1, 10)

F_1_10 = F_x_N(1, 10)

print(f"F(1, 10) = {F_1_10}")

2. c. Compare F(1,N)F(1, N)F(1,N) with ln⁡(1+N)\ln(1+N)ln(1+N) for several values of NNN

def compare_F_and_ln(N_values):
for N in N_values:

F_value = F_x_N(1, N)

ln_value = math.log(1 + N)

print(f"For N = {N}: F(1, {N}) = {F_value}, ln(1 + {N}) = {ln_value}")

# Example usage

compare_F_and_ln([1, 3, 7, 10])

2. d. Plot F(x,N)F(x, N)F(x,N) and ln⁡(1+x)\ln(1+x)ln(1+x)

This will plot F(x,N)F(x, N)F(x,N) for several values of NNN and compare it to ln⁡(1+x)\
ln(1+x)ln(1+x).

import numpy as np

import matplotlib.pyplot as plt

# Define the functions

def ln_1_plus_x(x):

return np.log(1 + x)

def F_x_N_vectorized(x, N):

return np.sum([(-1)**(n+1) * (x**n) / n for n in range(1, N+1)], axis=0)

# Define the x values and N values

x_values = np.linspace(-1, 1, 400)

N_values = [3, 7, 10]

# Plot for each N value

plt.figure(figsize=(10, 6))

for N in N_values:
F_values = F_x_N_vectorized(x_values, N)

plt.plot(x_values, F_values, label=f"F(x, {N})")

# Plot ln(1 + x)

ln_values = ln_1_plus_x(x_values)

plt.plot(x_values, ln_values, label="ln(1 + x)", linestyle='dashed')

# Customize the plot

plt.title("Comparison of F(x, N) and ln(1 + x)")

plt.xlabel("x")

plt.ylabel("Value")

plt.legend()

plt.grid(True)

plt.show()

3. a. Code to calculate G(x)G(x)G(x)

The formula for G(x)G(x)G(x) is:

G(x)=1+12x−18x2+116x3−5128x4G(x) = 1 + \frac{1}{2}x - \frac{1}{8}x^2 + \frac{1}{16}x^3


- \frac{5}{128}x^4G(x)=1+21x−81x2+161x3−1285x4
def G_x(x):

return 1 + (1/2)*x - (1/8)*x**2 + (1/16)*x**3 - (5/128)*x**4

# Example usage

x_value = 1

G_value = G_x(x_value)

print(f"G({x_value}) = {G_value}")

3. b. Compare G(x)G(x)G(x) with ln⁡(1+x)\ln(1+x)ln(1+x)

def compare_G_and_ln(x_values):
for x in x_values:

G_value = G_x(x)

ln_value = math.log(1 + x)

print(f"For x = {x}: G(x) = {G_value}, ln(1 + {x}) = {ln_value}")

# Example usage

compare_G_and_ln([0.5, 1, -0.5])

3. c. Plot 1+x\sqrt{1+x}1+x and G(x)G(x)G(x)

# Define the square root function

def sqrt_1_plus_x(x):

return np.sqrt(1 + x)

# Plotting G(x) and sqrt(1 + x)

x_values = np.linspace(-1, 1, 400)

G_values = G_x(x_values)

sqrt_values = sqrt_1_plus_x(x_values)

plt.figure(figsize=(10, 6))

# Plot G(x)

plt.plot(x_values, G_values, label="G(x)", color="blue")

# Plot sqrt(1 + x)

plt.plot(x_values, sqrt_values, label=r"$\sqrt{1+x}$", color="red", linestyle='dashed')

# Customize the plot

plt.title("Comparison of G(x) and sqrt(1+x)")

plt.xlabel("x")

plt.ylabel("Value")
plt.legend()

plt.grid(True)

plt.show()

You might also like