Golden ratio/Convergence: Difference between revisions

(Golden ratio/Convergence in Odin)
Line 1,341:
/* definitions */
iterate :: proc() {
count : u32 = 0
phi0: f64 = 1.0
difference: f64 = 1.0
Line 1,347:
fmt.println("\nGolden ratio/Convergence")
fmt.println("-----------------------------------------")
for (1.0e-5 < difference) {
phi1 = 1.0 + (1.0 / phi0)
difference = abs((phi1 - phi0))
phi0 = phi1
count += 1
fmt.printf("Iteration: %2d : iterationsEstimate ==>: %.8f\n", count, phi1)
}
fmt.println("-----------------------------------------")
Line 1,358:
fmt.printf("\nThe error is approximately %.10f\n", (phi1 - (0.5 * (1.0 + math.sqrt_f64(5.0)))))
}
 
</syntaxhighlight>
{{out}}
Line 1,363 ⟶ 1,364:
Golden ratio/Convergence
-----------------------------------------
Iteration: 01 : iterationsEstimate ==>: 2.00000000
Iteration: 02 : iterationsEstimate ==>: 1.50000000
Iteration: 03 : iterationsEstimate ==>: 1.66666667
Iteration: 04 : iterationsEstimate ==>: 1.60000000
Iteration: 05 : iterationsEstimate ==>: 1.62500000
Iteration: 06 : iterationsEstimate ==>: 1.61538462
Iteration: 07 : iterationsEstimate ==>: 1.61904762
Iteration: 08 : iterationsEstimate ==>: 1.61764706
Iteration: 09 : iterationsEstimate ==>: 1.61818182
Iteration: 10 : iterationsEstimate ==>: 1.61797753
Iteration: 11 : iterationsEstimate ==>: 1.61805556
Iteration: 12 : iterationsEstimate ==>: 1.61802575
Iteration: 13 : iterationsEstimate ==>: 1.61803714
Iteration: 14 : iterationsEstimate ==>: 1.61803279
-----------------------------------------
Result: 1.61803279 after 14 iterations
37

edits