0% found this document useful (0 votes)
11 views17 pages

MP Assignment 3

Here are the steps to plot mathematical formula or data points in Python: 1. Import required libraries like matplotlib, math, numpy, pandas etc. 2. Define variables and parameters of the formula. 3. Generate data points by iterating over a range of values using loops or numpy functions. 4. Plot the data points using matplotlib plot function. 5. Add labels, titles, legends etc to embellish the plot. 6. Save the figure or display it. 7. For multiple plots on same axes, use subplot functionality of matplotlib. 8. Create dataframes using pandas to store and manipulate data. 9. Extract insights by analyzing plots or using dataframe functions.

Uploaded by

Manish Raj
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)
11 views17 pages

MP Assignment 3

Here are the steps to plot mathematical formula or data points in Python: 1. Import required libraries like matplotlib, math, numpy, pandas etc. 2. Define variables and parameters of the formula. 3. Generate data points by iterating over a range of values using loops or numpy functions. 4. Plot the data points using matplotlib plot function. 5. Add labels, titles, legends etc to embellish the plot. 6. Save the figure or display it. 7. For multiple plots on same axes, use subplot functionality of matplotlib. 8. Create dataframes using pandas to store and manipulate data. 9. Extract insights by analyzing plots or using dataframe functions.

Uploaded by

Manish Raj
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/ 17

1.

Discharging of a Capacitor: A capacitor with capacitance C=1 µF discharges via


resistance with R= 1K Ω according to the equation q(t) = q0 e(−t/RC)
where the initial charge on the capacitor is q0 = 1 µC.

(a) Find charge on the capacitor after every 0.1 ms and display the data on the python console as data
frame. Take time range [0 : 7 ms]

(b) From the given data frame, determine the time constant(time duration after which the charge on the
capacitor falls to q0/e) of the given RC circuit. (c) Plot the variation of charge on capacitor as function of
time.

INPUT:
import matplotlib.pyplot as plt
import numpy as np

import pandas as pd

C = 10**(-6)
R = 1000
qo = 1*10**(-6)
t = np.arange(0,7+0.1,0.1)
q = qo*np.exp(-t/(1000*R*C))
z = {'Charge' :q, 'Time' :t}
df = pd.DataFrame(z)
tc = R*C
print(tc)
print(df)
plt.plot(t,q)
plt.grid()
plt.xlabel('Time')
plt.ylabel('Charge')
plt.title('Discharge of Capacitor')
plt.text(3, 0.6*10**(-6), '''Ronak Raj Sharma-877''')
OUTPUT:
runfile('C:/Users/shaya/Assignment 3/Q1.py', wdir='C:/Users/shaya/Assignment 3')

0.001

Charge Time

0 1.000000e-06 0.0

1 9.048374e-07 0.1

2 8.187308e-07 0.2

3 7.408182e-07 0.3

4 6.703200e-07 0.4

.. ... ...

66 1.360368e-09 6.6

67 1.230912e-09 6.7

68 1.113775e-09 6.8

69 1.007785e-09 6.9

70 9.118820e-10 7.0

[71 rows x 2 columns]


2. Radioactive Decay: Radioactive decay of nuclei takes place according to the equation
N(t) = N0 e(−λt). Consider a radioactive nuclei with number of nucleons N0 = 1000 at t =0. (a) Find number
of nucleons remains after every 0.02 day if decay constant is λ = 1 days−1. Display the data on the python
console as data frame. Take time range [0: 5 days] (b) From the given data frame, find the half life and
mean life of the given radioactive nuclei.(c) Plot the variation of number of remaining nucleons as
function of time.

INPUT:

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

No = 1000

ld = 1

t = np.arange(0,5+0.02,0.02)

N = No*np.exp(-ld*t)

z = {'Nucleons' :N, 'Time' :t}

df = pd.DataFrame(z)

print(df)

plt.plot(t, N)

plt.grid()

plt.ylabel('Nucleons')

plt.xlabel('Time')

plt.text(3, 600, '''Ronak Raj Sharma-877''')

OUTPUT:

runfile('C:/Users/shaya/Assignment 3/Q2.py', wdir='C:/Users/shaya/Assignment 3')

