Professional Documents
Culture Documents
Objective
To get familiarized with Secant Method to find the root of non-linear equation and a small
introduction to GUI in Matlab.
Theory
o Secant Method
In numerical analysis, the secant method is a root-finding algorithm that uses a succession of
roots of secant lines to better approximate a root of a function f . The secant method can be
thought of as a finite-difference approximation of Newton's method.
f ( xi 1 ) f ( xi )
f ( xi )
xi 1 xi
This approximation can be substituted to yield the following iterative equation:
f ( xi )( xi 1 xi )
xi 1 xi
f ( xi 1 ) f ( xi )
This is the formula for the secant method. Notice that the approach requires two initial
estimates of x
Demonstration
Problem Statement: Use the secant method to estimate the root of f ( x) e x x . Start with initial
estimates of x1 0 and x0 1 .
clc
clear all
close all
Lab sheet prepared by Md. Arif Hossain, Lecturer (EEE), Islamic University of Technology (IUT)
%Prompt the user to give the function
f=inline(input('Enter function:','s'));
%Run the algorithm until the result is below the value of tolerance
while abs(a-b)>maxerr
a=b;
b=c;
c=b-((f(b)*(a-b))/(f(a)-f(b)));
end
Verification:
t Root
8.0% 0.61270
0.58% 0.56384
0.0048% 0.56717
GUIs (also known as graphical user interfaces or UIs) provide point-and-click control of software
applications, eliminating the need to learn a language or type commands in order to run the
application. MATLAB® apps are self-contained MATLAB programs with GUI front ends that
automate a task or calculation.
Lab sheet prepared by Md. Arif Hossain, Lecturer (EEE), Islamic University of Technology (IUT)
Problem Statement: Use the secant method and generate a GUI to estimate the root of
f ( x) e x x . Start with initial estimates of x1 0 and x0 1 .
Steps:
1. Type “guide” on the command window of your Matlab.
2. Select “Blank GUI (default)”.
3. Select “Edit text” box and place it on the fig. (Double click on the box and clear the
contents.)
4. Select “Push Button” and place it on the fig. (You can change the nema of the push button
to “Calculate”)
5. Right click on the push button and select “View callbacks callback”
6. Then an editor will pop up and write the following:
while abs(a-b)>maxerr
a=b;
b=c;
c=b-((f(b)*(a-b))/(f(a)-f(b)));
end
disp(['Root is x=' num2str(c)]);
7. Give the required inputs and hit “Calculate”. Your result will be displayed in the
command window.
Lab sheet prepared by Md. Arif Hossain, Lecturer (EEE), Islamic University of Technology (IUT)
Task in the Lab:
1. Modify the given code and make it a function using Indexing, (i.e xi , xi 1 , etc) and generate
the exact same result.
2. Fix the „maxerror‟ to 0.0048% in the GUI and generate the required interface.
3. Prompt the user to give the expression to be solved in GUI instead of pre-allocating it.
1. Use the modified secant method to estimate the root of f ( x) e x x . Use a value of 0.01 for
δ and start with x0 = 1.0.
2. Determine the highest real root of f ( x) 0.95x3 5.9 x2 10.9 x 6 : Using the secant method
(three iterations, xi 1 = 2.5 and xi = 3.5)
Lab sheet prepared by Md. Arif Hossain, Lecturer (EEE), Islamic University of Technology (IUT)