Anonymous user
Roots of a function: Difference between revisions
m
Fixed lang tags.
(omit m4) |
m (Fixed lang tags.) |
||
Line 46:
=={{header|ALGOL 68}}==
Finding 3 roots using the secant method:
<lang algol68>MODE DBL = LONG REAL;▼
▲MODE DBL = LONG REAL;
FORMAT dbl = $g(-long real width, long real width-6, -2)$;
Line 116 ⟶ 115:
)
OUT printf($"No third root found"l$); stop
ESAC</lang>
Output:<lang algol68>1st root found at x = 9.1557112297752398099031e-1 (Approximately)▼
▲1st root found at x = 9.1557112297752398099031e-1 (Approximately)
2nd root found at x = 2.1844288770224760190097e 0 (Approximately)
3rd root found at x = 0.0000000000000000000000e 0 (Exactly)</
=={{header|AutoHotkey}}==
Poly(x) is a test function of one variable. <br>We search for its roots.
Line 312 ⟶ 308:
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<lang fortran>
f(x) = x*x*x - 3.0_dp*x*x + 2.0_dp*x▼
x = -1.0_dp ; step = 1.0e-6_dp ; e = 1.0e-9_dp▼
s = (f(x) > 0)▼
value = f(x)▼
IF(ABS(value) < e) THEN▼
WRITE(*,"(A,F12.9)") "Root found at x =", x▼
s = .NOT. s▼
ELSE IF ((value > 0) .NEQV. s) THEN▼
WRITE(*,"(A,F12.9)") "Root found near x = ", x▼
s = .NOT. s▼
END IF▼
x = x + step▼
END DO▼
END PROGRAM ROOTS_OF_A_FUNCTION</lang>▼
The following approach uses the Secant Method[http://en.wikipedia.org/wiki/Secant_method] to numerically find one root. Which root is found will depend on the start values x1 and x2 and if these are far from a root this method may not converge.▼
<lang fortran> INTEGER, PARAMETER :: dp = SELECTED_REAL_KIND(15)▼
INTEGER :: i=1, limit=100▼
f(x) = x*x*x - 3.0_dp*x*x + 2.0_dp*x
x1 = -1.0_dp ; x2 = 3.0_dp ; e = 1.0e-15_dp▼
DO ▼
IF (i > limit) THEN▼
WRITE(*,*) "Function not converging"▼
DO WHILE (x <
END IF
IF (ABS(d) < e) THEN▼
WRITE(*,"(A,F18.15)") "Root found at x = ", x2▼
EXIT ▼
▲The following approach uses the Secant Method[http://en.wikipedia.org/wiki/Secant_method] to numerically find one root. Which root is found will depend on the start values x1 and x2 and if these are far from a root this method may not converge.
END IF▼
x1 = x2▼
REAL(dp) :: d, e,
END DO</lang>▼
▲ EXIT
d = (x2 - x1) / (f(x2) - f(x1)) * f(x2)
▲ x1 = x2
Line 367 ⟶ 363:
J has builtin a root-finding operator, '''<tt>p.</tt>''', whose input is the (reversed) coeffiecients of the polynomial. Hence:
<lang j> 1{::p. 0 2 _3 1
We can determine whether the roots are exact or approximate by evaluating the polynomial at the candidate roots, and testing for zero:
<lang j> (0=]p.1{::p.) 0 2 _3 1
As you can see, <tt>p.</tt> is also the operator which evaluates polynomials. This is not a coincidence.
Line 399 ⟶ 395:
=={{header|Maple}}==
<lang
outputs:
<lang
which means there are three roots. Each root is named as a pair where the first element is the value (0, 1, and 2), the second one the multiplicity (=1 for each means none of the three are degenerate).
Line 416 ⟶ 412:
===Solve===
This requires a full equation and will perform symbolic operations only:
===NSolve===
This requires merely the polynomial and will perform numerical operations if needed:
(note that the results here are floats)
===FindRoot===
This will numerically try to find one(!) local root from a given starting point:
(note that there is no guarantee which one is found).
===FindInstance===
This finds a value (optionally out of a given domain) for the given variable (or a set of values for a set of given variables) that satisfy a given equality or inequality:
===Reduce===
This will (symbolically) reduce a given expression to the simplest possible form, solving equations and performing substitutions in the process:
(note that this doesn't yield a "solution" but a different expression that expresses the same thing as the original)
Line 448 ⟶ 444:
A general root finder using the False Position (Regula Falsi) method, which will find all simple roots given a small step size.
<lang ocaml>let bracket u v =
((u > 0.0) && (v < 0.0)) || ((u < 0.0) && (v > 0.0));;
Line 481 ⟶ 476:
x fx (if fx = 0.0 then "exact" else "approx") in
let f x = ((x -. 3.0)*.x +. 2.0)*.x in
List.iter showroot (search (-5.0) 5.0 0.1 f);;</lang>
Output:
|