In: Advanced Math
Study the roots of the nonlinear equation f(x) = cos(x) + (1 /(1 + e^2x)) both theoretically and numerically. (a) Plot f(x) on the interval x ∈ [−15, 15] and describe the overall behaviour of the function as well as the number and location of its roots. Use the “zoom” feature of Matlab’s plotting window (or change the axis limits) in order to ensure that you are identifying all roots – you may have to increase your plotting point density in order to see sufficient detail!
Please provide MATLAB code as well.
%%Matlab code for finding root using newton secant bisection and
false
clear all
close all
%function for which root have to find
fun=@(x) cos(x)+(1./(1+exp(2*x)));
fprintf('function for which root have to find f(x)=')
disp(fun)
xx=linspace(-15,15,100001);
yy=fun(xx);
plot(xx,yy)
xlabel('x')
ylabel('f(x)')
title('x vs. f(x) plot')
grid on
x0=-10;
[root]=newton_method(fun,x0,1000);
hold on
plot(root,fun(root),'r*')
x0=-5;
[root]=newton_method(fun,x0,1000);
hold on
plot(root,fun(root),'r*')
x0=2;
[root]=newton_method(fun,x0,1000);
hold on
plot(root,fun(root),'r*')
x0=5;
[root]=newton_method(fun,x0,1000);
hold on
plot(root,fun(root),'r*')
x0=8;
[root]=newton_method(fun,x0,1000);
hold on
plot(root,fun(root),'r*')
x0=10;
[root]=newton_method(fun,x0,1000);
hold on
plot(root,fun(root),'r*')
x0=13;
[root]=newton_method(fun,x0,1000);
hold on
plot(root,fun(root),'r*')
%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]
fprintf('\nRoot using Newton method for initial guess
%f\n',x0)
%Loop for all intial guesses
n=5*10^-15; %error limit for close
itteration
for i=1:maxit
x2=double(xx-(fun(xx)./g1(xx))); %Newton Raphson Formula
cc=abs(double(fun(x2)));
%Error
err(i)=cc;
xx=x2;
if cc<=n
break
end
end
fprintf('\tAfter %d iteration root using Newton
method is %f\n',i,xx)
root=xx;
end
%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%