Matlab Matlab Matlab Matlab Matlab Matlab Matlab Matlab Matlab Matlab Matlab Mat
ID: 3210265 • Letter: M
Question
Matlab Matlab Matlab Matlab Matlab Matlab Matlab Matlab Matlab Matlab Matlab Matlab Matlab Matlab Matlab
Solve problem2 by Matlab and write comments please
Write a function myNewtonBasiniD.m with the following definition: function [x, flag, z] NewtonMethodID(f, df , x0, tol , max!ter) where · f, df are the function we are looking for the roots of and its derivative respectively ·xo is an initial approximation tol is a desired level of tolerance for approximating a root and maIter is the maximum number of iterations of Newton's method You can choose any suitable stopping criteria for your Newton steps. Your results might differ slightly depending on the stopping criterion used but should not be fundamentally different. The function should return ·x, an approximation to a root, if the Newton m ethod iterations converge; or an empty variable, if the Newton method doesn't converge and flag that is either 1 if the method converges or 0 if it does not converge or there are other issues MATH353, Spring 2018 Assignment 4 ·z, the Newton's method sequence, defined asExplanation / Answer
function [ x, flag, z ] = NewtonMethod1D( f, df, x0, tol, maxIter )
% f is input funtion
% df is derivative of input function
% x0 is initial aproximation
% tol is tolerance
% maxIter - maximum number of iterations
% x - aproximation to root
% z - error estimate
% flag - returns 1(if successful); 0 (if diverges)
if nargin ~= 5 % if number of arguments is not equal to 5
error('Invalid input. Please enter the required arguments correctly');
end
f = inline(f);% capturing entered function in f
df= inline(df);% capturing entered derivative of f in df
x(1) = x0 - (f(x0)/df(x0));% initial root
z(1) = abs(x(1)-x0);% initial error
k = 1;% initiating counter k
while (z(k) >= tol) && (k <= maxIter) % satisfying criteria for tolerance and iterations
x(k+1) = x(k) - (f(x(k))/df(x(k)));
z(k+1) = abs(x(k)-x(k));
k= k+1;
flag = 1;% Shows - no issue with solution
else
flag = 0;% Shows - issue with solution
end
for i = 2:1:(k-1)
Ratio1(i) = z(i)/z(i-1);% ratio of errors in consecutive iterations
Ratio2(i) = z(i)/(z(i-1))^2;% ratio of errors in current to square of previous
end
Ratio1(1) = 'N/A'; Ratio2(1) = 'N/A';% defining initial ratio 1 and 2 to 'N/A'
table(k, Ratio1, Ratio2) % Displaying table for ratio 1 and 2
end % End of function