Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.3 kB
0
Indexable
Never
import matplotlib.pyplot as plt
import numpy as np

def value_of_fx(x):
    return x**3-x-1





print("      n         |        a        |     f(a)         |     b         |        f(b)       |        c        |       Approximation Error        | Relative Approximation Error")
print(" ")



a=50
b=-50
i=1
x0=1
iter_nmbr=[]
approximation_err=[]
rel_apprx=[]
while abs(value_of_fx(x0))>= 10 ** -5 or i==1:
    x0=(a+b)/2

    if(value_of_fx(x0)*value_of_fx(a)<0):
        apprxmtn=abs(x0-b)
        b=x0
        
    else:
        apprxmtn=abs(x0-a)
        a=x0;
    rel_apprxmtn=(apprxmtn*100)
    if(x0== 0):
        rel_apprxmtn=100
    else:
        rel_apprxmtn=rel_apprxmtn/x0
    print(f" {i:10.6f}      {a:10.6f}       {value_of_fx(a):15.6f}       {b:10.6f}     {value_of_fx(b):15.6f}   {x0:15.6f}    {apprxmtn:19.6f}         {rel_apprxmtn:20.4f}%    ")
    i=i+1
    iter_nmbr.append(i)
    approximation_err.append(apprxmtn)
    rel_apprx.append(rel_apprxmtn)


x_bapp = np.array(iter_nmbr)
y_bapp = np.array(approximation_err)

plt.bar(x_bapp,y_bapp)
plt.xlabel("Iteration Number")
plt.ylabel("Approximation error")
plt.title("Bisection Method")
plt.show()

y_brl=np.array(rel_apprx)
plt.bar(x_bapp,y_brl)
plt.xlabel("Iteration Number")
plt.ylabel("Relative Approximation error")
plt.title("Bisection Method")
plt.show()

print("  n    |   X0        |     f(x0)       |     x1         |        f(x1)       |    x2     | Approximation Error   | Relative Approximation Error")
print(" ")




x0=50
x1=-50
i=1
x2=1
F_iter_nmbr=[]
F_approximation_err=[]
F_rel_apprx=[]
while abs(value_of_fx(x2))>= 10 ** -2 or i==1:
    x2=(x0*value_of_fx(x1))-(x1*value_of_fx(x0))
    x2/=(value_of_fx(x1) - value_of_fx(x0))
   
    if(value_of_fx(x2)*value_of_fx(x1)<0):
        apprxmtn=abs(x2-x0)
        x0=x2
        
    else:
        apprxmtn=abs(x2-x1)
        x1=x2
    

    rel_apprxmtn=(apprxmtn*100)
    if(x2== 0):
        rel_apprxmtn=100
    else:
        rel_apprxmtn=rel_apprxmtn/x2
    print(f" {i:10.6f}   {x0:10.6f}   {value_of_fx(x0):15.6f}   {x1:10.6f}   {value_of_fx(x1):15.6f}     {x2:15.6f}    {apprxmtn:19.6f}    {rel_apprxmtn:20.4f}%    ")
    i=i+1
    F_iter_nmbr.append(i)
    F_approximation_err.append(apprxmtn)
    if(rel_apprx> 10 ** -3) :
        F_rel_apprx.append(rel_apprxmtn)
   




x_fapp = np.array(F_iter_nmbr)
y_fpp = np.array(F_approximation_err)

y_frl=np.array(F_rel_apprx)

plt.bar(x_fapp,y_fpp)
plt.xlabel("Iteration Number")
plt.ylabel("Approximation error")
plt.title("False Position Method")
plt.show()

plt.bar(x_fapp,y_frl)
plt.xlabel("Iteration Number")
plt.ylabel("Relative Approximation error")
plt.title("False Position Method")
plt.show()


plt.bar(x_bapp,y_bapp,label="Bisection Method")
plt.bar(x_fapp,y_fpp,label="False Position Method")
plt.xlabel("Iteration Number")
plt.ylabel("Approximation error")
plt.title("Bisection Method vs False Position Method")
plt.legend()
plt.show()

plt.bar(x_bapp,y_brl,label="Bisection Method")
plt.bar(x_fapp,y_frl,label="False Position Method")
plt.xlabel("Iteration Number")
plt.ylabel("Relative Approximation error")
plt.title("Bisection Method vs False Position Method")
plt.legend()
plt.show()