Problem 3. Newton\'s method is a very efficient iterative method for finding the
ID: 3168379 • Letter: P
Question
Problem 3. Newton's method is a very efficient iterative method for finding the root of the algebraic equation f(x) = 0 (it also works for systems of equations). The procedure is the following: choose some number Po that you suspect is close to some root p of the equation f(z) = 0; let {Pn}n=o be the sequence defined by m) If the sequence {Pn} converges, then its limit, linn p , is a root of f(x) = 0. (a) Write a (MATLAB) code newton, m which solves a single equation f(x) = 0 by using Newton's method. You will solve the equation f(z) =2+3x2-4x-12=0, whose roots are -3, -2, and 2. The code will perform Newton's iteration starting from Po and will iterate until the distance between the iterates Pn 1 and pn is no more than e; the function prints all intermediate steps (not just the final result). At each step of the Newton's iteration, the code prints out the number n, the value pn, the difference Pn-Pn-1Explanation / Answer
newton.m
function [] = newton(f,f_d,eps,p0)
i = 0;
p = p0;
fprintf('Iter: %d, p: %f ', i, p);
while true
i = i + 1;
p_ = p;
p = p - f(p)/f_d(p);
fprintf('Iter: %d, p: %f ', i, p);
if abs(p_ - p) <= eps
break;
end
end
end
code.m
clear
clc
syms x;
f(x) = x^3 + 3*x^2 - 4*x - 12;
f_d(x) = diff(f);
fprintf('For p0 = -0.1 ');
newton(f,f_d,1e-14,-0.1);
fprintf('For p0 = -2.45 ');
newton(f,f_d,1e-14,-2.45);
fprintf('For p0 = 5 ');
newton(f,f_d,1e-14,5);
output of code.m
For p0 = -0.1
Iter: 0, p: -0.100000
Iter: 1, p: -2.631947
Iter: 2, p: -3.720440
Iter: 3, p: -3.254044
Iter: 4, p: -3.050960
Iter: 5, p: -3.002820
Iter: 6, p: -3.000009
Iter: 7, p: -3.000000
Iter: 8, p: -3.000000
Iter: 9, p: -3.000000
For p0 = -2.45
Iter: 0, p: -2.450000
Iter: 1, p: -0.859567
Iter: 2, p: -1.865247
Iter: 3, p: -1.989571
Iter: 4, p: -1.999920
Iter: 5, p: -2.000000
Iter: 6, p: -2.000000
Iter: 7, p: -2.000000
For p0 = 5
Iter: 0, p: 5.000000
Iter: 1, p: 3.336634
Iter: 2, p: 2.422009
Iter: 3, p: 2.062322
Iter: 4, p: 2.001677
Iter: 5, p: 2.000001
Iter: 6, p: 2.000000
Iter: 7, p: 2.000000
Iter: 8, p: 2.000000
As you can see the final values are the roots: -3, -2 and 2.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.