---
# System prepended metadata

title: Newton's Method
tags: [python, student homework]

---

{%hackmd @RintarouTW/About %}

# Newton's Method

![](https://i.imgur.com/CrGxDm8.png)


$$
f(z) = z^2 \implies f'(z) = 2z\\
\Delta y = z^2 - x\\
f'(z) = 2z = \frac{\Delta y}{\Delta z} \implies \Delta z = \frac{\Delta y}{2z}\\
z_{n+1} = z_{n} - \Delta z
$$


```!=Go
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`