Nucleons Time

0 1000.000000 0.00

1 980.198673 0.02

2 960.789439 0.04

3 941.764534 0.06
4 923.116346 0.08

.. ... ...

246 7.299131 4.92

247 7.154598 4.94

248 7.012928 4.96

249 6.874063 4.98

250 6.737947 5.00

[251 rows x 2 columns]

3. Single Slit Diffraction: When LASER light falls on a single slit, diffraction phenomenon produces a fringe
pattern on the screen with peak brightness Io at the center. Intensity distribution as a function of
diffraction angle can be described by the equation

I(α) = I0 (sinα/α)2

(a) Vary angle α in range [-10 radian :10 radian] in step of 0.1 radian and plot the

variation of I vs α. If any error is raised by the python interpreter then explain

the error. Take I0 = 1.

(b) Vary angle α in range [-10 radian :10 radian] in step of 0.09 radian and plot the

variation of I vs α. Take I0 = 1.

INPUT:

a) import matplotlib.pyplot as plt

import math as M

I1=[]
alpha1=[]

q=1

for i in range(-100,101,1):

alpha=i/10

if alpha==0:

raise ZeroDivisionError("function is not defined when alpha is zero")

else:

pass

alpha1.append(alpha)

I=q*(M.sin(alpha)/(alpha))**2

I1.append(I)

plt.plot(I1,alpha1,color='g')

plt.xlabel("Intensity")

plt.ylabel("angle")

plt.grid()

plt.text(0.5,5,'Ronak Raj Sharma-877')

plt.title("plot of I vs alpha")

OUTPUT:

b) INPUT:

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

Io = 1

alpha = np.arange(-10,10,0.09)

I = Io*(np.sin(alpha)/alpha)**2
z = {'Intensity' :I, 'alpha' :alpha}

df = pd.DataFrame(z)

print(df)

plt.plot(alpha, I)

plt.grid()

plt.ylabel('Intensity')

plt.xlabel('Alpha')

plt.text(5, 1, '''Ronak Raj Sharma-877''')

OUPUT:

runfile('C:/Users/shaya/Assignment 3/Q3A.py', wdir='C:/Users/shaya/Assignment 3')

Intensity alpha

0 0.002960 -10.00

1 0.002215 -9.91

2 0.001537 -9.82

3 0.000954 -9.73

4 0.000491 -9.64

.. ... ...

218 0.000407 9.62

219 0.000840 9.71

220 0.001398 9.80

221 0.002058 9.89

222 0.002790 9.98


4. Resonance in series LCR: In a series LCR circuit with C = 1 μF , L = 0.01 H and R = 100 Ω a source V=
Vosin(ωt) is applied with . The rms value of potential is 5 volt.

(a) Plot the variation of current Irms as a function of ω(rad/s). Vary ω from [500 rad/s : 40,000 rad/s] in
step of 500 rad/s.

(b) Plot the variation of capacitive reactance and inductive reactance as a function of ω in a single graph
(using the same ω axis) .

(c) Also plot the variation of impedance (Z) as a function of ω.

(d) Plot above three graphs (of part (a), (b) & (c)) in a single graphic window as separate plots using
subplot.

(e) Create dataframe which display ω, Irms, XL, XC, R & Z. Display data frame on the console.

(f) Use the given data frame to find value of ω called the Resonant Frequency for which impedance is
minimum and current is maximum.

INPUT:

a=int(input("enter the option no.= "))

import matplotlib.pyplot as plt

import math as M

import pandas as pd

def LCR():

C=10**(-6)

L=10**(-2)

R=100

V=5

w1=[]

Z1=[]

I1=[]

Xl1=[]

Xc1=[]

for w in range(500,40000+500,500):

w1.append(w)

Xl=w*L
Xl1.append(Xl)

Xc=1/(w*C)

Xc1.append(Xc)

Z=M.sqrt((Xl-Xc)**2+(R)**2)

Z1.append(Z)

I=V/Z

I1.append(I)

return(w1,I1,Xl1,Xc1,Z1,R)

p,q,r,s,t,u=LCR()

