Question

In: Advanced Math

Write Matlab programs implementing the algorithms based on bisection,Newton, and secant method for numerical solution of...

Write Matlab programs implementing the algorithms based on bisection,Newton, and secant method for numerical solution of scalar nonlinear equa-tions. Use these programs to compute approximations to real roots of the

following equations:

exp(x)−3x^2=0, (1)

x^3=x^2+x+1, (2)

exp(x) =1/(0.1 +x^2), (3)

and

x= 1 + 0.3 cos(x). (4)

Use an error tolerance tol=10^(−12). Display the obtained approximations to the roots of these equations, and compare the number of iterations, starting with the same initial values x0 for bisection and Newton methods, and with x0 and x1, where x1 was computed by bisection starting with x0, for secant method.

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) exp(x)-3*x.^2;
%fun=@(x) x-1 -0.3.*cos(x);

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

%Root using Newton method
x0=4; %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=1; x1=4; %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=1; x1=4; %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);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

fun=@(x) x.^3-x.^2-x-1;

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

%Root using Newton method
x0=4; %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=1; x1=4; %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=1; x1=4; %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);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


fun=@(x) exp(x)-1./(0.1 +x.^2);

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

%Root using Newton method
x0=4; %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; x1=4; %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; x1=4; %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);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

fun=@(x) x-1 -0.3.*cos(x);

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

%Root using Newton method
x0=4; %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; x1=4; %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; x1=4; %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>10^-12
    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=10^-12; %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>10^-12
    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 %%%%%%%%%%%%%%%%


Related Solutions

Write one a MATLAB function that implements the Bisection method, Newton’s method and Secant Method (all...
Write one a MATLAB function that implements the Bisection method, Newton’s method and Secant Method (all in one function). Your function must have the following signature function output = solve(f,options) % your code here end where the input is • f: the function in f(x) =0. options: is a struct type with the following fields o method: bisection, newton or secant tol: the tolerance for stopping the iterations. maximum_iterations: the maximum number of iterations allowed. initial_guess: that is P_0; if...
Write a C++ program for all the methods (Bisection, Newton-Raphson, Secant, False-Position, and Modified Secant) for...
Write a C++ program for all the methods (Bisection, Newton-Raphson, Secant, False-Position, and Modified Secant) for locating roots. Make sure that you have clever checks in your program to be warned and stop if you have a divergent solution or stop if the solution is very slowly convergent after a maximum number of iterations.
In MATLAB write a function secant.m to apply the secant method. function [x,i] = secant(f, x0,...
In MATLAB write a function secant.m to apply the secant method. function [x,i] = secant(f, x0, x1, tol, maxiters) [x,i] = secant(f, x0, x1, tol, maxiters) performs the secant method with f(x), starting at x_0 = x0 and x_1 = x1, and continuing until either |x_i+1 - x_i| <= tol, or maxiters iterations have been taken. The number of iterations, i, is also returned. An error is raised if the first input is not a function handle. A warning is...
To find a positive root for , write a MATLAB script file that uses Bisection method....
To find a positive root for , write a MATLAB script file that uses Bisection method. Choose any initial value that is needed. Use absolute relative approximate error to be less than 0.01. Your code should report the number of iteration and the value of x.
***Please code in Python Write another code Newton (in double precision) implementing the Newton-Raphson Method   (copy...
***Please code in Python Write another code Newton (in double precision) implementing the Newton-Raphson Method   (copy your Bisect code and modify).   Evaluation of F(x) and F'(x) should be done in a subprogram FCN(x).   The code should ask for input of: x0, TOL, maxIT (and should print output similar to Bisect code).   Debug on a simple problem, like x2−3 = 0.   Then use it to find root of F(x) in [1,2] with TOL=1.e-12. Now consider the problem of finding zeros of      ...
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
7. Finding Roots Using the Bisection Method Write a function that implements the "bisection method" for...
7. Finding Roots Using the Bisection Method Write a function that implements the "bisection method" for finding the roots of function. The signature of your function should look like def find_root(f,a,b,n): where n is the maximum number of iterations of to search for the root. The code should follow this algorithm: We are given a continuous function f and numbers a and b and with a<b with f(a)<0<f(b). From the intermediate value theorem we know that there exists a c...
Use matlab code for bisection method and regula falsi. Thank you!
Use matlab code for bisection method and regula falsi. Thank you!
Numerical Analysis: Perform the steps of the secant method to approximate the first three positive roots...
Numerical Analysis: Perform the steps of the secant method to approximate the first three positive roots of the transcendent equation x = sin (πx).
Explain the importance of error analysis in numerical methods with suitable example. Out of Bisection method...
Explain the importance of error analysis in numerical methods with suitable example. Out of Bisection method and secant method which one is better and why? Solve one application based problem using that method. Use Newton-Raphson Method to find the root of trigonometric function correct up to seven decimal places. (Trigonometric function should be complex) Solve one problem which is based on the application of Interpolation. Using numerical differentiation solve one application based problem. (Use central difference approximation and problem must...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT