Matlab help with Newton's method

121 Views Asked by At

I'm doing something wrong. I don't know what. I get random numbers instead of anything that converges. So, this is supposed to converge to $\sqrt{x}$ ($\sqrt{3}$ here, which is approx. $1.732$).

x = .5;
b = 3;
for i = 1:25
   x = x - (x.^(1/2)-b)/((x.^(-1/2)/2)-1)
end
1

There are 1 best solutions below

1
On

If we want to find $x = \sqrt{b}$, then we let $f(x) = x^2 - b$ and use Newton's method to find where $f(x)$ is equal to $0$:

$$ x_{i+1} = x_i - { {f(x_i)} \over {f'(x_i)} } $$

For this case, $f'(x) = 2x$, so

$$ x_{i+1} = x_i - { {x_i^2 - b} \over {2x_i} } $$

In Matlab, this would look something like:

x = 0.5;
b = 3;
for i = 1 : 25
  x = x - (x^2 - b) / (2*x);
end