if a==1:

plt.plot(p,q,color='g')

plt.xlabel("w(in rad/s)")

plt.ylabel("Irms")

plt.grid()

plt.text(22000,0.04,'Ronak Raj Sharma-877',color='r',size='15')

plt.title("plot of Irms as a function of w")

if a==2:

plt.plot(p,r,color='g')

plt.plot(p,s,color='b')

plt.xlabel("w(in rad/s)")

plt.ylabel("Reactance")

plt.grid()

plt.title("plot of reactance as a function of w")

plt.text(20000,1500,'Ronak Raj Sharma-877',color='r',size='15')

if a==3:

plt.plot(p,t,color='g')

plt.xlabel("w(in rad/s)")

plt.ylabel("Impedance")

plt.grid()
plt.title("plot of impedance as a function of w")

plt.text(20000,1500,'Ronak Raj Sharma-877',color='r',size='15')

if a==4:

plt.subplot(3,1,1)

plt.plot(p,q,color='g')

plt.xlabel("w(in rad/s)")

plt.ylabel("Irms")

plt.title('plot of all above graph in a single plot')

plt.grid()

plt.text(25000,0.03,'Ronak Raj Sharma-877',color='r',size='10')

plt.subplot(3,1,2)

plt.plot(p,r,color='b')

plt.plot(p,s,color='black')

plt.xlabel("w(in rad/s)")

plt.ylabel("XL")

plt.grid()

plt.text(20000,1500,'Ronak Raj Sharma-877',color='r',size='10')

plt.subplot(3,1,3)

plt.plot(p,s,color='black')

plt.grid()

plt.xlabel("w(in rad/s)")

plt.ylabel("XC")

plt.text(20000,1500,'Ronak Raj Sharma-877',color='r',size='10')

if a==5:

dictio={'w(in rad/sec)':p,'Irms(Ampere)':q,'Xl':r,'Xc':s,'R':u,'Z':t}

df1=pd.DataFrame(dictio)

print(df1)

if a==6:

for i in t:
if i==min(t):

k=t.index(i)

print("Resonant frequency ",p[k])

OUTPUT:

a)

b)

c)

d)

e)
f)

5. Newton Law of Cooling: A hot cup of coffee initially at a temperature of 80oC cools down according to
Newton law of cooling given by the equation

T(t) = Ts + (To − Ts)e^(−Kt)

where k is cooling constant

Ts = Ambient temperature

To = Initial temperature of the coffee.

If the ambient temperature is 10oC & and cooling constant is K = 0.1 sec−1,

(a) Plot the variation of Temperature of coffee as a function of time. Vary time inthe interval [0:50 s] in
steps of 0.5 s.

(b) Repeat part (a) for K= 0.05 s−1 & 0.01 s−1. Plot all the three variations on the same plot & compare.
Hence, explain the affect of K on cooling of coffee.

a)INPUT:

import matplotlib.pyplot as plt

import numpy as np

To = 80

Ts = 10

k = 0.1

t = np.arange(0,50+0.5,0.5)

T = Ts + (To-Ts)*np.exp(-k*t)

plt.plot(t,T)

plt.grid()
OUTPUT:

b) INPUT:

import matplotlib.pyplot as plt

import numpy as np

To = 80

Ts = 10

k1 = 0.1

k2 = 0.05

k3 = 0.01

t = np.arange(0,50+0.5,0.5)

T1 = Ts + (To-Ts)*np.exp(-k1*t)

T2 = Ts + (To-Ts)*np.exp(-k2*t)

T3 = Ts + (To-Ts)*np.exp(-k3*t)

plt.plot(t,T1, t, T2, t, T3)

plt.grid()

plt.xlabel('Time')

plt.ylabel('Temperature')

plt.legend(['k1','k2','k3'])
OUTPUT:
CCL ASSIGNMENT
III

Name – Ronak Raj Sharma

Roll No. - 877


Course – B.Sc Physics 1st Year
Semester – I
Assignment -III
Plotting in Python

22
AIM:

20
• Learn to plot mathematical formula or data points in python using different libraries.

