0% found this document useful (0 votes)
15 views7 pages

Mathematics Practical

This document discusses plotting various mathematical functions in Python including linear, greatest integer, polynomial, and rational functions. Code examples are provided to plot each function by defining the function, generating x-values, calculating corresponding y-values, and plotting the graph.

Uploaded by

Ifra Khan
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)
15 views7 pages

Mathematics Practical

This document discusses plotting various mathematical functions in Python including linear, greatest integer, polynomial, and rational functions. Code examples are provided to plot each function by defining the function, generating x-values, calculating corresponding y-values, and plotting the graph.

Uploaded by

Ifra Khan
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/ 7

Mathematics Practical

(Using Python)
B.Sc. (Mathematics) Semester-I
Some Basic Codes and their Results in Python
Numbers & Operators:
 print(1+2) 3
 print(5-2) 3
 print(2*2) 4
 print(2**3) 8
 print(5/2) 2.5
 print(5//2) 2
 print(5%2) 1

Text/Strings:
 print(“hello world”) hello world
 print(“what’s up”) what’s up

Comments:
 # is used to comment

Variables:
 x=2
print(x) 2
 x = "hello world"
print(x) hello world
 x = "hello world"
del x
print(x) NameError:name 'x' is not defined
Conditional logic:
 print(5==4) False
 print(5!=4) True
Symbol Operation Examples
== Equal to 2==2 True
!= Not equal to 4!=4 False
< Less than 3<5 True
> Greater than 3>5 False
<= Less or equal to 3<=5 True
>= Greater or equal to 2>=3 False

1. Plotting the graphs of the following functions:


(i) f(x)=ax
Code:
import matplotlib.pyplot as plt

def plot_line(a):
x = range(-10, 11) # Define x range
y = [a * xi for xi in x] # Calculate y values

plt.plot(x, y) # Plot the graph


plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title(f'Graph of f(x) = {a}x') # Set title
plt.grid(True) # Show grid
plt.axhline(0, color='black',linewidth=0.5) # Add horizontal line at y=0
plt.axvline(0, color='black',linewidth=0.5) # Add vertical line at x=0
plt.show()

# Change the value of 'a' to plot different lines


plot_line(2) # Example with a = 2

(ii) f(x)=[x]
Code:
import matplotlib.pyplot as plt
import numpy as np

# Define the greatest integer function


def greatest_integer(x):
return np.floor(x)

# Generate x values
x_values = np.linspace(-5, 5, 1000) # Choose the range of x values

# Calculate corresponding y values using the greatest integer function


y_values = greatest_integer(x_values)

# Plot the graph


plt.figure(figsize=(8, 6))
plt.plot(x_values, y_values, label='f(x) = [x]')
plt.title('Graph of the Greatest Integer Function')
plt.xlabel('x')
plt.ylabel('f(x)')

plt.axhline(0, color='black',linewidth=0.5) # Add x-axis for clarity


plt.axvline(0, color='black',linewidth=0.5) # Add y-axis for clarity
plt.legend()
plt.grid(True)
plt.show()

(iii) f(x)=x^{2n}
Code:

import matplotlib.pyplot as plt


import numpy as np

def plot_function(n):
x = np.linspace(-2, 2, 1000) # Define x values
y = x**(2*n) # Calculate y values

plt.plot(x, y, label=f'f(x) = x^{2*n}') # Plot the graph


plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title(f'Graph of f(x) = x^{2*n}') # Set title
plt.legend() # Show legend
plt.grid(True) # Show grid
plt.axhline(0, color='black',linewidth=0.5) # Add horizontal line at y=0
plt.axvline(0, color='black',linewidth=0.5) # Add vertical line at x=0
plt.show()

# Change the value of 'n' to plot different functions


plot_function(2) # Example with n = 2

(iv) f(x)=x^{2n-1}
Code:

import matplotlib.pyplot as plt


import numpy as np

def plot_function(n):
x = np.linspace(-2, 2, 1000) # Define x values
y = x**(2*n-1) # Calculate y values

plt.plot(x, y, label=f'f(x) = x^{2*n-1}') # Plot the graph


plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title(f'Graph of f(x) = x^{2*n-1}') # Set title
plt.legend() # Show legend
plt.grid(True) # Show grid
plt.axhline(0, color='black',linewidth=0.5) # Add horizontal line at y=0
plt.axvline(0, color='black',linewidth=0.5) # Add vertical line at x=0
plt.show()

# Change the value of 'n' to plot different functions


plot_function(2) # Example with n = 2

(v) f(x)=1/x^{2n}
Code:

import matplotlib.pyplot as plt


import numpy as np

def plot_function(n):
x = np.linspace(0.1, 2, 1000) # Define x values, avoiding division by zero
y = 1 / x**(2*n) # Calculate y values

plt.plot(x, y, label=f'f(x) = 1/x^{2*n}') # Plot the graph


plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title(f'Graph of f(x) = 1/x^{2*n}') # Set title
plt.legend() # Show legend
plt.grid(True) # Show grid
plt.axhline(0, color='black', linewidth=0.5) # Add horizontal line at y=0
plt.axvline(0, color='black', linewidth=0.5) # Add vertical line at x=0
plt.show()

# Change the value of 'n' to plot different functions


plot_function(2) # Example with n = 2

(vi) f(x)=1/x^{2n-1}
Code:

import matplotlib.pyplot as plt


import numpy as np

def plot_function(n):
x = np.linspace(-2, 2, 1000) # Define x values
y = 1 / x**(2*n-1) # Calculate y values

plt.plot(x, y, label=f'f(x) = 1/x^{2*n-1}') # Plot the graph


plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title(f'Graph of f(x) = 1/x^{2*n-1}') # Set title
plt.legend() # Show legend
plt.grid(True) # Show grid
plt.axhline(0, color='black', linewidth=0.5) # Add horizontal line at y=0
plt.axvline(0, color='black', linewidth=0.5) # Add vertical line at x=0
plt.show()

# Change the value of 'n' to plot different functions


plot_function(2) # Example with n = 2

You might also like