Golden ratio/Convergence: Difference between revisions

Content added Content deleted
(Golden ratio/Convergence in Odin)
Line 1,341: Line 1,341:
/* definitions */
/* definitions */
iterate :: proc() {
iterate :: proc() {
count: u32 = 0
count := 0
phi0: f64 = 1.0
phi0: f64 = 1.0
difference: f64 = 1.0
difference: f64 = 1.0
Line 1,347: Line 1,347:
fmt.println("\nGolden ratio/Convergence")
fmt.println("\nGolden ratio/Convergence")
fmt.println("-----------------------------------------")
fmt.println("-----------------------------------------")
for (1.0e-5 < difference) {
for 1.0e-5 < difference {
phi1 = 1.0 + (1.0 / phi0)
phi1 = 1.0 + (1.0 / phi0)
difference = abs((phi1 - phi0))
difference = abs(phi1 - phi0)
phi0 = phi1
phi0 = phi1
count += 1
count += 1
fmt.printf("Iteration: %2d : iterations ==> %.8f\n", count, phi1)
fmt.printf("Iteration %2d : Estimate : %.8f\n", count, phi1)
}
}
fmt.println("-----------------------------------------")
fmt.println("-----------------------------------------")
Line 1,358: Line 1,358:
fmt.printf("\nThe error is approximately %.10f\n", (phi1 - (0.5 * (1.0 + math.sqrt_f64(5.0)))))
fmt.printf("\nThe error is approximately %.10f\n", (phi1 - (0.5 * (1.0 + math.sqrt_f64(5.0)))))
}
}

</syntaxhighlight>
</syntaxhighlight>
{{out}}
{{out}}
Line 1,363: Line 1,364:
Golden ratio/Convergence
Golden ratio/Convergence
-----------------------------------------
-----------------------------------------
Iteration: 01 : iterations ==> 2.00000000
Iteration 01 : Estimate : 2.00000000
Iteration: 02 : iterations ==> 1.50000000
Iteration 02 : Estimate : 1.50000000
Iteration: 03 : iterations ==> 1.66666667
Iteration 03 : Estimate : 1.66666667
Iteration: 04 : iterations ==> 1.60000000
Iteration 04 : Estimate : 1.60000000
Iteration: 05 : iterations ==> 1.62500000
Iteration 05 : Estimate : 1.62500000
Iteration: 06 : iterations ==> 1.61538462
Iteration 06 : Estimate : 1.61538462
Iteration: 07 : iterations ==> 1.61904762
Iteration 07 : Estimate : 1.61904762
Iteration: 08 : iterations ==> 1.61764706
Iteration 08 : Estimate : 1.61764706
Iteration: 09 : iterations ==> 1.61818182
Iteration 09 : Estimate : 1.61818182
Iteration: 10 : iterations ==> 1.61797753
Iteration 10 : Estimate : 1.61797753
Iteration: 11 : iterations ==> 1.61805556
Iteration 11 : Estimate : 1.61805556
Iteration: 12 : iterations ==> 1.61802575
Iteration 12 : Estimate : 1.61802575
Iteration: 13 : iterations ==> 1.61803714
Iteration 13 : Estimate : 1.61803714
Iteration: 14 : iterations ==> 1.61803279
Iteration 14 : Estimate : 1.61803279
-----------------------------------------
-----------------------------------------
Result: 1.61803279 after 14 iterations
Result: 1.61803279 after 14 iterations