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

1 Year Lab 2 Programs

Uploaded by

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

1 Year Lab 2 Programs

Uploaded by

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

Maths Python Lab

Lab Set 2: 2D plots of Cartesian and polar curves


Program 1: Plotting a line(line plot)

Program Code:
import matplotlib.pyplot as plt
x=[1,2,3,4,6,7,8]
y=[2,7,9,1,5,10,3]
plt.plot(x,y,'r+--')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('My first graph!')
plt.show( )

Output:

Program 2: Sine and Cosine Curves

Program Code:
import numpy as np
import matplotlib.pyplot as plt
x=np.arange(-10,10,0.001)
y1=np.sin(x)
y2=np.cos(x)
plt.plot(x,y1,x,y2)
plt.title("sine curve and cosine curve")
plt.xlabel("Values of x")
plt.ylabel("Values of sin(x) and cos(x)")
plt.grid()
plt.show( )
Output:

Program 3: A Simple graph

Program Code:
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(0,2,100)
plt.plot(x,x,label='linear')
plt.plot(x,x**2,label='quadratic')
plt.plot(x,x**3,label='cubic')
plt.xlabel('x label')
plt.ylabel('y label')
plt.title("Simple Plot")
plt.legend()
plt.show( )

Output:
Program 4: Implicit Function-Plot the circle: x2 + y2 = 4

Program Code:
from sympy import plot_implicit, symbols, Eq
x,y=symbols('x y')
p1=plot_implicit(Eq(x**2+y**2, 4), (x, -4, 4) , (y, -4, 4), title= 'Circle: $x^2+y^2 = 4$')

Output:

Program 5: Implicit Function-Plot the lemniscate:a2y2=x2(a2-x2)

Program Code:
from sympy import plot_implicit, symbols, Eq
x,y=symbols('x y')
p2=plot_implicit(Eq(4*(y**2),(x**2)*( 4-x**2)), (x, -5, 5) , (y, -5, 5), title= 'lemniscate:
$4y^2=x^2(a^2-x^2)$')

Output:
Program 6: Implicit Function-Plot the Cardioid:r=5(1+cos𝜃)

Program Code:
from pylab import *
theta=linspace(0,2*np.pi,1000)
r1=5+5*cos(theta)
polar(theta, r1, 'r' )
show( )

Output:

Program 7: Implicit Function-Plot the Four leaved Rose: r = 2 cos 2𝑥

Program Code:
from pylab import *
theta=linspace(0,2*np.pi,1000)
r=2*abs(cos(2*theta))
polar(theta, r, 'r' )
show( )

Output:
Program 8: Implicit Function-Plot the Cardioids: r = a+ a cos𝜃 and r = a- a cos𝜃

Program Code:
import numpy as np
import matplotlib.pyplot as plt
import math
plt.axes(projection = 'polar')
a=3
rad=np.arange(0,(2*np.pi),0.001)
for i in rad:
r = a + (a * np.cos(i))
plt.polar(i, r , 'g.')
r1 = a - (a * np.cos(i))
plt.polar(i, r1 , 'r.')
plt.show( )

Output:

You might also like