Question

In: Advanced Math

f(x)=x^3-3x-1=0 x=[0,2] epsilon=5*10^-2 1. perform the bisection method for the root in [0,2] until your root...

f(x)=x^3-3x-1=0

x=[0,2]

epsilon=5*10^-2

1. perform the bisection method for the root in [0,2] until your root is closer to the real root within epsilon.

Let x_0=1.0, x_1=1.2

2. perform the secant method until your root is closer to the real root within epsilon.

3. do as in 2. with the Newton's method, with x_0=1.1

Solutions

Expert Solution


%%Matlab code for finding root using Newton, Secant and Bisection method
clear all
close all


%Function for which root have to find
fun=@(x) x^3-3*x-1;

%displaying the function
fprintf('\tFor the function\n')
disp(fun)

%Root using Bisection method
x0=1; x1=2; %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\n',x0,x1,root);

%Root using Secant method
x0=1; x1=2; %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\n',x0,x1,root);

%Root using Newton method
x0=1.1; %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\n',x0,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
fprintf('\nRoot using Bisection method\n')
%f(x1) should be positive
%f(x0) should be negative
k=10; count=0;
while k>5*10^-2
    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
    fprintf('\tAfter %d iteration root using Bisection method is %f\n',count,xx(count))
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]
fprintf('\nRoot using Newton method\n')
%Loop for all intial guesses
    n=5*10^-2; %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
        fprintf('\tAfter %d iteration root using Newton method is %f\n',i,xx)
    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;
fprintf('\nRoot using Secant method\n')
while k>5*10^-2
    count=count+1;
    xx=double(x1-(fun(x1)*((x1-x0)/(fun(x1)-fun(x0)))));
    x0=x1;
    x1=xx;
    k=abs(fun(xx));
    if count>=maxit
            break
    end
    fprintf('\tAfter %d iteration root using Secant method is %f\n',count,xx)
end
root=xx;
end
  
%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%


Related Solutions

USING BISECTION METHOD, FIND THE ROOT OF 0.5e^x - 5x + 2 = 0 ON THE...
USING BISECTION METHOD, FIND THE ROOT OF 0.5e^x - 5x + 2 = 0 ON THE INTERVAL [ 0 , 1 ] UP TO 3 DECIMAL PLACES. USE NEWTON'S METHOD TO APPROXIMATE THE ROOT OF f(x)=x^2-5    IN THE INTERVAL  [ 2 , 3 ] UP TO 4 DECIMAL PLACES.
find f'(x) 1. f(x)=3sinx-secx+5 _______ 2. f(x)=e^xcot(3x) _______ 3. f(x)= cos^5(3x-1) _______
find f'(x) 1. f(x)=3sinx-secx+5 _______ 2. f(x)=e^xcot(3x) _______ 3. f(x)= cos^5(3x-1) _______
Find all functions f(x) with f′′(x) = 3x^3 − 2x^2 + x, f(0) = 1, and...
Find all functions f(x) with f′′(x) = 3x^3 − 2x^2 + x, f(0) = 1, and f(1) = 1.
MatLab code for Bisection and Newton Method to find root for f(k) = p(k)^3+2*(p(k)^2)-10 on interval...
MatLab code for Bisection and Newton Method to find root for f(k) = p(k)^3+2*(p(k)^2)-10 on interval [-1,2]. P(o)=1.5, P(1)=2. Answer= 1.654249
In Sciland code, use the Newton Method to find the root of f(x)=10-x^2 and tol =10^-5
In Sciland code, use the Newton Method to find the root of f(x)=10-x^2 and tol =10^-5
Use the bisection method to approximate the root of f(x)=x-cosx in the range [0.0,1.5]. Stop when...
Use the bisection method to approximate the root of f(x)=x-cosx in the range [0.0,1.5]. Stop when the error is less than 0.002%
f(x) = ((x − 1)^2) e^x How easy would it be to apply the Bisection Method...
f(x) = ((x − 1)^2) e^x How easy would it be to apply the Bisection Method compared to Newton's method and modified Newton's method to the function f(x)? Explain.
consider the function f(x)=3x-5/sqrt x^2+1. given f'(x)=5x+3/(x^2+1)^3/2 and f''(x)=-10x^2-9x+5/(x^2+1)^5/2 a) find the local maximum and minimum...
consider the function f(x)=3x-5/sqrt x^2+1. given f'(x)=5x+3/(x^2+1)^3/2 and f''(x)=-10x^2-9x+5/(x^2+1)^5/2 a) find the local maximum and minimum values. Justify your answer using the first or second derivative test . round your answers to the nearest tenth as needed. b)find the intervals of concavity and any inflection points of f. Round to the nearest tenth as needed. c)graph f(x) and label each important part (domain, x- and y- intercepts, VA/HA, CN, Increasing/decreasing, local min/max values, intervals of concavity/ inflection points of f?
Given: f(x,y) = 5 - 3x - y for 0 < x,y < 1 and x...
Given: f(x,y) = 5 - 3x - y for 0 < x,y < 1 and x + y < 1, 0 otherwise 1) find the covariance of x and y 2) find the marginal probability density function for x c) find the probability that x >= 0.6 given that y <= 0.2
Consider f(x) = 2 + 3x^2 − x^3
Consider f(x) = 2 + 3x2 − x3 a) Find local max and min values b) Find intervals of concavity and infection points
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT