In: Advanced Math
Please use python or matlab. The function e^x −100x^2 =0 has three true solutions.Use secant method to locate the solutions with tolerance 10^(−10).
%Matlab code for Secant method
clear all
close all
%Function for which root have to find
f=@(x) exp(x)-100.*x.^2;
fprintf('For the function f(x)= ')
disp(f)
%finding root using Secant Method
e=10^-10; %error convergence
max=50000; %Maximum iterations
a=[-10 -5]; %initial guess
%Root using Secant method
[root]=secant_method(f, a, e, max);
fprintf('Finding root using Secant method\n')
fprintf('\tThe root for this function for initial guess [%d %d] is
%f\n\n',a(1),a(2),root)
a=[2 10]; %initial guess
%Root using Secant method
[root]=secant_method(f, a, e, max);
fprintf('\tThe root for this function for initial guess [%d %d] is %f\n\n',a(1),a(2),root)
a=[1 2]; %initial guess
%Root using Secant method
[root]=secant_method(f, a, e, max);
fprintf('\tThe root for this function for initial guess [%d %d] is %f\n\n',a(1),a(2),root)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Matlab function for Secant Method
function [root]=secant_method(f, a, e, max)
%a= vector of two initial guess
%e= error tollerence
%max= maximum number of iterations
%x0 and x1 values for two initial guess
x0=a(1); x1=a(2);
%checking wheather function values for two
initial guesses are same
if f(x0)==f(x1)
msg = 'Function values
are identical at initial guesses.';
error(msg);
end
%loop for Secant iterations
k=0;
for i=1:max
k=k+1;
xx=double(x1-(f(x1)*abs((x1-x0)/(f(x1)-f(x0)))));
x0=x1;
x1=xx;
if(abs(xx-x0)<=e)
break
end
end
root=xx;
%If maximum iteration reached
if i==max
fprintf('Has reached
maximum iteration steps allowed.\n')
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%