Skip to content

Commit

Permalink
added CSE-218 all
Browse files Browse the repository at this point in the history
  • Loading branch information
kawshikbuet17 committed Mar 29, 2021
1 parent 7e74ca9 commit da20ead
Show file tree
Hide file tree
Showing 21 changed files with 760 additions and 760 deletions.
Binary file added Course Outline CSE 218 Jan2019_students.pdf
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,93 +1,93 @@
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import math
import numpy
import matplotlib.pyplot as plt

def func_ln_onePLUSx(x,n):
sum = 0
for i in range(1,n+1):
sign = (-1)**(i+1)
temp = (x**i)/i
temp = sign*temp
sum = sum + temp
return sum

print("Make a test of your written function")
x = input("Input the value of x : ")
n = input("Input the value of n : ")

x = float(x)
n = int(n)

print("Where x = ", x , ",n = ", n, "Value of ln(1+x) is : ",func_ln_onePLUSx(x,n))
print()
print()
print("Here is the graph of ln(1+x)")
list1 = []
list2 = []
list3 = []



plt.figure(figsize=(10,10))
plt.grid("True")
plt.xlabel("value of x")
plt.ylabel("value of ln(1+x)")

for i in numpy.arange(-1+0.1, 1, 0.1):
list1.append(i)

for i in list1:
list2.append(math.log(1+i))

plt.plot(list1, list2, label="Builtin Log")

for i in list1:
list3.append(func_ln_onePLUSx(i,1))
plt.plot(list1,list3, label="iteration=1")
list3.clear()
for i in list1:
list3.append(func_ln_onePLUSx(i,3))
plt.plot(list1,list3, label="iteration=3")
list3.clear()
for i in list1:
list3.append(func_ln_onePLUSx(i,5))
plt.plot(list1,list3, label="iteration=5")
list3.clear()
for i in list1:
list3.append(func_ln_onePLUSx(i,20))
plt.plot(list1,list3, label="iteration=20")
list3.clear()
for i in list1:
list3.append(func_ln_onePLUSx(i,50))
plt.plot(list1,list3, label="iteration=50")
list3.clear()
plt.legend()
plt.show()

print()
print()
print("Here is the graph of relative error")
plt.figure(figsize=(10, 10))
plt.xlabel("value of n")
plt.ylabel("Relative error")
list4 = []
list5 = []
for i in range(1,51):
list5.append(i)
for i in range(1, 51):
list4.append(abs((math.log(1.5)-func_ln_onePLUSx(0.5,i))))
plt.plot(list5, list4)
plt.grid("True")
#plt.legend()
plt.show()




# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import math
import numpy
import matplotlib.pyplot as plt

def func_ln_onePLUSx(x,n):
sum = 0
for i in range(1,n+1):
sign = (-1)**(i+1)
temp = (x**i)/i
temp = sign*temp
sum = sum + temp
return sum

print("Make a test of your written function")
x = input("Input the value of x : ")
n = input("Input the value of n : ")

x = float(x)
n = int(n)

print("Where x = ", x , ",n = ", n, "Value of ln(1+x) is : ",func_ln_onePLUSx(x,n))
print()
print()
print("Here is the graph of ln(1+x)")
list1 = []
list2 = []
list3 = []



plt.figure(figsize=(10,10))
plt.grid("True")
plt.xlabel("value of x")
plt.ylabel("value of ln(1+x)")

for i in numpy.arange(-1+0.1, 1, 0.1):
list1.append(i)

for i in list1:
list2.append(math.log(1+i))

plt.plot(list1, list2, label="Builtin Log")

for i in list1:
list3.append(func_ln_onePLUSx(i,1))
plt.plot(list1,list3, label="iteration=1")
list3.clear()
for i in list1:
list3.append(func_ln_onePLUSx(i,3))
plt.plot(list1,list3, label="iteration=3")
list3.clear()
for i in list1:
list3.append(func_ln_onePLUSx(i,5))
plt.plot(list1,list3, label="iteration=5")
list3.clear()
for i in list1:
list3.append(func_ln_onePLUSx(i,20))
plt.plot(list1,list3, label="iteration=20")
list3.clear()
for i in list1:
list3.append(func_ln_onePLUSx(i,50))
plt.plot(list1,list3, label="iteration=50")
list3.clear()
plt.legend()
plt.show()

