介绍 (Introduction)
I’ve been wanting to write about this topic for a while now. I recently had the opportunity to work on simulating the GoalSeek functionality of Excel for a web application. I found the whole purpose of GoalSeek and how it works fascinating.
我一直想写这个话题有一段时间了。 最近,我有机会为网络应用程序模拟Excel的GoalSeek功能。 我发现GoalSeek的整个目的以及它的工作方式令人着迷。
The whole purpose of GoalSeek in Excel is finding an input for an equation that will provide the desired solution. To understand how this is supposed to work, we’ll consider something really simple.
Excel中GoalSeek的全部目的是为方程提供输入,以提供所需的解决方案。 为了理解它应该如何工作,我们将考虑一些非常简单的事情。
什么是Goalsseek? (What Is Goalseek?)
Let’s take the example of finding the amount due based on a principal using the Simple Interest formula.
让我们以使用简单利息公式基于本金查找应付金额为例。
The equation for the simple interest formula is, well, simple:
简单利息公式的方程式很简单:
A = P(1+rt), eqn(1)
P -> principalr -> rate of interestt -> time in years
We’ll set the following values:
我们将设置以下值:
P -> 10000r -> 7.5t -> 15
This gives us the amount due as being:
这使我们得到的金额为:
A = 10000(1+7.5*15) = 1135000
Now, let’s say the requirement for our solution changed. Now, instead of finding the amount due based on the principal, rate of interest, and time, we instead need to find the rate of interest that will give us the desired amount due but keeping the principal and time the same.
现在,我们解决方案的需求发生了变化。 现在,而不是寻找到期量基础上的本金,利率的利息和时间,而不是我们需要找到的利率,这将使我们由于所需的量 ,但是保持本金和时间相同。
Let’s alter the example now:
现在让我们更改示例:
P -> 10000r -> ?t -> 15A -> 1120000
Here, we are trying to find the interest rate that will allow us to pay 1120000 instead of 1135000. We can solve this by switching the variables around.
在这里,我们试图找到允许我们支付1120000而不是1135000的利率。我们可以通过切换变量来解决这一问题。
A = P(1+rt) => 1120000 = 10000(1+r*15)
1+15*r = 1120000 / 10000 => r = (112 - 1) / 15
r = 7.4%
Brilliant! There we have it! We did something Excel’s Goalseek does.
辉煌! 到了! 我们做了Excel的Goalseek所做的事情。
One problem, though. That was a really simple equation and problem. What happens if the equation is significantly more complex and involves trigonometric functions along with multiple possible solutions? I’ll give you an example of an equation that you would be able to solve with Goalseek:
不过,有一个问题。 那是一个非常简单的方程式和问题。 如果方程式复杂得多,并且包含三角函数和多个可能的解,会发生什么? 我将为您提供一个方程式的示例,您可以使用Goalseek求解该方程式:
f(x, y) = 1550 - (4*x/y * sinh(y/2 * 1500 / (2*x))), eqn(2)
Yeah, that definitely looks like a handful. One of the daunting factors when looking at something like this for me is that things are being expressed as functions with dependent variables.
是的,那肯定看起来很少。 在我看来,这种艰巨的因素之一是事物被表示为具有因变量的函数。
Wasn’t this A = P(1+rt)
easier to look at? Granted, part of that is also the fact that the equation is a lot smaller.
这不是更容易看A = P(1+rt)
吗? 当然,部分原因是方程式要小得多。
But, what if we re-wrote it like this:
但是,如果我们这样重写它,该怎么办:
f(P, r, t) = P(1+rt)
See? It’s still the same thing.
看到? 还是一样。
Let’s go back to eqn(2). What if we have the following problem statement:
让我们回到eqn(2)。 如果我们有以下问题陈述该怎么办:
0 = 1550 - (4*x/0.022 * sinh(0.022/2 * 1500 / (2*x))),solve for x
Well, again, all you’re really doing is solving for a variable, but just look at how much harder the problem’s gotten. And it’s primarily because of that pesky sinh
sitting in the equation.
恩,再次,您真正要做的只是解决一个变量,而只是看问题变得更加困难。 这主要是因为方程式中有令人讨厌的sinh
。
Okay, if you’re new to this, I imagine things are getting a little overwhelming. Let’s take a step back and think about what we’ve figured out so far.
好吧,如果您是新手,我想事情会变得有些不知所措。 让我们退后一步,想想到目前为止我们所想的。
- We figured out that there’s no real difference between writing a function with notations like the following two: 我们发现编写具有以下两种表示法的函数之间没有真正的区别:
f(P, r, t) = P(1+rt)A = P(1+rt)
2. We figured out that we can solve for one variable to give us the desired result. However, the more complex the equation is, the more complicated getting the solution is.
2.我们发现,我们可以求解一个变量以提供期望的结果。 但是,方程越复杂,求解的难度就越大。
We have two equations of very opposing difficulties to solve. I will introduce a third equation that will help to bridge the gap
我们有两个非常困难的方程式需要解决。 我将介绍第三个方程,这将有助于弥合差距
y = 2x^2+3x-5, eqn(3)
The equation above is a basic parabolic function. This is what the equation looks like when plotted.
上式是一个基本的抛物线函数。 这就是绘制等式时的样子。
Okay, now let’s think about how to solve this equation. Let’s say we want to solve for x
so that y = 0
:
好的,现在让我们考虑如何求解该方程式。 假设我们要求解x
以使y = 0
:
y = 2x^2+3x-5 => 2x^2+3x-5 = 0
x = [-3 + sqrt(3^2 - 4*2*(-5))] / (2*2), [-3 - sqrt(3^2 - 4*2*(-5))] / (2*2)]
x = 1, -2.5
If you are wondering where I got the equation for the solutions from, notice it’s just the classic solution to a quadratic equation.
如果您想知道从哪里获得方程式的解,请注意,这只是二次方程式的经典解。
y = ax^2+bx+c, where y = 0 => ax^2+bx+c = 0
x = -b+sqrt(b^2-4ac) / 2a, x = -b-sqrt(b^2-4ac) / 2a
Note: If you want to find out how this solution was derived, take a look here.
注意:如果要了解此解决方案的来源,请在此处查看 。
Well, that’s one way to solve the equation. You could potentially write a parser that could accept any equation, check the coefficients, accurately separate them and then attempt to solve the equation. You could also use the wonderful algebra.js library here, which does what I just described.
好吧,这是求解方程式的一种方法。 您可能会编写一个解析器,该解析器可以接受任何方程式,检查系数,准确地将它们分开,然后尝试求解方程式。 您还可以在这里使用精彩的algebra.js库,该库可以完成我刚刚描述的操作。
However, if you look at the graph, you will notice that you could have solved this graphically. The goal was to find the point on the curve where y = 0
但是,如果您查看该图,您会发现您可以通过图形方式解决此问题。 目的是找到曲线上y = 0
Well, look carefully and see where the curve crosses the X-axis. It crosses it at two points: [1, -2.5]
There’s your solution!
好吧,仔细看一下,看看曲线与X轴交叉的位置。 它跨越了两点: [1, -2.5]
有您的解决方案!
Now, you’re probably thinking that’s all great, but I can’t exactly teach a computer to look at the graph, find the points where it crosses the X-axis and identify those points. Well, potentially you could, with some form of model trained for image recognition, but that’s another post. So, how do we find our way around this?
现在,您可能会认为这很好,但是我无法完全教计算机观察图形,找到与X轴交叉的点并识别这些点。 好吧,潜在的可能是,经过某种形式的模型训练以进行图像识别,但这是另一篇文章。 那么,我们如何找到解决方法呢?
There are two methods we can use, and these are the ones I will be exploring in depth in this article.
我们可以使用两种方法,而这是我将在本文中深入探讨的方法。
They are called the Newton-Raphson method and the bisection method.
它们被称为牛顿-拉夫森法和二等分法 。
I’ll give you a brief overview of how each method works.
我将简要概述每种方法的工作原理。
TL;DR Version
TL; DR版本
The Newton-Raphson method works by picking a random point and drawing a tangent line at that point. It then calculates a new x
value which is closer to the root. If you keep repeating this, you will find the root.
牛顿-拉夫森方法通过选取随机点并在该点处绘制切线来工作。 然后,它计算一个更接近根的新x
值。 如果继续重复此操作,则会找到根。
The Bisection method works on the principle of finding the interval within which the root lies. Once the accurate interval lies, the solution is found by using an algorithm similar to the one used for binary search.
二等分方法基于找到根所在的区间的原理。 一旦确定了准确的时间间隔,便使用类似于二进制搜索的算法找到解决方案。
Let’s get into each one in more detail.
让我们更详细地介绍每个。
牛顿-拉夫森法 (Newton-Raphson Method)
Okay, let’s dig into the Newton-Raphson method. The Newton-Raphson method is based on three major ideas.
好吧,让我们深入研究牛顿-拉夫森方法。 牛顿-拉夫森方法基于三个主要思想。
- The tangent to a curve at a specific point is a straight line 在特定点上曲线的切线为直线
- The tangent to a curve at a specific point is also the derivative of the curve at that point 在特定点处曲线的切线也是该点处曲线的导数
The equation of a straight line, which is:
y = mx + c
直线方程,即:
y = mx + c
The image above is that of a random curve with a tangent drawn to it.
上面的图像是绘制了切线的随机曲线的图像。
We’ve picked a random point x_n
on the X-axis.
我们在X轴上选择了一个随机点x_n
。
f(x_n)
is the equivalent of the point on the curve. i.e the y-intercept
f(x_n)
等于曲线上的点。 即y截距
f’(x_n)
is the tangent to the curve at the point f(x_n).
f'(x_n)
是曲线在点f(x_n)的切线。
x_(n+1)
is the point where the tangent intercepts the X-axis.
x_(n+1)
是切线与X轴相交的点。
Remember, we said we wanted to find the point where the curve crosses the X-axis, as this would give us our solution. Notice, the point x_(n+1)
is a lot closer to the solution than x_n
was, despite us picking x_n
at random.
记住,我们说过要找到曲线与X轴交叉的点,因为这将为我们提供解决方案。 注意,尽管我们随机选择x_n
,但x_(n+1)
点比x_n
更接近解。
Well, what if we repeated the same process, except this time with x_(n+1)
as our new point initial point? Well, presumably we would end up with a new x
that is even closer to the solution.
好吧,如果我们重复相同的过程,除了这次用x_(n+1)
作为新点的初始点呢? 好吧,大概我们最终会得到一个更接近解决方案的新x
。
So, how do we find the point x_(n+1)
given the equation, the derivative and the original x_n
?
那么,如何在给定方程,导数和原始x_n
找到点x_(n+1)
呢?
Let’s go back to the equation of a straight line: y = mx+c
让我们回到直线方程: y = mx+c
We said that the tangent to a curve at a point is a straight line.
我们说一点与曲线的切线是一条直线。
We also said that the y-intercept is equal to f(x_n)
我们还说过y截距等于f(x_n)
We know from calculus, that the derivative is equal to the slope.
从微积分中我们知道,导数等于斜率。
Therefore, we get the following:
因此,我们得到以下信息:
Now, we need to find the root of this tangent line, so set y = 0
and x = x_(n+1)
, and solve for x_(n+1)
现在,我们需要找到该切线的根,因此设置y = 0
和x = x_(n+1)
并求解x_(n+1)
This gives us the following:
这给我们以下内容:
Now, we have everything we need to solve for x_(n+1)
.
现在,我们拥有解决x_(n+1)
所需的一切。
This went way over my head the first time I saw all the equations, so let’s try it with an example to see how it works.
第一次看到所有方程式时,这种感觉就超乎寻常了,所以让我们通过一个例子来尝试一下它的工作原理。
We’ll take eqn(2) and work through that. Let’s pick x_n=3
我们将使用eqn(2)并进行处理。 让我们选择x_n=3
f(x) = 2x^2+3x-5f'(x) = 4x+3f(3) = 18+9-5 = 22f'(3) = 15x_1 = 3 - 22/15 = 1.53
f(1.53) = 4.2718f'(1.53) = 9.12x_2 = 1.53 - 4.2718/9.12 = 1.0616
If you follow that all the way through, you should get a solution where x=1
and as we know from the earlier graph, this is one of our solutions.
如果从头开始,那么应该得到x=1
的解决方案,正如我们从前面的图中所知道的,这是我们的解决方案之一。
If you notice what we did above was just follow a series of steps in a certain order repeatedly, i.e. the very definition of an algorithm. Here is what the code looks like for the same.
如果您注意到我们上面所做的,就是按照特定顺序重复执行一系列步骤,即算法的定义。 这是相同代码的外观。
The code snippet makes heavy use of the math.js library. The main functions I am making use of are the math.derivative and the math.eval functions. Respectively, they calculate the derivative of an expression and evaluate an expression based on an object of key-value pairs.
该代码段大量使用了math.js库 。 我使用的主要函数是math.derivative和math.eval函数。 他们分别计算表达式的派生并基于键值对的对象评估表达式。
The bit of the code snippet I want to draw your attention to is lines 14–16.
我想引起您注意的代码段是第14至16行。
if (Math.abs(result - guess) < Math.exp(-15)) { return result }
What we’re doing here is defining the base condition to end our iteration. We’re saying that if the difference between x_n
and x_(n+1)
is less than 10^(-15)
return the result.
我们在这里所做的是定义结束迭代的基本条件。 我们说的是,如果x_n
与x_(n+1)
差小于10^(-15)
返回结果。
If you work through the prior exercise all the way through, you’ll arrive at a situation where each successive x
value is almost identical to the prior x
value, and this is how we know we have found a solution.
如果您一直进行先前的练习,就会遇到以下情况:每个连续的x
值几乎都与先前的x
值相同,这就是我们知道找到解决方案的方式。
I have a nice little simulation built with d3.js in codepen showing you how this would run iteratively.
我在Codepen中使用d3.js构建了一个不错的小模拟,向您展示了如何迭代运行。
Just enter a value in the input box and hit submit and you can watch the algorithm run graphically.
只需在输入框中输入一个值,然后单击“提交”,即可看到算法以图形方式运行。
Note: Please try a range of sensible inputs, I haven’t exactly built a robust system here.
注意:请尝试一系列明智的输入,这里我还没有完全构建一个强大的系统。
二等分法 (Bisection Method)
Okay, so we figured out how the Newton-Raphson method works. Let’s tackle the bisection method next.
好的,所以我们弄清楚了Newton-Raphson方法的工作原理。 接下来让我们解决二分法。
The bisection method is a lot easier to understand than the Newton-Raphson method. It’s based on a very simple mathematical property:
与牛顿-拉夫森法相比,二分法更容易理解。 它基于非常简单的数学属性:
If a function f(x) is continuous on the interval [a, b] and the sign of f(a) !== f(b), then there is a value c in the range (a, b) where f(c) = 0. In other words, c is the root of the equation.
如果函数f(x)在区间[a,b]上连续且f(a)的符号!== f(b),则在(a,b)范围内有一个值c,其中f( c)=0。换句话说,c是等式的根。
If that didn’t make sense to you, think about it purely numerically and then purely graphically.
如果那对您没有意义,请纯粹从数字上考虑,然后从图形上考虑。
Let’s say you have the following interval: [-7, 6]. Now if I ask you to count just the integers from -7 to 6, you would also count 0 at some point in that interval. That’s essentially what the property above says.
假设您有以下间隔:[-7,6]。 现在,如果我只要求您计数从-7到6的整数,那么您也将在该时间间隔的某个点计数为0。 基本上就是上面属性所说的。
Let’s look at what this means graphically.
让我们以图形的方式看一下这意味着什么。
The above function is a continuous line and it goes from negative to positive, which implies that it has to cross 0 at some point. Since it has to cross 0, that means the root lies in this interval.
上面的函数是一条连续的线,它从负到正,这意味着它必须在某个点处越过0。 由于必须越过0,这意味着根位于此间隔中。
Okay, this means that using the bisection method is a two-step process.
好的,这意味着使用二等分方法是一个两步过程。
- Find the interval within which the root lies, if such an interval exists 找到根所在的间隔(如果存在)
- Find the actual root within this interval 在此间隔内找到实际根
Here’s the code for how you’d find the interval:
这是找到间隔的代码:
Again, I am making use of mathjs here, so you can look up the documentation for that.
同样,我在这里使用mathjs,因此您可以查找有关此文档。
The interesting bit of this algorithm is in lines 18–26, where I am making a check to see if either my function evaluation of the left interval or right interval has resulted in something that is NaN
. I will explain why I included this code block when we explore how to solve eqn(2).
该算法的有趣之处在于第18–26行,在此处检查我对左间隔或右间隔的函数求值是否得出NaN
。 当我们探索如何解决eqn(2)时,我将解释为什么我包含此代码块。
Once we have the interval within which the solution lies, we can turn our attention to actually finding the solution itself.
一旦有了解决方案所在的间隔,我们就可以将注意力转移到实际找到解决方案本身上。
If you’ve ever tried to write a binary search algorithm on an array, the code snippet above should look very familiar to you. We are employing more or less the same approach here. Here are the steps involved.
如果您曾经尝试在数组上编写二进制搜索算法,则上面的代码片段对您来说应该非常熟悉。 我们在这里采用或多或少相同的方法。 这是涉及的步骤。
- I start with my left and right intervals and find a mid-point 我从左右间隔开始,找到一个中点
- Check whether the solution lies to the left of the mid-point or to the right of the mid-point 检查解决方案是位于中点左侧还是中点右侧
If it lies to the left, set
right = mid
, else setleft = mid
如果位于左侧,则设置
right = mid
,否则设置left = mid
Eventually, the midpoint will be the root itself.
最终,中点将成为根本身。
Here is a little simulation running through what is actually going on.
这是实际运行过程中的一些模拟。
Note: I apologise for how ugly the simulation looks, unfortunately styling is not my forte. Again, sensible range of inputs, because otherwise its going take quite a while for the simulation to run.
注意:我很抱歉模拟看起来很丑陋,不幸的是样式不是我的专长。 再次,合理的输入范围,因为否则它将花费相当长的时间才能运行模拟。
In the pen above, enter a value, and the simulation will attempt to find an interval within which a potential root could exist. Once it has found an interval, it will start trying to find the root by using the algorithm we discussed immediately prior to this.
在上面的笔中,输入一个值,模拟将尝试查找一个可能存在根的间隔。 一旦找到间隔,它将开始尝试使用我们之前讨论的算法来寻找根。
求解复杂方程 (Solving Complex Equations)
Alright, we’ve explored two different methods of finding the roots of equations. Now, its time to explore the more complex eqn(2) we had and see which of these methods can solve that equation.
好了,我们探索了找到方程根的两种不同方法。 现在,该探讨更复杂的eqn(2)了,看看这些方法中的哪一种可以解决该方程。
I’ll put the equation below so it’s clear
我将等式放在下面,这样很清楚
f(x, y) = 1550 - (4*x/y * sinh(y/2 * 1500 / (2*x))), eqn(2)
Solve for f(x, y) = 0, where y = 0.022
0 = 1550 - (4*x/0.022 * sinh(0.022/2 * 1500 / (2*x)))
First, let’s visualize what this equation looks like. It’ll give us a much better intuition for why something might go wrong.
首先,让我们形象地看一下这个方程式。 这将使我们对为什么可能出现问题有更好的直觉。
The thing to note about this equation is that it tends to infinity as x tends to 0. This is going to pose a problem for the Newton-Raphson method because the Newton-Raphson solution tends to follow the path of the tangent, in which case it might quickly dissolve to infinity as a solution unless it managed to hit on the solution by chance.
关于该方程式的注意事项是,随着x趋于0,它趋于无穷大。这将为Newton-Raphson方法带来问题,因为Newton-Raphson解趋于遵循切线的路径,在这种情况下除非它偶然碰上了解决方案,否则它可能会很快溶解为无穷大。
Try running the above equation with the Newton-Raphson method and you’ll see what I mean. You will probably get a result of null.
尝试使用Newton-Raphson方法运行上述方程式,您将明白我的意思。 您可能会得到null的结果。
The bisection method, on the other hand, will work quite nicely for this. It works well because we are taking very small incremental steps with a step size we have control over. Run the below codepen and you should see how nicely the bisection method works for most equations.
另一方面,二分法对此非常有效。 它之所以行之有效,是因为我们正在采取很小的增量步长,并且步长可以控制。 运行下面的代码笔,您应该看到平分法对大多数方程式的工作效果如何。
The code above is almost identical to the previous version we set up for the bisection method, baring a few differences. I set up a separate codepen so I could be spared the effort of having to allow a way to enter equations, which would require extensive checks and error handling. Also, this equation requires special boundaries for defining its data since it tends to infinity as x approaches 0. If you’re interested you can see what I mean if you have a look through the code.
上面的代码几乎与我们为二等分方法设置的先前版本相同,仅存一些差异。 我设置了一个单独的Codepen,因此可以省去允许输入方程式的工作量,这将需要大量检查和错误处理。 另外,该方程式需要特殊的边界来定义其数据,因为当x接近0时,方程式趋于无穷大。如果您感兴趣的话,可以看一看代码。
Now, in the bisection method code I told you about this block of code here:
现在,在二等分方法代码中,我在这里告诉了您有关此代码块的信息:
if (Number.isNaN(result_left)) { left -= stepSize scope_left[variable] = left result_left = math.eval(eqn, scope_left) } if (Number.isNaN(result_right)) { right += stepSize scope_right[variable] = right result_right = math.eval(eqn, scope_right)}
So the reason I have this is to handle situations like those that arise for eqn(2). Because eqn(2) tends to infinity as x tends to 0, there could be a situation where the evaluation of the equation returns either NaN
or Infinity
. To avoid this situation, I simply shift the equation over by the step size repeatedly until I can get back to the domain of the function that lies in the real number range.
因此,我这样做的原因是要处理类似eqn(2)出现的情况。 由于eqn(2)随着x趋于0趋于无穷大,因此可能存在方程的求值返回NaN
或Infinity
。 为了避免这种情况,我只是简单地反复将方程式移步长,直到可以回到实数范围内的函数域为止。
二等分>牛顿-拉夫开? (Bisection > Newton-Raphson?)
This brings me to an important point, why did Newton-Raphson fail for this equation? We know that since Newton-Raphson follows the tangent of the curve at different points, it can dissolve to infinity if the equation tends to infinity at any particular point. This highlights one of the shortcomings of the Newton-Raphson method.
这使我想到了一个重要的观点,为什么牛顿-拉夫逊公式无法通过该方程式? 我们知道,由于牛顿-拉夫森曲线在不同点处遵循曲线的切线,因此,如果方程在任何特定点处趋于无穷大,则它可以溶解为无穷大。 这凸显了牛顿-拉夫森法的缺点之一。
The Newton-Raphson method works well for a continuous function. If the function is discontinuous as in eqn(2) is, it will typically fail.
Newton-Raphson方法对于连续函数非常有效。 如果函数在eqn(2)中是不连续的,则通常会失败。
- Newton-Raphson cannot account for multiple maxima and minima in a function. Newton-Raphson无法解释函数中的多个最大值和最小值。
Take the following graph for example.
以下图为例。
Pick a point at random between -0.19 and +0.19, and you should see that you will get a negative slope, which means the tangent to the curve at that point will intercept the X-axis at a point further away from the root, which goes against the principle of the Newton-Raphson method. This implies that Newton-Raphson will typically fail for cubic and higher order equations.
在-0.19和+0.19之间随机选择一个点,您应该看到会得到一个负斜率,这意味着该点处曲线的切线将在离根更远的点处截取X轴。违反了牛顿-拉夫森法的原理。 这意味着对于三次方程和更高阶方程,Newton-Raphson通常会失效。
The Bisection Method should not have the same problem because it depends on finding an interval within which the solution has to lie, and curves like the above will not be an obstacle to that as long it is continuous in that domain.
二等分法不应该有相同的问题,因为它取决于找到解决方案所必须位于的区间,并且只要上述曲线在该域中是连续的,上述曲线就不会成为障碍。
If you compare the two in terms of Big(O) notation, it seems obvious that Newton-Raphson runs on fewer iterations than the Bisection method, simply because it converges much faster when you view it graphically. Ironically, if you run this with a timing process, it frequently turns out that, given the same starting point, the Bisection method runs faster than the Newton-Raphson method.
如果以Big(O)表示法比较两者,则很明显,牛顿-拉夫森的迭代次数比Bisection方法要少,这仅仅是因为当以图形方式查看时,它的收敛速度更快。 具有讽刺意味的是,如果使用计时过程运行此程序,则通常会发现,在相同的起点下,对分方法的运行速度比牛顿-拉夫森方法的运行速度快。
This is because the Newton-Raphson involves computing a derivative at every step, which turns out to be very computationally expensive. Incrementing and decrementing a number on the other is relatively computationally inexpensive.
这是因为Newton-Raphson涉及到在每个步骤上计算导数,这在计算上非常昂贵。 另一方面,递增和递减数字在计算上相对便宜。
If you want to run the same on your machine and check the results, check out the repo here. You can clone that repo, run npm install
and then npm run start
on your machine, and you should see the results of running both the Newton-Raphson and Bisection method on an identical equation given the same initial guess.
如果要在计算机上运行相同的文件并检查结果,请在此处签出仓库。 您可以克隆该存储库,在计算机上运行npm install
,然后npm run start
,并且在给定相同的初始猜测的情况下,应该在相同的方程式上同时运行Newton-Raphson和Bisection方法的结果。
结论 (Conclusion)
Okay, we’ve covered a lot here. But honestly, this is such a ridiculously vast topic that I’ve barely scratched the surface. Convergence of equations is a widely studied topic. Consider one of the most basic things we haven’t covered: finding multiple roots.
好的,我们在这里介绍了很多内容。 但老实说,这是一个如此荒谬的话题,我几乎没有摸索过。 方程的收敛是一个广泛研究的话题。 考虑一下我们尚未涵盖的最基本的事情之一:找到多个根源。
You can of course modify the algorithms provided in this article to achieve that.
您当然可以修改本文提供的算法来实现。
Take the equation below, for example. It has 3 roots (3 points where it intercepts the X-axis, and you need to find all of these roots).
以下面的等式为例。 它具有3个根(与X轴相交的3个点,您需要找到所有这些根)。
I’m going to post all my sources here, feel free to look through them.
我将在这里发布我的所有资源,随时查看它们。
Note: If you have questions or comments about the article, don’t hesitate to reach out to me via comments on this article or on GitHub or Twitter.
注意:如果您对本文有疑问或意见,请随时通过本文或GitHub或Twitter上的评论与我联系。
http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/bisection.html
http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/bisection.html
翻译自: https://www.freecodecamp.org/news/how-to-tackle-root-finding-in-javascript-40d2c34507b2/