Multidimensional Newton-Raphson method: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added syntax colouring, made p2js compatible) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 8: | Line 8: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
For matrix inversion and matrix and vector definitions - see C# source from [[Gaussian elimination]] |
For matrix inversion and matrix and vector definitions - see C# source from [[Gaussian elimination]] |
||
< |
<syntaxhighlight lang="csharp"> |
||
using System; |
using System; |
||
Line 43: | Line 43: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
< |
<syntaxhighlight lang="csharp"> |
||
using System; |
using System; |
||
Line 100: | Line 100: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}}<pre> |
{{out}}<pre> |
||
2.54258545959024 |
2.54258545959024 |
||
Line 110: | Line 110: | ||
<br> |
<br> |
||
We follow the Kotlin example of coding our own matrix methods rather than using a third party library. |
We follow the Kotlin example of coding our own matrix methods rather than using a third party library. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 344: | Line 344: | ||
sol = solve(fs, jacob, guesses) |
sol = solve(fs, jacob, guesses) |
||
fmt.Printf("Approximate solutions are x = %.7f, y = %.7f, z = %.7f\n", sol[0], sol[1], sol[2]) |
fmt.Printf("Approximate solutions are x = %.7f, y = %.7f, z = %.7f\n", sol[0], sol[1], sol[2]) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 355: | Line 355: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
NLsolve is a Julia package for nonlinear systems of equations, with the Newton-Raphson method one of the choices for solvers. |
NLsolve is a Julia package for nonlinear systems of equations, with the Newton-Raphson method one of the choices for solvers. |
||
< |
<syntaxhighlight lang="julia"># from the NLSolve documentation: to solve |
||
# (x, y) -> ((x+3)*(y^3-7)+18, sin(y*exp(x)-1)) |
# (x, y) -> ((x+3)*(y^3-7)+18, sin(y*exp(x)-1)) |
||
using NLsolve |
using NLsolve |
||
Line 373: | Line 373: | ||
println(nlsolve(f!, j!, [ 0.1; 1.2], method = :newton)) |
println(nlsolve(f!, j!, [ 0.1; 1.2], method = :newton)) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
Results of Nonlinear Solver Algorithm |
Results of Nonlinear Solver Algorithm |
||
Line 392: | Line 392: | ||
As neither the JDK nor the Kotlin Standard Library have matrix functions built in, most of the functions used have been taken from other tasks. |
As neither the JDK nor the Kotlin Standard Library have matrix functions built in, most of the functions used have been taken from other tasks. |
||
< |
<syntaxhighlight lang="scala">// Version 1.2.31 |
||
import kotlin.math.abs |
import kotlin.math.abs |
||
Line 570: | Line 570: | ||
val (xx2, yy2, zz2) = solve(funcs2, jacobian2, guesses2) |
val (xx2, yy2, zz2) = solve(funcs2, jacobian2, guesses2) |
||
System.out.printf("Approximate solutions are x = %.7f, y = %.7f, z = %.7f\n", xx2, yy2, zz2) |
System.out.printf("Approximate solutions are x = %.7f, y = %.7f, z = %.7f\n", xx2, yy2, zz2) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 581: | Line 581: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="nim">import sequtils, strformat, sugar |
||
type |
type |
||
Line 751: | Line 751: | ||
guesses2 = @[1.0, 1.0, 0.0] |
guesses2 = @[1.0, 1.0, 0.0] |
||
sol2 = solve(funcs2, jacobian2, guesses2) |
sol2 = solve(funcs2, jacobian2, guesses2) |
||
echo &"Approximate solutions are x = {sol2[0]:.7f}, y = {sol2[1]:.7f}, z = {sol2[2]:.7f}"</ |
echo &"Approximate solutions are x = {sol2[0]:.7f}, y = {sol2[1]:.7f}, z = {sol2[2]:.7f}"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 765: | Line 765: | ||
[[Matrix_multiplication#Phix]]<br> |
[[Matrix_multiplication#Phix]]<br> |
||
See std distro for a complete runnable version. |
See std distro for a complete runnable version. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Multidimensional_Newton-Raphson_method.exw</span> |
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Multidimensional_Newton-Raphson_method.exw</span> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
Line 859: | Line 859: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span> |
||
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span> |
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 869: | Line 869: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
<lang |
<syntaxhighlight lang="raku" line># Reference: |
||
# https://github.com/pierre-vigier/Perl6-Math-Matrix |
# https://github.com/pierre-vigier/Perl6-Math-Matrix |
||
# Mastering Algorithms with Perl |
# Mastering Algorithms with Perl |
||
Line 936: | Line 936: | ||
say "Solution: ", @solution; |
say "Solution: ", @solution; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 946: | Line 946: | ||
{{libheader|Wren-matrix}} |
{{libheader|Wren-matrix}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/matrix" for Matrix |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 1,020: | Line 1,020: | ||
guesses = [1, 1, 0] |
guesses = [1, 1, 0] |
||
sols = solve.call(funcs, jacobian, guesses) |
sols = solve.call(funcs, jacobian, guesses) |
||
Fmt.print("Approximate solutions are x = $.7f, y = $.7f, z = $.7f", sols[0], sols[1], sols[2]</ |
Fmt.print("Approximate solutions are x = $.7f, y = $.7f, z = $.7f", sols[0], sols[1], sols[2]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,031: | Line 1,031: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
This doesn't use Newton-Raphson (with derivatives) but a hybrid algorithm. |
This doesn't use Newton-Raphson (with derivatives) but a hybrid algorithm. |
||
< |
<syntaxhighlight lang="zkl">var [const] GSL=Import.lib("zklGSL"); // libGSL (GNU Scientific Library) |
||
// two functions of two variables: f(x,y)=0 |
// two functions of two variables: f(x,y)=0 |
||
Line 1,039: | Line 1,039: | ||
v.format(11,8).println(); // answer overwrites initial guess |
v.format(11,8).println(); // answer overwrites initial guess |
||
fs.run(True,v.toList().xplode()).println(); // deltas from zero</ |
fs.run(True,v.toList().xplode()).println(); // deltas from zero</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,046: | Line 1,046: | ||
</pre> |
</pre> |
||
A condensed solver (for a different set of functions): |
A condensed solver (for a different set of functions): |
||
< |
<syntaxhighlight lang="zkl">v:=GSL.VectorFromData(-10.0, -15.0); |
||
GSL.multiroot_fsolver(T( fcn(x,y){ 1.0 - x }, fcn(x,y){ 10.0*(y - x*x) }),v) |
GSL.multiroot_fsolver(T( fcn(x,y){ 1.0 - x }, fcn(x,y){ 10.0*(y - x*x) }),v) |
||
.format().println(); // --> (1,1)</ |
.format().println(); // --> (1,1)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,054: | Line 1,054: | ||
</pre> |
</pre> |
||
Another example: |
Another example: |
||
< |
<syntaxhighlight lang="zkl">v:=GSL.VectorFromData(1.0, 1.0, 0.0); // initial guess |
||
fxyzs:=T( |
fxyzs:=T( |
||
fcn(x,y,z){ x*x*9 + y*y*36 + z*z*4 - 36 }, // 9x^2 + 36y^2 + 4z^2 - 36 = 0 |
fcn(x,y,z){ x*x*9 + y*y*36 + z*z*4 - 36 }, // 9x^2 + 36y^2 + 4z^2 - 36 = 0 |
||
Line 1,061: | Line 1,061: | ||
(v=GSL.multiroot_fsolver(fxyzs,v)).format(12,8).println(); |
(v=GSL.multiroot_fsolver(fxyzs,v)).format(12,8).println(); |
||
fxyzs.run(True,v.toList().xplode()).println(); // deltas from zero</ |
fxyzs.run(True,v.toList().xplode()).println(); // deltas from zero</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |