You are on page 1of 4

Islamic University of Technology (IUT)

Organization of Islamic Cooperation (OIC)


Department of Electrical and Electronic Engineering (EEE)

Course No. : Math 4522


Course Name : Numerical Methods Lab.
Experiment No. : 04
Experiment Name : Introduction to Secant Method and GUI.

 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.

The derivative can be approximated by a backward finite divided difference, as

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

Example 6.6 of the book.

Problem Statement: Use the secant method to estimate the root of f ( x)  e x  x . Start with initial
estimates of x1  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'));

%Ask for the initial guesses


a=input('Enter first guess:');
b=input('Enter second guess:');

%Ask for the tolerance


maxerr=input('enter max err in %:');

%Run the algorithm for the first time


c=(a*f(b)-b*f(a))/(f(b)-f(a));

%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

%Select the range of x between which the function needs to be plotted


x=0:0.5:10;

%Display the result


disp(['Root is x=' num2str(c)]);

%Plot the function


plot(x,f(x));grid
title('Entered function on given interval with the result ')
hold on

%Plot the result on the same graph


plot([c,c],[5,-5],'g','linewidth',2)

 Verification:

t Root
8.0% 0.61270
0.58% 0.56384
0.0048% 0.56717

 Graphical User Interface.

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 x1  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:

function pushbutton1_Callback(hObject, eventdata, handles)


% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see
GUIDATA)
a=str2num(get(handles.edit1,'string'));
b=str2num(get(handles.edit2,'string'));
maxerr=str2num(get(handles.edit3,'string'));
f=@(x) exp(-x)-x;
c=(a*f(b)-b*f(a))/(f(b)-f(a));

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.

 Task for the report:

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)

You might also like