Berikut adalah code python untuk Metode Regula Falsi
import numpy
as np import
matplotlib.pyplot as plt # Define the
function def f(x): #return 2*x**3 - 3 return x**3 - 4*x + 1 # Plotting
the function x =
np.arange(0, 2, 0.01) plt.plot(x,
f(x), linewidth=3) plt.grid() plt.title('Root
of x**3 - 4*x + 1') plt.xlabel('x') plt.ylabel('f(x)') plt.savefig('plot_regula_falsi.png') # Save plot as PNG plt.show() #
Implementing Regula Falsi Method #xl = 1 #xu = 2 xl = 0 xu = 1 tol = 0.0001 xnew =
[0] # Initialize xnew with a zero
value if f(xu) *
f(xl) < 0: print('Iter\t', ' xl\t', ' xu\t', '
xr\t', ' f(xr)', sep='\t') #print('Iter\t\t xl\t xu\t\t\t xr\t
f(xr)') # Menampilkan heading for i in range(1, 1000): xr = (xl*f(xu) - xu*f(xl)) / (f(xu) -
f(xl)) # print(i, xl, xu, xr, f(xr),
sep='\t') print(f'{i:.0f}\t\t {xl:.6f}\t
{xu:.6f}\t {xr:.6f}\t {f(xr):.6f}') # if np.abs(f(xr)) < tol: break # Root found if f(xr) > 0: xu = xr else: xl = xr xnew.append(xr) if
np.abs((xnew[i]-xnew[i-1])/xnew[i]) < tol: break print(f'Solusi akarnya adalah: {xr}') else: print('Nilai tebakannya salah! Masukkan
xl dan xu yang baru') # Final root
value and function evaluation print(f(xr)) |
0 Comments