print()
print()
print("Here is the graph of relative error")
plt.figure(figsize=(10, 10))
plt.xlabel("value of n")
plt.ylabel("Relative error")
list4 = []
list5 = []
for i in range(1,51):
list5.append(i)
for i in range(1, 51):
list4.append(abs((math.log(1.5)-func_ln_onePLUSx(0.5,i))))
plt.plot(list5, list4)
plt.grid("True")
#plt.legend()
plt.show()





Original file line number Diff line number Diff line change
@@ -1,75 +1,75 @@
# -*- coding: utf-8 -*-
"""
Created on Tue May 21 12:16:26 2019
@author: ASUS
"""

import math
import numpy as np
import matplotlib.pyplot as plt

def func(x):
f = x**3 - 2400*(x**2) - 3*x +2
#f = (x/(1-x))*((6/(2+x))**0.5)-0.05
return f

def falsePosition(func, a, b, TOL, N):
i=1
FA = func(a)

while(i<=N):
x = (a*func(b)-b*func(a))/(func(b)-func(a))
FP = func(x)

if(FP==0 or np.abs(func(x))<TOL):
break

i = i+1

if(FA*FP>0):
a = x
else:
b = x
return x,i

def secant(func, x0, x1, TOL, N):
f_x0 = func(x0)
f_x1 = func(x1)
i = 0
while abs(f_x1) > TOL and N>0:
if(f_x1 - f_x0)/(x1 - x0)==0:
print("Denominator zero")
x = x1 - (f_x1)*(x1-x0)/(f_x1-f_x0)
x0 = x1
x1 = x
f_x0 = f_x1
f_x1 = func(x1)
i += 1
N-=1
if abs(f_x1) > TOL:
i = -1
return x, i

a = 0
b = 1
result1, step1 = falsePosition(func, a, b, 0.005, 10000)
print("Result from False Position Method : ", result1)
print("Iteration number of False Position Method : ", step1)

result2, step2 = secant(func, a, b, 0.005, 10000)
print("Result from Secant Method : ", result2)
print("Iteration number of Secant Method : ", step2)

list1 =[]
list2 =[]
for i in np.arange(-500, 2500, 0.01):
list1.append(i)
list2.append(func(i))
plt.figure(figsize=(10,10))
plt.grid("True")

plt.plot(list1, list2)
plt.show()

# -*- coding: utf-8 -*-
"""
Created on Tue May 21 12:16:26 2019
@author: ASUS
"""

import math
import numpy as np
import matplotlib.pyplot as plt

def func(x):
f = x**3 - 2400*(x**2) - 3*x +2
#f = (x/(1-x))*((6/(2+x))**0.5)-0.05
return f

def falsePosition(func, a, b, TOL, N):
i=1
FA = func(a)

while(i<=N):
x = (a*func(b)-b*func(a))/(func(b)-func(a))
FP = func(x)

if(FP==0 or np.abs(func(x))<TOL):
break

i = i+1

if(FA*FP>0):
a = x
else:
b = x
return x,i

def secant(func, x0, x1, TOL, N):
f_x0 = func(x0)
f_x1 = func(x1)
i = 0
while abs(f_x1) > TOL and N>0:
if(f_x1 - f_x0)/(x1 - x0)==0:
print("Denominator zero")
x = x1 - (f_x1)*(x1-x0)/(f_x1-f_x0)
x0 = x1
x1 = x
f_x0 = f_x1
f_x1 = func(x1)
i += 1
N-=1
if abs(f_x1) > TOL:
i = -1
return x, i

a = 0
b = 1
result1, step1 = falsePosition(func, a, b, 0.005, 10000)
print("Result from False Position Method : ", result1)
print("Iteration number of False Position Method : ", step1)

result2, step2 = secant(func, a, b, 0.005, 10000)
print("Result from Secant Method : ", result2)
print("Iteration number of Secant Method : ", step2)

list1 =[]
list2 =[]
for i in np.arange(-500, 2500, 0.01):
list1.append(i)
list2.append(func(i))
plt.figure(figsize=(10,10))
plt.grid("True")

plt.plot(list1, list2)
plt.show()


File renamed without changes.
File renamed without changes.
Loading

0 comments on commit da20ead

Please sign in to comment.