學海無涯,思境無維;數乃六藝,理之始也。
或有一得足矣 愚千慮

Newton's Method

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

f(z)=z2f(z)=2zΔy=z2xf(z)=2z=ΔyΔzΔz=Δy2zzn+1=znΔz

package main

import (
	"fmt"
)

// x is the number we want to know it's sqaure root.
func Sqrt(x float64) float64 {

	z := 2.0 // z is the initial value we guess z^2 approches x

	for i := 0; i < 10; i++ {

		derivative := 2 * z  // since f(z) = z^2, then f'(z) = 2 * z
		delta_y := z * z - x
		delta_z := delta_y / derivative
		z = z - delta_z
		
		// Above codes are equal to the following one line code
		// z -= (z * z - x) / (2 * z)
		fmt.Println(z)
	}
	
	return z
}

func main() {
	
	fmt.Println(Sqrt(2))
}
tags: student homework python