Multidimensional Newton-Raphson method: Difference between revisions

m
→‎{{header|Wren}}: Minor tidy and corrected a previous copy/paste error.
m (→‎{{header|Phix}}: added syntax colouring, made p2js compatible)
m (→‎{{header|Wren}}: Minor tidy and corrected a previous copy/paste error.)
 
(One intermediate revision by one other user not shown)
Line 8:
=={{header|C sharp|C#}}==
For matrix inversion and matrix and vector definitions - see C# source from [[Gaussian elimination]]
<langsyntaxhighlight lang="csharp">
using System;
 
Line 43:
}
}
</syntaxhighlight>
</lang>
<langsyntaxhighlight lang="csharp">
using System;
 
Line 100:
}
}
</syntaxhighlight>
</lang>
{{out}}<pre>
2.54258545959024
Line 110:
<br>
We follow the Kotlin example of coding our own matrix methods rather than using a third party library.
<langsyntaxhighlight lang="go">package main
 
import (
Line 344:
sol = solve(fs, jacob, guesses)
fmt.Printf("Approximate solutions are x = %.7f, y = %.7f, z = %.7f\n", sol[0], sol[1], sol[2])
}</langsyntaxhighlight>
 
{{out}}
Line 355:
=={{header|Julia}}==
NLsolve is a Julia package for nonlinear systems of equations, with the Newton-Raphson method one of the choices for solvers.
<langsyntaxhighlight lang="julia"># from the NLSolve documentation: to solve
# (x, y) -> ((x+3)*(y^3-7)+18, sin(y*exp(x)-1))
using NLsolve
Line 373:
 
println(nlsolve(f!, j!, [ 0.1; 1.2], method = :newton))
</langsyntaxhighlight>{{out}}
<pre>
Results of Nonlinear Solver Algorithm
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.
<langsyntaxhighlight lang="scala">// Version 1.2.31
 
import kotlin.math.abs
Line 570:
val (xx2, yy2, zz2) = solve(funcs2, jacobian2, guesses2)
System.out.printf("Approximate solutions are x = %.7f, y = %.7f, z = %.7f\n", xx2, yy2, zz2)
}</langsyntaxhighlight>
 
{{out}}
Line 581:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import sequtils, strformat, sugar
 
type
Line 751:
guesses2 = @[1.0, 1.0, 0.0]
sol2 = solve(funcs2, jacobian2, guesses2)
echo &"Approximate solutions are x = {sol2[0]:.7f}, y = {sol2[1]:.7f}, z = {sol2[2]:.7f}"</langsyntaxhighlight>
 
{{out}}
Line 765:
[[Matrix_multiplication#Phix]]<br>
See std distro for a complete runnable version.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
Line 859:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 869:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line># Reference:
# https://github.com/pierre-vigier/Perl6-Math-Matrix
# Mastering Algorithms with Perl
Line 936:
 
say "Solution: ", @solution;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 946:
{{libheader|Wren-matrix}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./matrix" for Matrix
import "./fmt" for Fmt
 
var solve = Fn.new { |funcs, jacobian, guesses|
Line 1,020:
guesses = [1, 1, 0]
sols = solve.call(funcs, jacobian, guesses)
Fmt.print("Approximate solutions are x = $.7f, y = $.7f, z = $.7f", sols[0], sols[1], sols[2])</langsyntaxhighlight>
 
{{out}}
Line 1,031:
=={{header|zkl}}==
This doesn't use Newton-Raphson (with derivatives) but a hybrid algorithm.
<langsyntaxhighlight lang="zkl">var [const] GSL=Import.lib("zklGSL"); // libGSL (GNU Scientific Library)
 
// two functions of two variables: f(x,y)=0
Line 1,039:
v.format(11,8).println(); // answer overwrites initial guess
 
fs.run(True,v.toList().xplode()).println(); // deltas from zero</langsyntaxhighlight>
{{out}}
<pre>
Line 1,046:
</pre>
A condensed solver (for a different set of functions):
<langsyntaxhighlight 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)
.format().println(); // --> (1,1)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,054:
</pre>
Another example:
<langsyntaxhighlight lang="zkl">v:=GSL.VectorFromData(1.0, 1.0, 0.0); // initial guess
fxyzs:=T(
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:
(v=GSL.multiroot_fsolver(fxyzs,v)).format(12,8).println();
 
fxyzs.run(True,v.toList().xplode()).println(); // deltas from zero</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits