Topic Root Finding of Equations NewtonRaphson and Secant Me
Topic: Root Finding of Equations - Newton-Raphson and Secant Method Employ Newton-Raphson method with the help of a programming tool such as Matlab, C, C++ or Python to determine a real root for f(x) = -2 + 6 x - 4 x^2 + 0.5 x^3 Use an initial guess of 4.2 Employ Secant method with the help of a programming tool such as Matlab, C, C++ or Python to determine the first positive root of f(x) = sin(x) + cos(1 + x^2) - 1 Use initial guesses of x_i-1 = 1 and x_i = 3
Solution
% Newton Raphson Method
clear all
close all
clc
% Change here for different functions
f=@(x) -2+6*x-4*x*x+0.5*x*x*x
%this is the derivative of the above function
df=@(x) 6-8*x+1.5*x*x
% Change lower limit \'a\' and upper limit \'b\'
a=4.2; b=50;
x=a;
for i=1:1:100
x1=x-(f(x)/df(x));
x=x1;
end
sol=x;
fprintf(\'Approximate Root is %.15f\',sol)
a=0;b=1;
x=a;
er(5)=0;
for i=1:1:5
x1=x-(f(x)/df(x));
x=x1;
er(i)=x1-sol;
end
plot(er)
xlabel(\'Number of iterations\')
ylabel(\'Error\')
title(\'Error Vs. Number of iterations\')
