Solver Help

Solver solves f(x) = 0 using either secant method, Newton's method or several variants of Newton's method some of which can automatically detect multiple roots and speed iterations. The variants include

  1. Approximating Newton's method by using approximate derivatives.
  2. An unusual modified Newton's that uses a parabola as an aid in find double roots.
  3. An accelerated Newton's method that may detect double, triple, and even quadruple roots and accelerates the iterations accordingly.
  4. A method which iterates with g(x) = f(x)/f'(x) which reduces multiple roots to singular roots. Uses Newton method with g'.
  5. A method which iterates with g(x) = f(x)/f'(x) which reduces multiple roots to singular roots. Uses Newton method approximating g'.

Methods 3 and 4 are "well" known (en.wikipedia.org/wiki/Newton%27s_method).

While the methods to solve functions with multiple roots are mathematically great, there is often disappointment that mathematical calculations at roots with multiplicity result finding those roots with less accuracy than hoped even though the calculations are done with double precision.

It graphs the function and the iterative steps. There are some tests that may help when the iterations do not converge. Optionally, it can graph the first and second derivatives of the function.

Another option is provided for solving cubic (as well as quadratic and linear) polynomials analytically (without iteration). It uses a complicated set of formulas to find all the exact real solutions without requiring any starting values.

Graphing the function may be helpful in select starting points for the iteration and showing the iterations graphically may be helpful in understanding the method.

To use the solver, one supplies the function and its first and second derivative as needed. The secant method and the methods that use approximate derivatives do not require a derivative. The first derivative is required for the Newton's and the accelerated method. Both the first and second derivatives are required for the modified method and the iteration with g(x) = f(x)/f'(x) requiring g'. The user also supplies an initial guess or guesses at the root.

This apt is based on the grapher apt but only the rectangular mode is supported. While the functions are labeled differently, one can actually graph up to the 6 functions that grapher allows if it is not desired to solve f(x) = 0.

It is best to make sure that "Allow motion" is not checked before typing anything to change any of the input boxes.

The Result Text Area

The white area above the plot is used to display the results of iterations for iterative methods and the solutions when the cubic method is used and some messages. The line for iterations and solutions contains:

The Functions Boxes

See Options for special features. below.

The first 3 function input boxes are labeled f(x), f '(x) and f ''(x). The fourth box has several different uses. It holds the code for the drawing tangent lines drawn when using Newton's and related methods. When using the secant method, it has code for the secant lines. In the accelerated method is used, the label changes to "iter line" because the line is no longer tangent when an accelerator is used. The fifth function box labeled "v line" holds the code for the vertical lines for the iteration methods. The normally unlabeled last function box, is label "quad" box, when it holds the code for the pieces of quadratics (parabolas) for the modified Newton's iteration steps. It changes to g(x) when one of "use g(x)" methods are used.

Use x for the independent variable.

Note: If the "f(x)" function box is changed and you press "enter", then the "tangent" and "v line" boxes are cleared in order to clear the graph of any previous solution's iterations because those iterations no longer apply. In addition, the last function box is cleared if the last solution method used that box.

It is possible to use the 6 function input boxes to plot 6 different functions if you do not want to use the "Solve" methods. Just remember to check the Show f ' and Show f '' boxes to show the functions in the second and third function boxes. (But change the first function box before the third, fourth, and fifth boxes because changing the first box clears the third and fourth (and sometimes the last) boxes as noted above.)

secant The secant method

Secant Method

