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

2D practical no 1

The document contains a series of Python programs that plot various mathematical functions using libraries such as Matplotlib and NumPy. Each program includes a specific function to plot, such as f(x)=x^2, f(x)=log10(x), and f(x)=sin(x), along with their respective intervals. Additionally, there are examples of customizing plots with colors, markers, and subplots.

Uploaded by

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

2D practical no 1

The document contains a series of Python programs that plot various mathematical functions using libraries such as Matplotlib and NumPy. Each program includes a specific function to plot, such as f(x)=x^2, f(x)=log10(x), and f(x)=sin(x), along with their respective intervals. Additionally, there are examples of customizing plots with colors, markers, and subplots.

Uploaded by

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

12/30/24, 1:52 AM Untitled2

PRACTICAL NO 1

In [ ]: 2D GRAPH

In [3]: #Q1) Write a python program to plot 2D graph of the function f(x)= x**2
#g(x) = x**3 im [1,-1]
from pylab import*
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(-1,1)
f=x**2
g=x**3
plot(x,f)
plot(x,g)
show()

file:///C:/Users/admin1/Downloads/Untitled2.html 1/9
12/30/24, 1:52 AM Untitled2

In [11]: #Q2) Write a python program to plot 2D graph of the function f(x)=Log10(x) in the
# interval[0,5]
from pylab import*
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0,5)
f=log10(x)
plot(x,f)
show()

C:\Users\admin1\AppData\Local\Temp\ipykernel_5016\2415860448.py:7: RuntimeWarning: divide by zero encountered in log1


0
f=log10(x)

In [7]: #Q3) Write a python program to plot 2D graph of the function f(x)=Log10(x) in the
# interval[0,10] SLOVE STUDENTS

file:///C:/Users/admin1/Downloads/Untitled2.html 2/9
12/30/24, 1:52 AM Untitled2

In [9]: #Q4) Write a python program to plot 2D graph of the function f(x)=sin(x) in the
# interval[0,2pi] SLOVE STUDENTS

In [13]: #Q5) Write a python program to plot 2D graph of the function f(x)=sin**(-1)x in the
# interval[-1,1]
from pylab import*
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(-1,1)
f=arcsin(x)
plot(x,f)
show()

In [15]: #Q6) Write a python program to plot 2D graph of the function


#f(x)=sin(x)-e**x+3x-log10(x) in the interval[0,pi]
from pylab import*

file:///C:/Users/admin1/Downloads/Untitled2.html 3/9
12/30/24, 1:52 AM Untitled2

import matplotlib.pyplot as plt


import numpy as np
x=np.linspace(0,pi)
f=sin(x)-e**x+3*x-log10(x)
plot(x,f)
show()

C:\Users\admin1\AppData\Local\Temp\ipykernel_5016\729763212.py:7: RuntimeWarning: divide by zero encountered in log10


f=sin(x)-e**x+3*x-log10(x)

In [1]: #Q7) Plot the graph of f(x)= x**5 in [0,5] with red dashed line with circle markers.
from pylab import*
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0,5)
f=x**5

file:///C:/Users/admin1/Downloads/Untitled2.html 4/9
12/30/24, 1:52 AM Untitled2

plot(x,f,'r--o')
show()

In [9]: #Q8)plot the graphs of sinx,cosx, e**2 and x**2in [0,5] in one figure with 2*2 subplots
from pylab import*
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0,5)
y1=sin(x)
y2=cos(x)
y3=e**x
y4=x**2
subplot(2,2,1)
plot(x,y1,label='sin(x)')
legend()
subplot(2,2,2)
plot(x,y2,label='cos(x)')

file:///C:/Users/admin1/Downloads/Untitled2.html 5/9
12/30/24, 1:52 AM Untitled2

legend()
subplot(2,2,3)
plot(x,y3,label='e**x')
legend()
subplot(2,2,4)
plot(x,y4,label='x**2')
legend()
show()

In [13]: #Q9)Write the python program to plot 2D x-axis black color and in the same
# diagram plot green triangle with vertices [5,4],[7,4],[6,6]
from pylab import*
import matplotlib.pyplot as plt
import numpy as np
fig, ax =plt.subplots()
ax.set_xlim([-10,10])
ax.set_ylim([-10,10])

file:///C:/Users/admin1/Downloads/Untitled2.html 6/9
12/30/24, 1:52 AM Untitled2

ax.axhline(y=0,color='black')
ax.axvline(x=0,color='black')
ax.plot([5,7,6,5],[4,4,6,4],color='green')
show()

In [15]: #Q10) Plot the graph of y=e**(-x**2) in [-5,5] with red dashed point
#line with upward pointing triangle
from pylab import*
import matplotlib.pyplot as plt
import numpy as np
fig, ax =plt.subplots()
ax.set_xlim([-5,5])
ax.set_ylim([-0.1,1.1])
x=np.linspace(-5,5)
y=e**(-x**2)

file:///C:/Users/admin1/Downloads/Untitled2.html 7/9
12/30/24, 1:52 AM Untitled2

ax.plot(x,y,'r--o',markerfacecolor='green')
plot(x)

Out[15]: [<matplotlib.lines.Line2D at 0x233fc78740>]

In [17]: #Q11) Write the python code to plot the graph 2x**2-4x+5 in
#[-10,10] in magenta colored dashed pattern
from pylab import*
import matplotlib.pyplot as plt
import numpy as np
fig,ax=plt.subplots()
ax.set_xlim([-10,10])
ax.set_ylim([-30,30])
x=linspace(-10,10)
y=2*x**2-4*x+5
ax.plot(x,y,'4--')
show()

file:///C:/Users/admin1/Downloads/Untitled2.html 8/9
12/30/24, 1:52 AM Untitled2

In [ ]:

file:///C:/Users/admin1/Downloads/Untitled2.html 9/9

You might also like