Multidimensional Newton-Raphson method: Difference between revisions
Multidimensional Newton-Raphson method (view source)
Revision as of 14:10, 4 January 2024
, 4 months ago→{{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]]
<
using System;
Line 43:
}
}
</syntaxhighlight>
<
using System;
Line 100:
}
}
</syntaxhighlight>
{{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.
<
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])
}</
{{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.
<
# (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))
</
<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.
<
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)
}</
{{out}}
Line 581:
=={{header|Nim}}==
{{trans|Kotlin}}
<
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}"</
{{out}}
Line 765:
[[Matrix_multiplication#Phix]]<br>
See std distro for a complete runnable version.
<!--<
<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>
<!--</
{{out}}
<pre>
Line 869:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
# https://github.com/pierre-vigier/Perl6-Math-Matrix
# Mastering Algorithms with Perl
Line 936:
say "Solution: ", @solution;
</syntaxhighlight>
{{out}}
<pre>
Line 946:
{{libheader|Wren-matrix}}
{{libheader|Wren-fmt}}
<
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])</
{{out}}
Line 1,031:
=={{header|zkl}}==
This doesn't use Newton-Raphson (with derivatives) but a hybrid algorithm.
<
// 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</
{{out}}
<pre>
Line 1,046:
</pre>
A condensed solver (for a different set of functions):
<
GSL.multiroot_fsolver(T( fcn(x,y){ 1.0 - x }, fcn(x,y){ 10.0*(y - x*x) }),v)
.format().println(); // --> (1,1)</
{{out}}
<pre>
Line 1,054:
</pre>
Another example:
<
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</
{{out}}
<pre>
|