aa - PHP Online
Form of PHP Sandbox
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of aa.php
- <?php
- // Function representing the equation: x^3 + 2x - (sin(x) + 1)
- function equation($x) {
- return pow($x, 3) + 2 * $x - (sin($x) + 1);
- }
- // Secant method function to approximate the root of the equation
- function secantMethod($x0, $x1, $accuracy) {
- $maxIterations = 1000;
- // Iterative loop for the Secant method
- for ($i = 0; $i < $maxIterations; ++$i) {
- // Calculate function values at current points
- $fX0 = equation($x0);
- $fX1 = equation($x1);
- // Compute the next approximation using the Secant method formula
- $x2 = $x1 - ($fX1 * ($x1 - $x0)) / ($fX1 - $fX0);
- // Check if the desired accuracy is achieved
- if (abs($x2 - $x1) < $accuracy) {
- return $x2;
- }
- // Update points for the next iteration
- $x0 = $x1;
- $x1 = $x2;
- }
- // If the method does not converge within the specified iterations
- return null;
- }
- // Case (i) X0 = 0.5, X1 = 0.6
- $x0_case1 = 0.5;
- $x1_case1 = 0.6;
- $root_case1 = secantMethod($x0_case1, $x1_case1, 1e-15);
- echo "Root for case (i): " . number_format($root_case1, 15) . "\n";
- // Case (ii) X0 = 0, X1 = 1
- $x0_case2 = 0;
- $x1_case2 = 1;
- $root_case2 = secantMethod($x0_case2, $x1_case2, 1e-15);
- echo "Root for case (ii): " . number_format($root_case2, 15) . "\n";
- ?>
File Description
- aa
- PHP Code
- 07 Feb-2024
- 1.33 Kb
You can Share it:
Latest PHP Pastes