Core JavaScript

Finding a Zero with a Fixed Point

The JavaScript code example demonstrates how to find a zero of a function using a fixed-point algorithm.

FindingAZeroWithAFixedPoint.html

<!DOCTYPE html>
<html>
	<head>
		<title>XoaX.net's Javascript</title>
		<style>
			table {
				background-color:white;
			}
		</style>
	</head>
	<body>
		<script type="text/javascript" src="FindingAZeroWithAFixedPoint.js"></script>
	</body>
</html>

FindingAZeroWithAFixedPoint.js

FindZeroAtPi();

function FindZeroAtPi() {
	let dX0 = 0.0;
	let dX1 = 4.0;
	document.writeln('<table cellspacing="5" cellpadding="5" border="3">');
	document.writeln('<thead><tr><th>X0</th><th>X1</th></tr></thead>');

	while (Math.abs(dX0 - dX1) > 1.0e-15) {
		dX0 = dX1;
		dX1 = FixedPoint(dX1);
		document.writeln('<tr><td>'+dX0+'</td><td>'+dX1+'</td></tr>');
	}

	document.writeln('</table>');
}

// Given y = sin(x)
// To find sin(x) = 0, add x to both sides to get x = sin(x) + x.
// Now, Pi is a fixed point of y = sin(x) + x
// Note that y'(pi) = 0 < 1. So, the algorithm converges ... rapidly.
// Let x(n) = sin(x(n-1)) + x(n-1)
function FixedPoint(dX) {
	return Math.sin(dX) + dX;
}

 

Output

 
 

© 2007–2025 XoaX.net LLC. All rights reserved.