%%Matlab code for finding root using Newton, Secant and
Bisection method
clear all
close all
%Function for which root have to find
fun=@(x) 580.*x.^4-2320.*x.^3-1160.*x.^2+6960.*x-1740;
%plotting of the function
xx=linspace(-2,4,1001);
yy=fun(xx);
plot(xx,yy)
xlabel('x')
ylabel('y')
title('f(x) vs x plot')
%displaying the function
fprintf('For the function\n')
disp(fun)
%Finding roots
p = [580 -2320 -1160 6960 -1740];
r = roots(p);
fprintf('The roots are occured at\n')
disp(r)
hold on
plot(r,fun(r),'r*')
%Root using Newton method
x0=2; %Initial guess
maxit=1000; %maximum iteration
[root]=newton_method(fun,x0,maxit);
fprintf('Root using Newton method for initial guess %f is
%2.15f.\n',x0,root);
%Root using Secant method
x0=0.3; x1=0.9; %Initial guess
maxit=1000; %maximum iteration
[root]=secant_method(fun,x0,x1,maxit);
fprintf('Root using Secant method for initial guess[%f,%f] is
%2.15f.\n',x0,x1,root);
%Root using Bisection method
x0=0.5; x1=2.9; %Initial guess
maxit=1000; %maximum iteration
[root]=bisection_method(fun,x0,x1,maxit);
fprintf('Root using Bisection method for initial guess[%f,%f] is
%2.15f.\n',x0,x1,root);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Matlab function for Bisection Method
function [root]=bisection_method(fun,x0,x1,maxit)
if fun(x0)<=0
t=x0;
x0=x1;
x1=t;
end
%f(x1) should be positive
%f(x0) should be negative
k=10; count=0;
while k>eps
count=count+1;
xx(count)=(x0+x1)/2;
mm=double(fun(xx(count)));
if mm>=0
x0=xx(count);
else
x1=xx(count);
end
err(count)=abs(fun(x1));
k=abs(fun(x1));
if count>=maxit
break
end
end
root=xx(end);
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
root=xx;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Matlab function for Secant Method
function [root]=secant_method(fun,x0,x1,maxit)
%f(x1) should be positive
%f(x0) should be negative
k=10; count=0;
while k>eps
count=count+1;
xx=double(x1-(fun(x1)*abs((x1-x0)/(fun(x1)-fun(x0)))));
x0=x1;
x1=xx;
k=abs(fun(xx));
if count>=maxit
break
end
end
root=xx;
end
%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%
Problem 2: 10 pts - Here is a cubic polynomial with three closely spaced real roots: p(x) = 580x4...
2. (a) Explain Newton's Method, which lets you improve approximations to roots of a function f(x) by following the tangent line down to the x-axis. (b) What if, instead of following a best fit straight line, you were to follow a best fit parabola? What's the equation of this parabola, and of its intersection with the x-axis? Compared with Newton's Method, how quickly do the approximate roots computed using this method typically converge to the exact root? (c) The method...
Problem 2 Determine the real roots of f(x)=-25 +82.x - 90x2 +44x-8x +0.7.x a) Graphically b) Using bisection method to determine root (0.5, 1.0) 6 = 0.1 c) False position method, Use initial guesses of x=0.5 and x. -1.0 Compute the estimated errore, with stopping criteria 1%
could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...