%%Matlab code for plotting the function
clear all
close all
%function for which root have to find
f=@(x) x.^-2-sin(x);
%function for which root have to find
fprintf('function for which root have to find f(x)=')
disp(f)
%Interval of x for which root have to find
xx=linspace(1,4*pi,500);
yy=f(xx);
%Plotting of the function
plot(xx,yy)
xlabel('x')
ylabel('f(x)')
title('Plotting of x vs. f(x)')
grid on
fprintf('From the plot we can say roots are obtained approximately
at\n')
fprintf('\t x=1.07 and function value is %f\n',f(1.07))
fprintf('\t x=3.04 and function value is %f\n',f(3.04))
fprintf('\t x=6.308 and function value is %f\n',f(6.308))
fprintf('\t x=9.414 and function value is %f\n',f(9.414))
%Finding root for initial guess using Newton method
%All initial guess
xx0=[1.5 2 3.2 4 5 2*pi];
for i=1: length(xx0)
x0=xx0(i);
fprintf('\n\t For initial guess
x0=%2.2f\n',x0)
%root using Newton method
[root]=newton_method(f,x0,1000);
fprintf('Root using Newton method is
%f\n',root)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Matlab function for Newton Method
function [root]=newton_method(fun,x0,maxit)
syms x
g1(x) =diff(fun,x); %1st Derivative of this
function
xx=x0;
%initial guess]
%Loop for all intial guesses
n=eps; %error limit for close itteration
for i=1:maxit
x2=double(xx-(fun(xx)./g1(xx))); %Newton Raphson Formula
cc=abs(fun(x2));
%Error
err(i)=cc;
xx=x2;
if cc<=n
break
end
end
if i==maxit
warning('Maximum number
of iteration reached.\n')
end
root=xx;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
in matlab -Consider the equation f(x) = x-2-sin x = 0 on the interval x E [0.1,4 π] Use a plot to approximately locate the roots of f. To which roots do the fol- owing initial guesses converge wh...
in matlab sthat this n is hardly different from the scalar version in Section 4.3 . The root estimates are stored as columns in an array . The Newton step is calculated using a backslash. The function norm is used for the magnitude of a vector, instead magnitude of a scalar. Function 4.5,1 (netonsys) Newron's method for a system of equations function x=newtonsys (f,x1) 2 % NETONSYS Newton's method for a system of equations 3 % Input : function that...