MATLAB Code:
close all
clear
clc
f = @(x) 2*x^3 + 3*x^2 - 36*x + 31; % Input Function
x0 = 1.5; tol = 1e-3; max_iter = 25;
[p4a, p4b] = newton(f, x0, tol, max_iter)
x0 = 2; tol = 1e-3; max_iter = 250;
[p4c, p4d] = newton(f, x0, tol, max_iter)
function [x,n] = newton(f, x0, tol, max_iter)
n = 0;
x = x0;
h = 1e-6;
while true
n = n + 1;
fd = (f(x + h) - f(x - h)) / (2*h); % Central finite difference of
f at x
x = x - f(x)/fd; % Newton update
if n >= max_iter || abs(f(x)) < tol % Termination
condition
break;
end
end
end
Output:
p4a =
1.0000
p4b =
4
p4c =
2.8807
p4d =
56
Problem 4 (programming): Create a MATLAB function named mynewton.m to estimate the root for any a...