Question

In: Advanced Math

These instructions are written with the assumption that code will be done in matlab. You might...

These instructions are written with the assumption that code will be done in matlab. You might find the following built in commands useful: length, plot, xlabel, ylabel, title, legend, fzero, plot, disp, axis, axes, min, max.

3. Root finding; Bisection, Secant, Newton. a) Write your own version of the bisection method to solve e 2x − 10x = 0. Assume the root is in the interval [0, 1]. Try to use the matlab command ”fzero” to accomplish the same thing. b) Write your own version of secant method to solve e −2x − 7x = 0. Assume the root is in the interval [1/9, 2/3]. Try to use the matlab command ”fzero” to accomplish the same thing. c) Write your own version of Newton’s method to solve x + sin(x) − cos(2x) = 0.

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(2*x)-10*x;

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

%Finding roots
r1=fzero(fun,0);
fprintf('The root using fzero is %f.\n',r1)

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

%Function for which root have to find
fun=@(x) exp(-2*x)-7*x;

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

%Finding roots
r1=fzero(fun,1);
fprintf('The root using fzero is %f.\n',r1)

%Root using Secant method
x0=1/9; x1=2/3; %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);

%Function for which root have to find
fun=@(x) x+sin(x)-cos(2*x);

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

%Finding roots
r1=fzero(fun,2);
fprintf('The root using fzero is %f.\n',r1)


%Root using Newton method
x0=0; %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
%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)*((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

These instructions are written with the assumption that code will be done in matlab. You might...
These instructions are written with the assumption that code will be done in matlab. You might find the following built in commands useful: length, plot, xlabel, ylabel, title, legend, fzero, plot, disp, axis, axes, min, max. 2. Numerical Integration (Quadrature). Write FOUR of your own numerical integration routines. One should use left-end, right-end, or mid-points, another should use the trapezoid method, another should use Simpson’s method, and the fourth should use either Guassian Quadrature or Romberg’s method. Use your four...
The following code must be written using matlab How to loop through a vector in matlab...
The following code must be written using matlab How to loop through a vector in matlab and assigning a value to every 4th entry. The vector could be of any length. Thanks
This code is to be written in Matlab. Write a function that will plot cos(x) for...
This code is to be written in Matlab. Write a function that will plot cos(x) for x values ranging from -pi to pi in steps of 0.1, using black *'s. It will do this three times across in one Figure Window, with varying line widths. If no arguments are passed to the function, the line widths will be 1, 2, and 3. If on the other hand, an argument is passed to the function, it is multiplier for these values....
I need code written in java for one of my projects the instructions are Write a...
I need code written in java for one of my projects the instructions are Write a program that interacts with the user via the console and lets them choose options from a food menu by using the associated item number. It is expected that your program builds an <orderString> representing the food order to be displayed at the end. (See Sample Run Below). Please note: Each student is required to develop their own custom menus, with unique food categories, items...
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals for position 1, position 2, position3 and position 4 of the array. After these first 4 positions are drawn. The whole thing should start over where position5 drawn from same interval as positions 1, position6 drawn from same interval as...
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals in chunks of 4 , that is chunk1-chunk2-chunk3-chunk4 The parameters for specifying the lintervals by which the random numbers should be drawn should be able to change and be hardcoded in the script, however, be hardcoded in the script.
Implement the steffenson method in matlab. The code must be in MATLAB DOnt answer if you...
Implement the steffenson method in matlab. The code must be in MATLAB DOnt answer if you cannot give correct MATLAB
Written Analysis Instructions As part your course requirements you are required to complete a Written Analysis...
Written Analysis Instructions As part your course requirements you are required to complete a Written Analysis of Data. The Analysis, 1 ½ to 2 pages in length (typed and double spaced), is worth 300 points. The maximum number of points you can obtain from the written assignment is 300. For this assignment you will be using be using both your creative writing and your analytical skills. Below you will find two sets of data set forth in two different tables....
Matlab code for Gauss Siedel with solved example in Matlab
Matlab code for Gauss Siedel with solved example in Matlab
solve in MATLAB and screenshot code ?′′ −??′ +??= ???(????−?????)
solve in MATLAB and screenshot code ?′′ −??′ +??= ???(????−?????)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT