0% found this document useful (0 votes)
96 views

Matlab Code For Bisection and Newton Method PDF

The document provides code for implementing the bisection method and Newton's method in MATLAB. For the bisection method, it defines the function, initial interval, tolerance, and iterates to find a root by bisecting the interval and checking if the function values on each side have opposite signs. For Newton's method, it defines the function, derivative, initial guess, tolerance, iterates to find a root by updating the guess as the value minus the function divided by the derivative, and breaks when successive guesses are within the tolerance.

Uploaded by

John Wick
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

Matlab Code For Bisection and Newton Method PDF

The document provides code for implementing the bisection method and Newton's method in MATLAB. For the bisection method, it defines the function, initial interval, tolerance, and iterates to find a root by bisecting the interval and checking if the function values on each side have opposite signs. For Newton's method, it defines the function, derivative, initial guess, tolerance, iterates to find a root by updating the guess as the value minus the function divided by the derivative, and breaks when successive guesses are within the tolerance.

Uploaded by

John Wick
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

LAB WORK # 2

Write a MATLAB M-file for bisection method.

F=@(x) x^2-exp(-x);
a= 0;
b=1;
Fa= F(a);
Fb = F(b);
max_iterations = 20;
tolerance = 0.002;
for i= 1: max_iterations
c = (a+b)/2;
Fc =F(c);
if i>1
tol = abs((c-g)/c);
if tol < tolerance
break
end
end
g = c;
if F(a)*F(c)<0
b = c;
else
a = c;
end
end
c

Write a MATLAB M-file for Newton Method.

F=@(x) sqrt(x) + (x.^2) -7;


FD =@(x) 1/(2*sqrt(x))+(2*x);
Xi = 7;
% F = Function
% FD = Derivative of Function
% Xi =Initial Guess
Tolerance = 0.0002;
max_iterations = 20;
for i= 1: max_iterations
Xnew = Xi - F(Xi)/(FD(Xi));
if abs((Xnew-Xi)/Xi) < Tolerance
Xans = Xnew;
break
end

1
Xi = Xnew;
Xans = Xnew;
end
Xans

You might also like