Python 中的 Regula Falsi 方法建模
在本教程中,我将向您展示如何借助 Regula Falsi 方法(也称为“假位置法”)找到方程的根。让我们考虑下图所示。
首先,我们搜索了两个 𝑥 值 $\mathrm{x_{1}}$ 和 $\mathrm{x_{2}}$,在这些值处函数的值 ($\mathrm{y_{1}} $和 $\mathrm{y_{2}}$) 不同,这意味着这些点应该使得这两个值的乘积为负(即,它们应该位于 X 轴的两侧)。由于这些不是精确点,即根存在的点,因此它们被视为假点,因此称为 Regula Falsi 方法。现在,这些点将由一条线连接,该线将在 $\mathrm{x_{n}}$ 点与 x 轴相交。现在,对应于此将计算 $y_{n}$。
如果 $\mathrm{y_{1}}$ 和 $\mathrm{y_{n}}$ 的乘积小于 0,则表示 $\mathrm{x_{1}}$ 和 $\mathrm{x_{n}}$ 位于根的两侧。然后,$\mathrm{x_{2}}$ 将被 $\mathrm{x_{n}}$ 替换,$\mathrm{y_{2}}$ 将被 $\mathrm{y_{1}}$ 替换。否则,它将表明 $\mathrm{x_{1}}$ 和 $\mathrm{x_{𝑛}}$ 位于同一侧,然后 $\mathrm{x_{1}}$ 将被 $\mathrm{x_{n}}$ 替换,$\mathrm{y_{1}}$ 将被 $\mathrm{y_{n}}$ 替换。此后,将重复相同的过程,直到 $y_{n}$ 的绝对值小于某个收敛准则(这里我们将其设置为 0.00001,但您可以根据所需的精度自由选择)。
现在问题是如何计算新的位置 $\mathrm{x_{n}}$。新的位置可以通过经过 ($x_{1},y_{1}$) 和 ($x_{2},y_{2}$) 的直线方程得到,如下所示 -
$$\mathrm{y-y_{1}=\tfrac{\left \{ y_{2}-y_{1} \right \}}{\left \{ x_{2}-x_{1} \right \}}(x-x_{1})}$$
但在 $\mathrm{x_{n}}$ 处,𝑦 截距为零,则上述方程变为
$$\mathrm{0-y_{1}=\tfrac{\left \{ y_{2}-y_{1} \right \}}{\left \{ x_{2}-x_{1} \right \}}(x_{n}-x_{1})}$$
$$\mathrm{0-\tfrac{\left \{ x_{2}-x_{1} \right \}}{\left \{ y_{2}-y_{1} \right \}}y_{_{1}}=(x_{n}-x_{1})}$$
$$\mathrm{x_{1}-\tfrac{\left \{ x_{2}-x_{1} \right \}}{\left \{ y_{2}-y_{1} \right \}}y_{_{1}}=x_{n}}$$
最终,用于计算新 𝑥 值 ($x_{n}$) 的方程变为 -
$$\mathrm{x_{n}=x_{1}-\tfrac{\left \{ x_{2}-x_{1} \right \}}{\left \{ y_{2}-y_{1} \right \}}y_{_{1}}}$$
Python 中 Regula Falsi 方法的实现
我们试图使用 Regula Falsi 方法找到其根的函数是 $\mathrm{𝑥^{3}}$−9𝑥−5=0。初始区间选择为 (−3,−1)。
导入用于数组使用和数据绘图的模块 -
from pylab import *
定义要计算其根的多项式的函数 -
f=lambda x: x**3-9*x-5
此步骤虽然不是严格必要的,但它将有助于可视化 Regula Falsi 过程的工作原理。这段代码将有助于绘制函数 -
# Creating array of x x=linspace(-4,4,50) # Drawing function figure(1,dpi=200) plot(x,f(x)) ylim(-19,10) plot([-4,4],[0,0],'k--')
选择您期望根存在的初始区间。我所取的值是上面提到的多项式。
# Selecting false positions x1 and x2 x1=-3 x2=-1 # Evaluating corresponding y1 and y2 y1=f(x1) y2=f(x2)
检查初始猜测区间 ($\mathrm{𝑥_{1},𝑥_{2}}$) 是否正确,即 $\mathrm{𝑦_{1}x𝑦_{2}}$>0。
如果不是,则使用适当的消息退出循环。否则,进入 while 循环。在 while 循环内,首先根据公式 1 计算 $\mathrm{𝑥_{𝑛}}$ 并找到 $\mathrm{𝑦_{ℎ}}$。
如果 $\mathrm{𝑦_{1}}$ 的绝对值本身很小,则使用最终答案 $\mathrm{𝑥_{1}}$ 中断 while 循环。否则,检查 $\mathrm{y_{1}\:X\:y_{n}s0}$ 是否为真,如果是,则设置 $\mathrm{x_{2}=x_{n}}$ 和 $\mathrm{𝑦_{2}=𝑦_{𝑛}}$。否则,设置 $\mathrm{𝑥_{1}=𝑥_{𝑛}}$ 和 $\mathrm{𝑦_{1}=𝑦_{𝑛}}$。
# Initial check
if y1*y2>0:
print("on the same side of x axis, Correct the Range")
exit
else:
# Plotting line joining (x1,y1) and (x2,y2)
plot([x1,x2],[y1,y2])
# Iteration counter
count=1
# Iterations
while True:
xn=x1-y1*(x2-x1)/(y2-y1)
plot([xn],[0],'o',label=f'{xn}')
yn=f(xn)
if abs(y1)<1.E-5:
print("Root= ",x1)
break
elif y1*yn<0:
x2=xn
y2=yn
plot([x1,x2],[y1,y2])
else:
x1=xn
y1=yn
plot([x1,x2],[y1,y2])
# printing few xn in legend
if count<6:
legend()
# Increasing iteration counter
count +=1
show()
程序的 输出 将是 -
Root= -2.669663840642225
函数图本身显示了对根的收敛,如下所示。
完整的 Python 代码
最终编译后的代码如下 -
# Importing modules for plotting and Array
from pylab import *
# Defining function using Lambda
f = lambda x: x ** 3 - 9 * x - 5
# Creating array of x using linspace
x = linspace(-4, 4, 50)
# Drawing function for better understanding of convergence
figure(1, figsize=(7.20,3.50))
plot(x, f(x))
ylim(-19, 10)
plot([-4, 4], [0, 0], 'k--')
# Selecting false position interval x1 and x2
x1 = -3
x2 = -1
# Evaluating the values at x1 and x2 viz. y1 and y2
y1 = f(x1)
y2 = f(x2)
# Initial check for gussed interval
if y1 * y2 > 0:
print("on the same side of x axis, Correct the Range")
exit
else:
# Plotting line joining (x1,y1) and (x2,y2)
plot([x1, x2], [y1, y2])
# Iteration counter
count = 1
# Iterations
while True:
xn = x1 - y1 * (x2 - x1) / (y2 - y1)
plot([xn], [0], 'o', label=f'{xn}')
yn = f(xn)
if abs(y1) < 1.E-5:
print("Root= ", x1)
break
elif y1 * yn < 0:
x2 = xn
y2 = yn
plot([x1, x2], [y1, y2])
else:
x1 = xn
y1 = yn
plot([x1, x2], [y1, y2])
# printing few xn
if count < 6:
legend()
# Incrementing counter
count += 1
show()
对于其他根,我们必须采用其他初始集合的 $\mathrm{x_{1}}$ 和 $\mathrm{x_{2}}$。您可以练习 (-1, 1) 和 (2, 4)。
结论
在本教程中,您学习了如何在 Python 中建模 Regula Falsi 方法。您还了解了初始猜测区间的选择有多么重要。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP