Monday, April 8, 2013

MATLAB - Secant Method

secant
% _________________________________________________________________________
% To get accurate root of given equation by Secant method
% By Mahesha MG, MIT
% Date: 09/04/2013
% _________________________________INPUT___________________________________
clc;
clear;
y = inline('x^2+x-2.5');                                          %Equation
x0=input('Enter first approximate root: ');                     %Example: 3
x1=input('Enter second approximate root: ');                    %Example: 4
e=input('Enter the accuracy: ');                             %Example: 1e-5
% _____________________________Secant method_______________________________
while abs(feval(y,x1))>e
x2=((x0*feval(y,x1))-(x1*feval(y,x0)))/(feval(y,x1)-feval(y,x0));
x0=x1;
x1=x2;
end
% _________________________________________________________________________
root=x2

3 comments:

  1. Dear Sir,
    Would you please give me a Secant Method with function and while loop.

    ReplyDelete
    Replies
    1. Above program uses while loop. Kindly elaborate your requirement.

      Delete
  2. could you please help me write a code involving secant method and while loop for f(x)=cosx-x, root should be within 10^-12

    ReplyDelete