Homework Help Question & Answers

Problem 4 (programming): Create a MATLAB function named mynewton.m to estimate the root for any a...

Problem 4 (programming): Create a MATLAB function named mynewton.m to estimate the root for any arbitrary function f given an

Problem 4 (programming): Create a MATLAB function named mynewton.m to estimate the root for any arbitrary function f given an initial guess xo, an absolute error tolerance e and a maximum number of iterations max.iter. Follow mynewton.m template posted in homework 2 folder on TritonED for guidance. You are not required to use the template. The function should return the approximated root n and the number of steps n taken to reach the solution. Use function mynewton.m to perform the following exercises. (a, b) Find the root of f(z)2 3r2 - 36 31 with o 1.5, e 10-3 and a maximum number of iterations equal to 25. Put the root in p4a and the corresponding number of steps in p4b (c. d) Find the root of f(z-2x3+3x2-36x +31 with z,-2, e 10-3 and a maximium number of iterations equal to 250. Put the root in p4c and the corresponding number of steps in p4d
0 0
ReportAnswer #1

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

Add Homework Help Answer
Add Answer of:
Problem 4 (programming): Create a MATLAB function named mynewton.m to estimate the root for any a...
Your Answer: Your Name: What's your source?
Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
More Homework Help Questions Additional questions in this topic.
Need Online Homework Help?
Ask a Question
Get FREE EXPERT Answers
WITHIN MINUTES
Related Questions