@
ge
Co I
HINT: To complete each exercise you should expect to use some or all of these Python
u s-
lle
features:
nd sic
• Use of matplotlib, math and pandas libraries of python
Hi hy
s, P

• Use of for or while loop statement of python


sic cal

• Use different embellishment features of plot function available in matplotlib library.


hy ati

• Use different features of pyhton list and dictionary to handle data.


f P em

• Create data frames using pandas library.


t o th
en Ma

• Use subplot function to create multiple plots in a single graphic window for com-
parison of different sets of data.

Problems:
m
rt
pa

1. Discharging of a Capacitor: A capacitor with capacitance C=1 µF discharges


via resistance with R= 1K Ω according to the equation
De

q(t) = q0 e(−t/RC)

where the initial charge on the capacitor is q0 = 1 µC.


(a) Find charge on the capacitor after every 0.1 ms and display the data on the
python console as data frame. Take time range [0 : 7 ms]
(b) From the given data frame, determine the time constant(time duration after
which the charge on the capacitor falls to q0 /e) of the given RC circuit.
(c) Plot the variation of charge on capacitor as function of time.

1
2. Radioactive Decay: Radioactive decay of nuclei takes place according to the
equation

N(t) = N0 e(−λt)
Consider a radioactive nuclei with number of nucleons N0 = 1000 at t =0.
(a) Find number of nucleons remains after every 0.02 day if decay constant is λ = 1
days−1 . Display the data on the python console as data frame. Take time range [0:
5 days]
(b) From the given data frame, find the half life and mean life of the given radioactive
nuclei.

22
(c) Plot the variation of number of remaining nucleons as function of time.

20
@
3. Single Slit Diffraction: When LASER light falls on a single slit, diffraction phe-
nomenon produces a fringe pattern on the screen with peak brightness Io at the

ge
Co I
center. Intensity distribution as a function of diffraction angle can be described by
u s-
lle
the equation
nd sic
Hi hy

2
I(α) = I0 ( sinα
α )
s, P
sic cal

(a) Vary angle α in range [-10 radian :10 radian] in step of 0.1 radian and plot the
variation of I vs α. If any error is raised by the python interpreter then explain
hy ati

the error. Take I0 = 1.


f P em

(b) Vary angle α in range [-10 radian :10 radian] in step of 0.09 radian and plot the
variation of I vs α. Take I0 = 1.
t o th
en Ma

4. Resonance in series LCR: In a series LCR circuit with C = 1 µF , L = 0.01 H


and R = 100 Ω a source V= Vo sin(ωt) is applied with . The rms value of potential
m

is 5 volt.
rt

(a) Plot the variation of current Irms as a function of ω(rad/s). Vary ω from [500
pa

rad/s : 40,000 rad/s] in step of 500 rad/s.


De

(b) Plot the variation of capacitive reactance and inductive reactance as a function
of ω in a single graph (using the same ω axis) .
(c) Also plot the variation of impedance (Z) as a function of ω.
(d) Plot above three graphs (of part (a), (b) & (c)) in a single graphic window as
separate plots using subplot.
(e) Create dataframe which display ω, Irms , XL , XC , R & Z. Display data frame on
the console.
(f) Use the given data frame to find value of ω called the Resonant Frequency
for which impedance is minimum and current is maximum.

2
5. Newton Law of Cooling: A hot cup of coffee initially at a temperature of 80o C
cools down according to Newton law of cooling given by the equation

T(t) = Ts + (To − Ts )e−Kt

where k is cooling constant


Ts = Ambient temperature
To = Initial temperature of the coffee.
If the ambient temperature is 10o C & and cooling constant is K = 0.1 sec−1 ,
(a) Plot the variation of Temperature of coffee as a function of time. Vary time in

22
the interval [0:50 s] in steps of 0.5 s.

20
(b) Repeat part (a) for K= 0.05 s−1 & 0.01 s−1 . Plot all the three variations on the

@
same plot & compare. Hence, explain the affect of K on cooling of coffee.

ge
Co I
u s-
lle
nd sic
Hi hy
s, P
sic cal
hy ati
f P em
t o th
en Ma
m
rt
pa
De

You might also like