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

Labset 08, 9, 10

Uploaded by

davidovwilliam
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)
9 views5 pages

Labset 08, 9, 10

Uploaded by

davidovwilliam
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

Labset 08: Solution of first order differential

equation and plotting the solution curves


𝒅𝒚
Program1:Solve 𝒅𝒙 + 𝒕𝒂𝒏 𝒙 − 𝒚𝟑 𝒔𝒆𝒄 𝒙 = 𝟎
Program code:
from sympy import *
x,y=symbols('x,y')
y=Function("y")(x)
y1=Derivative(y,x)
z1=dsolve(Eq(y1+y*tan(x)-y**3*sec(x)),y)
display(z1)

𝒅𝒚
Program2:Solve = −𝒌𝒚
𝒅𝒕

Program code:
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
def model(y,t):
k=0.3
return -k*y
y0=5
t=np.linspace(0,20)
y=odeint(model,y0,t)
plt.plot(t,y)
plt.title("Solution of dy/dt=-ky; k=0.3, y(0)=5")
plt.xlabel('time')
plt.ylabel('y(t)')
plt.show()
Output:
Labset 09: Finding GCD using Euclid’s algorithm
Program1:Prove that 163 and 512 are relatively prime
Program code:
def gcd1(a,b):
c=1;
if b<a:
t=b;
b=a;
a=t;
while (c>0):
c=b%a;
print(a,c);
b=a;
a=c;
continue
print('GCD=',b);
gcd1(163,512)

OUTPUT:
163 23
23 2
2 1
1 0
GCD= 1

Program2:Calculate GCD of (a,b) and express it as linear


combination of a and b. Calculate GCD=d of 76 and 13,
express the GCD as 76x+13y=d
Program code:
from sympy import *
a=int(input('Enter the first number:'))
b=int(input('Enter the Secound number:'))
s1=1;
s2=0;
t1=0;
t2=1;
r1=a;
r2=b;
r3=(r1%r2);
q=(r1-r3)/r2;
s3=s1-s2*(q);
t3=t1-t2*q;
while(r3!=0):
r1=r2;
r2=r3;
s1=s2;
s2=s3;
t1=t2;
t2=t3;
r3=(r1%r2);
q=(r1-r3)/r2;
s3=s1-s2*(q);
t3=t1-t2*q;
print('The GCD of',a,'and',b,'is',r2);
print('%d x %d + %d x %d = %d\n'%(a,s2,b,t2,r2));

Output:
Enter the first number: 76
Enter the Secound number: 13
The GCD of 76 and 13 is 1
76 x 6 + 13 x -35 = 1
Labset 10: Solving linear congruence of the form
ax≡b(mod m)
Program1:Find the solution of the congruence 5x≡
𝟑(𝒎𝒐𝒅𝟏𝟑)
Program code:
from sympy import *
a=int(input('enter integer a'));
b=int(input('enter integer b'));
m=int(input('enter integer m'));
d=gcd(a,m)
if(b%d!=0):
print('the congruence has no integer solution');
else:
for i in range(1,m-1):
x=(m/a)*i+(b/a)
if(x//1==x):
print('the solution of the congruence is',x)
break
Output:
enter integer a 5
enter integer b 3
enter integer m 13
the solution of the congruence is 11.0

You might also like