The secant method uses interpolation of two data points on the curve to find the next iterate. Geometrically, the method can be explained as follows. If xi-1 and xi are iterates, then xi+1 is determined by drawing a line through the points (xi-1, f(i-1)) and (xi, f(xi)). xi+1 is the intersection of that line and the x-axis. The convergence is typically fairly fast for simple roots but slow for multiple roots (i.e. when f '(x) is also zero). In fact, the convergence rate can be shown to be the Golden ratio ≈ 1.618 if the derivative is not zero at the solution.




Newton's Method

Newton's method Newton's method

The geometric concept of this method is to draw a tangent line to the function at a xi value. The next iterate value xi+1 is at the location where the tangent line crosses the x axis. Mathematically, the formula is
xi+1 = xi - f(xi)/f '(xi). If the derivative at the solution is not 0, the method converges quadratically and the number of correct decimal places in the approximate solution about doubles with every iteration.

The method can be derived by expanding the function in a Taylor series about the solution. If x is a solution of f(x) = 0 and xi is an iterate, then
0 = f(x) = f(xi) + f '(xi)(x - xi) + f ''(xi)(x - xi)2/2 + ...
If you ignore the second and additional derivatives, solve for x, and then set x to xi+1, you get Newton's iteration formula.

Normally, the iteration converge quickly if the initial guess is close. However, if f '(x) also is 0 (a multiple root), the iterations will converge slowly.

Show proof that Newton's method converges quadratically.

Newton's Method with Approximate Derivatives

This method approximates f '(x) with (f(x + delta) - f(x))/delta where delta is 10-6. The convergence rate closely approximates Newton's method and typically converges in the same number of steps as the true Newton's method. The value of delta is arbitrary but normally works very well. A two sided approximation of f ' would could be used but does not seem necessary and would require an extra evaluation of f(x - delta).

Using the single sided approximation method is about as efficient as the regular method because the calculation of f '(x) is replaced by the calculation of f(x + delta) with an extra subtraction and division. (Calculating f(x) is required by either method.)

This app uses double precision. It would not be recommended for single precision especially not with the value of delta as small as the one used in this app.

Warning: This method is not recommended for finding a multiple root.

Snow a mathematical analysis of the approximation method.

Modified Newton's Method

Modified Newton's method Modified Newton's method used on
x * (x - 1) **2 which has a double root at x = 1. First two steps are normal Newton's method iteration. The convergence of those steps was slow and appeared to be linear so the algorithm changed to the modified method for x3 and subsequent iterates. The iteration converged in 8 iterations. Newton's method took 30 iterations.
 
In the case of double roots (f(x) = 0 and f '(x) = 0), Newton's method coverages linearly instead of quadratically meaning that the number of iterations required increases dynamically. In an attempt to rectify this, lets return to the Taylor's series expansion of f(x).
f(x) = f(xi) + f'(xi)(x - xi) + f''(xi)(x - xi)2 - ...
Lets replace f'(xi) by a series.
f'(xi) = f'(x) - f''(x)(x - xi) + ...
But f'(x) = 0 so
f'(xi) = -f''(x)(x - xi) + ...
But we can expand f''(x) in the abbreviated series
f''(x) = f''(xi) + ... to obtain
f'(xi) = -f''(xi)(x - xi) + ...
Substituting this into the expansion of f(x) we get
0 = f(x)
  = f(xi) + [-f''(xi)(x - xi) + ...](x - xi)
                + f''(xi)(x - xi)2/2 + ... 
  = f(xi) - f''(xi)(x - xi)2/2 + ...
which means
(x - xi)2 = 2f(xi)/f''(xi) + ...
If we approximate x by xi+1, then
xi+1 = xi ±√2f(xi)/f''(xi) + ...
Then we ignore the higher order derivatives. We pick the + or - to make |f(xi+1)| as small as possible. The error is in terms of
f'''(xi)(x - xi)3 inside the square root. The iterations do not converge quadratically but still much better than linearly as they would with the regular Newton's method.

There remains a question: How do we know when to use the modified method? If one looks at the iterations, the situation is pretty obvious. But how can the computer make the decision automatically? It is known that when f '(x) = 0, the regular Newton's method converges linearly in this situation. Thus
xi/xi-1 ≈ xi+1/xi.
Hence, while iterating with the regular Newton's method, if the computer observes that |xi/xi-1 - xi+1/xi| is small, then it is wise to switch to the modified method.

Unfortunately, the modified iteration does not have the nice geometrical interpretation as the regular iteration. If we look at the above formula for (x - xi)2 and ignore the "+ ..." we have a quadratic function that has zeros at the x values given by the ± in the above formula for xi+1. The plot of the modified method shows the half of this parabola between the last xi value and xi+1.

In practice, the f(xi) / f''(xi) is almost always positive in the case of double roots, allowing its square root to be calculated if xi is close enough to the solution.

Warning: When trying to solutions to functions at a multiple root, accuracy of the result is limited by numerical problems. For example, when the solving
f(x) = x(x-1)2 = x3 - 2x2 + x = 0
as shown in the picture at the beginning of this section. Clearly, x = 1 is a double root. But starting at x = 5, the iteration stopped at
x8 = 0.99999999883
because it calculated f(0.99999999883) = 0 even though double precision arithmetic was being used.

Accelerated Newton's method

Accelerated Newton's method Accelerated Newton's method
If we look at the first 3 values in the Newton's sequence for solving
f(x) = x3 - 2x2 + x = 0
we see something interesting. Let
Δn = f(xn) / f'(xn)
which means
xn+1 = xn - Δn.
  n     xn           Δn        Δnn-1
  0    2.0000
  1    1.6000      0.4000
  2    1.3474      0.2526      0.6315
  3    1.1935      0.1539      0.6093

Clearly the number of correct decimal places is not doubling with each iteration. In fact, each difference is very roughly 6/10 of the previous difference. (If we continued this process, the ratio converges to 1/2.) This means that iteration is not converging quadratically and is, in fact, showing roughly linear convergence. This suggests that we have a double root and the convergence is only about half what would be expected for quadratic convergence. So might decide to accelerate the convergence by multiplying the normal Newton's method difference by 2 and calculating x3 using
x3 = x2 - 2 * Δn = 1.3474 - 2*(0.1539) = 1.0396
We will call this process accelerated Newton's method. This acceleration is shown in the picture. The normal tangent is used at x0 and x1. However, at x2, Δ2 is doubled and x3 is much closer to the root at x = 1 than the original x3 calculated by Newton's method.

The concept can be expanded to higher order roots. For example, for triple roots, Newton's method converges linearly with Δnn-1 is roughly 2/3. With quartic roots, Newton's method converges linearly with Δnn-1 is roughly 3/4. The program tries to guess triple and quartic roots and is sometimes successful. When it does, it multiplies Δk by an acceleration factor of 3 or 4. But there are some limitations. If it guess wrong, the sequence may not converge. Even if the sequence converges, numerical issues often mean that results are not as accurate as might be desired.

The method solves simple problems such as (x - a)k = 0 for k = 2, 3, and 4 in one accelerated iteration just as the regular Newton's method solves (x - a) = 0 in one iteration.

Warning: When trying to solutions to functions at a multiple root, accuracy of the result is limited by numerical problems. For example, when the solving
f(x) = x(x-1)2 = x3 - 2x2 + x = 0
as shown in the picture at the beginning of this section. Clearly, x = 1 is a double root. But starting at x = 2, the iteration stopped at
x6 = 0.9999999995
because it calculated f(0.9999999995) = 0 even though double precision arithmetic was being used. The situation can be even worse when there is an even higher order root.

Iterating with g(x) Iterating with g(x). Note: the red curve is the original f function. The dark blue curve is g(x) which was processed by Newton's method.

Iterating with g(x) = f(x)/f '(x)

Solve (using g(x) = f(x)/f '(x)) (f ''(x) required)

There is a serious advantage of iterating with g(x) = f(x)/f'(x) when f(x) has multiple roots. Namely, g(x) has the same roots as f(x) but will only have single roots. If f(x) has a root of multiplicity m at some x value, the f '(x) will have a root of multiplicity m-1 at that point.

Unfortunately, applying Newton's method to g(x) requires a second derivative of f(x). Because, if g(x) = f(x)/f'(x), then

g'(x) = [f'(x)f'(x) - f(x)f''(x)]/f'(x)2
                  = 1 - f(x)f''(x)/f'(x)2
This apt will automatically calculate g and g' when f, f ' and f '' are provided so that Newton's method with g(x) can be carried out. The g(x) function is added to the last function box allowing the iterations with g(x) to be shown in the graph.

Warning:The initial guess may have to be closer than would be needed with the regular Newton's method. The iteration may stop at a point where f '(x) = 0 as that causes a vertical asymptote in g(x).

Warning:In the listing of iterations, the value labeled f(x) is actually g(x).

Warning: The g(x) function is often quite complicated which allows significant numerical error. For example, if we try to solve
f(x) = x(x - 1)2 = x3 - 2x2 + x = 0
starting with x = 2, the iteration stops at x = 0.9999999995 when it evaluates g(0.9999999995) = 0.
The numerical problems can be even much worse with higher order multiple roots.

Iterating with g(x) = f(x)/f '(x)

Solve (using g(x) = f(x)/f '(x)) (f ''(x) not required)

Because g '(x) is quite complicated, using approximate derivatives seems quite attractive. It normally converges in the same number of steps. The comments in the previous section are still appropriate.

Cubic Polynomial Equation Solver

(This method can also find solutions of quadric and linear equations.)

Methods for solving cubic polynomials have been studied for centuries. Unfortunately, the methods are rather complicated and seldom used. This program uses a method that transforms the general cubic into a special form t3 + pt + q = 0 which has a known (but complicated) solution. The solutions for the transformed equations then can be used to obtain solutions of the original cubic equation with analytically without iteration. The method uses code published by Alexander Shtuchkin.

Assume the cubic equation is written in the form ax3 + bx2 + cx + d = 0. To use this method, the values of a, b, c, and d must be provided in the values boxes below the graphing area and the "Solve (Cubic Method)" is clicked. The cubic and its first 2 derivatives are displayed in the function boxes and function (and optionally the derivatives) are shown in the graphing area. The solutions are displayed in the result area. There is no need to type in functions or starting values. (Quadratic and linear equations can be solved by just setting a and b = 0, as needed.)

Because clicking "Solve (Cubic Method)" writes the function and its derivatives in the first 3 function boxes, the problem can then be solved by any of the other solution methods, if desired.

Note: If the cubic example is selected, then the cubic solving method is automatically selected. (This is invoked by setting the mode in the example to 1.)

Continue iterating

Occasionally, a "Continue iterating" button will appear below the solve buttons. This happens if the iterations are terminated for some reason, but the final value of the function does not seem small enough. Often the reason is numerical accuracy and the additional iterations do not help. But in some cases they may help.

Writing the Functions

When writing functions: The operational signs are +, -, *, /, % and **. You can use ( and ) to control the order of operations and with functions.

The % operator means modulus. That is, if we have a % b we would divide a by b as integers and the result is the remainder. For example, 13 % 3 = 1 as 13 divided by 3 is 4 with a remainder of 1.   Some more examples: 11 % 5 = 1.   215 % 10 = 5

The ** operator means "to the power". Thus, 3**2 = 9, 2**3 = 8, and 10**3 = 1000.

You can use some constants: PI (π = 3.141592653589793), QUARTER_PI, HALF_PI, TWO_PI, Math.E (e = 2.718281828459045).

You can use some values: a, b, c, d, e, and s. They will be explained in the values section.

Some of the functions that can be used follow. (Note: some of these functions are not differentiable and can't be used with Newton's method.)

Order of evaluation:

  1. Inside () -- first
  2. functions
  3. **(right to left in case of ties)
  4. *, / and % (left to right in case of ties)
  5. + and - (left to right in case of ties) -- last

Illegal values like sqrt(-2) and 1/0 are ignored.

After you finish typing a function, press "Enter".

Formulas can contain comments using // and /*...*/.

Special Options

There 7 options that can enhance a graph - vertical lines, rays through the origin, circles, line segments, quadratics, points, and words (labels or text) but they cannot be used in writing the function or its derivatives. The line segment option ("L") is used to illustrate secant and Newton's method iterations. The quadratic option ("q") is used in the modified method.

Common notes:

The "x=", "o=", "r=", "l", "q","p", and "w" must be the first character(s) in the field. After that spaces are optional. In these special cases, "x", "o", "r', "l", "q", "p" and "w" can be capitalized.

The different elements are separated by ";". Failure to use ";" may cause strange errors or the item may be ignored.

You can use formulas for the numerical values. They are evaluated the same way regular formulas are evaluated - just avoid using "x", "o", or "t" in the formulas (and ";" in words).

Minimums and Maximums

Minimum x and Maximum x are the left and right end points of the x axis. If the input boxes are blank, the default values are -5 and 5.
Minimum y and Maximum y, if specified, are the end points of the y axis. If these fields are blank, appropriate values will be calculated automatically based on the y values of the function.

If "Equal spacing" is true (checked) then the mins and maxs for x and y will be adjusted as needed in order to make the equal spacing possible.

Note: After you finish typing a minimum or maximum, press "Enter". The minimum and maximum values can be formulas which are evaluated the same way functions and values are evaluated. Just don't use x, t, or o in the formulas.

Zoom In and Zoom Out

Sometimes it is useful to zoom in to see the iterations more closely. The Zoom In button does this. The domain is reduced by one fifth and centered at the last iteration (typically a solution). If both the y minimum and y maximum are specified the range is reduced in a similar manner. Otherwise the range is determined in the usual manner. Unfortunately, the iterations that are sometimes outside the new domain and range and not shown but may prevent the range from zooming in appropriately. The are two possible solutions. 1. Restart the iteration within the new range. 2. Specify the y minimum and/or y maximum to show the appropriate range.

If the problem has not already been solved, the zoom will center on what ever value x happens to have. The user can set the center by double click at the desired point.

Zoom out increases the domain by a factor of 5.

Equal Spacing

When Equal Spacing is checked, the units on the x and y axis are the same so a 45o line would look like a 45o line and circles will look like circles.

Allow Motion

For the sake of efficiency, Solver stops redrawing graphs when nothing is changed. Checking Allow motion says to keep drawing. This is useful, for example, if one uses frameCount. E.g. 5 * sin(x + frameCount/10) or if you use the "s" slider. This is rarely useful when using solver.

Show enlarged canvas and enlargement ratio

The standard canvas is medium sized to allow the user see the canvas and many of the controls at the same time without having to scroll. This is convenient while solving problems. But a larger graph is drawn above the original canvas and controls when the "Show enlarged canvas" item is checked or the "Enlargement ratio" is changed. When using this option, one will normally have to scroll up to see the larger graph and scroll down to see the original canvas and the control items. The enlarged canvas shows a magnified image of the original canvas except text elements are the same size as in the original.

Notes:

Angle Measurement

The Radians and Degrees radio buttons allow the user to specify if the trig functions and trig inverse functions assume radians or degrees. The default is radians. Radians are required when using Newton's or the modified Newton's methods.

Values

You can supply values for a, b, c, d, and e which can be used in the functions and other input boxes. The default value of these values is 0 when the input box is blank. They are evaluated the same way functions are evaluated. Just don't use x, t, or o in the formulas. See below for the special s value. You can use the value of one of the variables in another. For example, the expressions for a and b might be s/10 and 2 * a. Because one can use formulas for variables a, ...,e, the numerical values for these variables (rounded to 3 decimal places) is shown below the values.

The s Value and its Slider

The slider moves from 0 to 100 and is always an integer. If you move the slider, its value is sent to the s value input box. One can type a value from 0 to 100 in the s value input box to adjust the slider to that value. Anything else typed into that s value box is ignored and will be replaced by the value of the slider.

If the value range of the slider is not appropriate you can do something like the following: f1(x) = b * sin(x) where b value is set to s/10.

Important note: When "Allow motion" is false (not checked) the value of s is set when the mouse releases the slider but the value of s does not track the slider while the slider is moving. However, if "Allow motion" is true (checked) (and at least one function has been defined) the value of s tracks the slider.

Number of Evaluations

The initial value of 200 is almost always adequate as that means the functions are evaluated at about every other pixel in the x direction.

Show Current Setup

This button may be valuable if you have a copy of Solver.pjs on your computer. If you set up a graph that you want to use again in another session, you can click this button and dialog box will show the information needed for the current plot to add it to the setupExamples function in the solver.pjs. Some browsers (e.g. Firefox) allow you to copy the information and paste it into the function. You will have to provide an unique example number and a name for the graph. Formulas are shown in the order of the example numbers. Because some browsers do not allow copying contents of the dialog box, you will be offered a chance to save the setup information to a file. If you elect this option, your browser may offer options about what to do with the file.

If a "," is included in a "word" special option w in function boxes, it will be replaced by "^$". The substitution is required because ","s are used to separate items in the data. However, this is not a problem because the "^$" is automatically replaced by "," when used in a example.

Note: If you use this technique to save a solve using the "cubic" method, you may want to change the "0," line to "1," so that the apt will recognize that it is a cubic problem and to treat it accordingly. Otherwise, it will be treated as a regular problem which can be processed by any of the solve methods.

Save as a Temp Example

If you click this button, the current setup saved as a new example which added to the list of examples. You will be required to supply a name for the new example. The temporary example will only be available for the rest of current session.

Save as an Image File Button

After finishing a graph, it can be saved as an image file. Click the button. If the enlarged canvas is being shown, the enlarged canvas is saved. Depending on your browser, you may be asked if you want to save the file. In any case, you will probably find it your normal download folder with name solver.jpg. If you save more than one time, the multiple copies will normally be numbered. Your browser may have a button to give direct access to this folder.

Error Messages

When the user types invalid info into one of the input boxes, a message is displayed on the top of the plot area. After fixing error, normally the error message will be hidden. Occasionally it may be necessary to click anywhere in the plot area to hide the message.



James Brink, 11/30/2021.