Matlab Project
Matlab Project
Matlab Project
MATLAB can be used for day–to– day computations with powerful mathematical
functions and graphics. It can be also used for programming with a script language.
MATLAB is a powerful computing system for handling the calculation involved in
scientific and engineering problems, because the system was designed to make
matrix computations particularly easy. One of many things you will like about
MATLAB (and which distinguishes it from many computer programming systems,
such as C++ and Java) is that you can use it interactively, this means you type
some command at the special MATLAB prompt, and get the answers immediately.
The problems solved in this way can be very simple, like finding a square root, or
they can be much more complicated, like finding the solution to a system of non
linear differential equations.
It is now feasible to compile and create stand – alone applications from M – files –
based programs, including even sparse matrices and graphical user interface
development functions.
On the other hand, numerical analysis studies algorithms that have wide
application in engineering, and sciences, in models of the atmosphere, the
environment and the economy as well as in many other areas, and it is useful in
many subjects such as Engineering. Biology, Computer graphics, Economics /
Finance, Scientific computing, … etc. Because of the MATLAB power mentioned,
it seems to be a good choice for designing a simple numerical analysis program
based on it.
The project aims to build a simple numerical analysis package based on MATLAB
language and using the Graphical User Interface environment of the MATLAB
utilizing the power of MATLAB in dealing with matrices and numeric data.
2
PART ONE
2.1-1 Introduction
To start GUIDE, enter guide at the MATLAB prompt. This displays the GUIDE
Quick Start dialog, as shown in figure 1.1.
3
Figure 1.1 Guide Quick Start dialog box
a. Create a new GUI from one of the GUIDE templates -- prebuilt GUIs that
you can modify for your own purposes.
b. Open an existing GUI.
Once you have selected one of these options, clicking OK opens the GUI in the
Layout Editor.
When you open a GUI in GUIDE, it is displayed in the Layout Editor, which is the
control panel for all of the GUIDE tools. Figure 1.2 shows the Layout Editor with
a blank GUI template.
The GUIDE Quick Start dialog provides templates for several basic types of GUIs.
The advantage of using templates is that often you can modify a template more
quickly and easily than starting from a blank GUI. When you select a template in
the Templates pane, a preview of it appears in the right-hand pane. For example,
when you select the GUI with Uicontrols, the Quick Start dialog appears as in
figure 1.4.
Figure 1.5 template for GUI with Uicontrol opened in the layout editor
To display the names of the GUI components in the component palette, select
Preferences from the File menu, check the box next to Show names in component
palette, and click OK.
To run a GUI, select Run from the Tools menu, or click the run button on the
toolbar. This displays the functioning GUI outside the Layout Editor. For example,
when you run the GUI with Uicontrol template, it appears as shown in figure 1.6.
When a user enters values for the density and volume of an object, and clicks the
Calculate button, the GUI calculates the mass of the object and displays the result
next to Mass(D*V).
6
2.1-7 Adding the Components
1- Add the two push buttons to the GUI. For each push button, select the push
button from the component palette at the left of the Layout Editor and drag it
into the layout area. Position them approximately as shown in figure 1.7
c. Two panels
Arrange the components as shown in Figure 1.5. Resize the axes component to
approximately 2-by-2 inches.
The component palette at the left of the Layout Editor contains the components
that you can add to your GUI as shown in figure 1.3. This section describes these
components. After selecting the components for your GUI and placing them in the
layout area, you need to set their properties and program their callbacks.
7
2.1-8-1 Push Button
Push buttons generate an action when clicked. For example, an OK button might
close a dialog box and apply settings. When you click a push button, it appears
depressed; when you release the mouse, the button appears raised and its callback
executes.
Toggle buttons generate an action and indicate whether they are turned on or off.
When you click a toggle button, it appears depressed, showing that it is on. When
you release the mouse button, the toggle button's callback executes. However,
unlike a push button, the toggle button remains depressed until you click the toggle
button again. When you do so, the button returns to the raised state, showing that it
is off, and again executes its callback.
Check boxes generate an action when checked and indicate their state as checked
or not checked. Check boxes are useful when providing the user with a number of
independent choices that set a mode, for example, displaying a toolbar or
generating callback function prototypes.
Radio buttons are similar to check boxes, but are typically mutually exclusive
within a group of related radio buttons. That is, you can select only one button at
any given time. To activate a radio button, click the mouse button on the object.
The display indicates the state of the button.
Edit text controls are fields that enable users to enter or modify text strings. Use
edit text when you want text as input. The String property contains the text entered
by the user. The callback executes when you press Enter for a single-line edit text,
Ctl+Enter for a multi-line edit text, or the focus moves away.
Static text controls display lines of text. Static text is typically used to label other
8
controls, provide directions to the user, or indicate values associated with a slider.
Users cannot change static text interactively and there is no way to invoke the
callback routine associated with it.
2.1-8-7 Slider
Sliders accept numeric input within a specific range by enabling the user to move a
sliding bar, which is called a slider or thumb. Users move the slider by pressing the
mouse button and dragging the slider, by clicking in the trough, or by clicking an
arrow. The location of the slider indicates a percentage of the specified range.
List boxes display a list of items and enable users to select one or more items.
Pop-up menus open to display a list of choices when users click the arrow.
2.1-8-10 Axes
Axes enable your GUI to display graphics (e.g., graphs and images). Like all
graphics objects, axes have properties that you can set to control many aspects of
its behavior and appearance.
2.1-8-11 Panel
Panels group GUI components. Panels can make a user interface easier to
understand by visually grouping related controls. A panel can have a title and
various borders.
Panel children can be panels and button groups as well as axes and user interface
controls. The position of each component within a panel is interpreted relative to
the panel. If you move the panel, its children move with it and maintain their
positions on the panel.
Button groups are like panels but can be used to manage exclusive selection
behavior for radio buttons and toggle buttons.
9
For radio buttons and toggle buttons that are managed by a button group, you must
include the code to control them in the button group's SelectionChangeFcn
callback function, not in the individual uicontrol Callback functions. A button
group overwrites the Callback properties of radio buttons and toggle buttons that it
manages.
GUIDE stores a GUI in two files, which are generated the first time you save or
run the GUI:
These two files correspond to the tasks of laying out and programming the GUI.
When you lay out of the GUI in the Layout Editor, your work is stored in the FIG-
file. When you program the GUI, your work is stored in the M-file.
After laying out your GUI, you can program the GUI M-file using the M-file
editor. GUIDE automatically generates this file from your layout the first time you
save or run the GUI. The GUI M-file includes:
Initially, each callback contains just a function definition line. You then use the M-
file editor to add code that makes the component function the way you want it to.
To open the M-file, click the M-file Editor icon on the Layout Editor Toolbar.
10
Example: ( Passing Data between Callbacks )
This example demonstrates how to use the handles structure to pass data between
callbacks. The example passes data between a slider and an editable text box in the
following way:
a. When a user moves the slider, the text box displays the slider's current value.
b. When a user types a value into the text box, the slider updates to this value.
c. If a user enters a value in the text box that is out of range for the slider -- that
is, a value that is not between 0 an 1 -- the application returns a message
indicating how many times he has entered an erroneous value. Figure 1.8
shows the GUI with a static text field above the edit text box.
Figure 1.8 GUI with static text: enter the value or click the slider
The GUI records the number of times a user enters an erroneous value in the text
box and stores this number in a field of the handles structure. You can define this
field, called number_errors, in the opening function as follows:
handles.number_errors = 0;
Type this command before the following line, which GUIDE automatically inserts
into the opening function.
The guidata command saves the handles structure so that it can be retrieved in the
callbacks.
11
Setting the Edit Text Value from the Slider Callback
The following command in the slider callback updates the value displayed in the
edit text when a user moves the slider and releases the mouse button.
Set ( handles.edit1 , ' String ' , num2str ( get ( handles.slider1 , ' Value ' ) ) ) ;
The edit text callback sets the slider's value to the number the user types in, after
checking to see if it is a single numeric value between 0 and 1. If the value is out of
range, then the error count is incremented and the edit text displays a message
telling the user how many times they have entered an invalid number.
else
handles.number_errors = handles.number_errors+1;
end
If you types a number between 0 and 1 in the edit box and then clicks outside the
edit box, the callback sets handles.slider1 to the new value and the slider moves to
the corresponding position.
If the entry is invalid -- for example, 2.5 -- the GUI increments the value of
handles.number_errors and displays a message like the following:
You can set preferences for the Layout Editor by selecting Preferences from the
File menu as shown in figure 1.9.
13
2.1-12 Displaying the Property Inspector
You can display the Property Inspector by any of the following actions:
To change a property, click in the field to the right of the property name, delete its
current value, and type a new value.
As examples, this section describes four important and commonly used properties
of user interface control components: Tag, Callback, String, and Value.
a. Tag
The Tag property is an identifier for the component. GUIDE assigns a value to the
Tag property of every component you insert in your layout (e.g., pushbutton1) and
then uses this string to name the callback associated with the Callback property
14
(e.g., pushbutton1_Callback). You should change the Tag property to a more
descriptive name, so that you can more easily identify the component's callback in
the M-file. For example, if you add a push button that closes the GUI, you might
set its tag to close_button.
b. Callback
The Callback property specifies the callback that is executed in the GUI M-file
when a user activates the component.
c. String
The String property contains text for the component. The following are examples:
a- For buttons, check boxes, list boxes, edit text, and static text, the String text
is displayed on or next to the component.
b- For an edit text, the String property contains a list of strings that is displayed
in the text box. When a user edits the text, the String property is updated.
d. Value
The Value property contains a numerical value for the component, which must lie
in the range specified by the Max and Min properties. The following are examples:
a. For radio buttons and check boxes, Max and Min are set to 1 and 0,
respectively, by default. The Value property is set to 1 when the radio button
or check box is selected and 0 when it is cleared.
b. When a user drags a slider, the Value is set to a number between Max and
Min corresponding to the location of the slider button.
15
PART TWO
Numerical Analysis
There are many problems required to be solved using numerical methods, some of
them includes:
16
2.2-2-1 Root Finding:
If we want to solve the equation x3 – 7x2 = 10, simply, putting this equation in the
form:
And then we look for the points of x which make f(x) = 0, such points are called
roots of f.
Many methods are available to deal with this kind of problems including (if f is in
the interval [a, b]):
False-position
This methods keep the root bracketed between an and bn as the x-intercept of
the secant line between ( p 1 , f ( pn1 )) and ( pn2 , f(pn-2 )) .
n
If we choose pn as the x-intercept of the line connecting the points (a, f (a))
and (b, f (b)) the equation for the new estimation of the root is:
bn1 an1
pn an1 f ( an1 )
f ( bn1 ) f ( an1 )
Secant method
pn 2 pn 1
p n pn - 1 f ( p n 1 )
f ( pn 2 ) f ( pn 1 )
Newton-Raphson method.
17
f ( pn1 )
pn pn-1
f ( pn1 )
All of these methods are taken into consideration in the proposed package as well
as the MATLAB built in function which, find the exact solution of the equation
and finds all roots at the same time.
Suppose that we have n linear equations with n variables, as in the following form
a11 x1 a12 x 2 . . . . . a1 n x n b1
a 21 x1 a 22 x 2 . . . . . a 2 n x n b2
a m 1 x1 a m 2 x 2 . . . . . a mn x n bm
a 11 a 12 . . . . a 1n x 1 b1
a 21 a 22 . . . . a2n x 2 b2
a m 1 am2 . . . . a mn x n bm
Or
AX = B
Example x + y = 2
Example
2x – 3y = 1 . . . . . (1)
x+y=0 . . . . . (2)
18
5x – y = 3 . . . . . (3)
3- If m n A square matrix
Such set of equations can be solved either by direct (exact) methods or iterative
(numerical) methods:
a- Iterative method:
N
a kk a kj
j 1
j k k 1,2,... , N
The iterative form of Jacobi method (for a set of three linear equations) is:
b1 a12 x2 a13 x3
(k) (k)
( k 1 )
x1
a11
b2 a21 x1 a23 x3
(k ) (k )
( k 1 )
x2
a22
b3 a31 x1 a32 x2
(k ) (k )
( k 1 )
x3
a33
b1 a12 x2 a13 x3
(k) (k )
( k 1 )
x1
a11
( k 1 )
b2 a21 x1 a23 x3
(k)
( k 1 )
x2
a22
( k 1 ) ( k 1 )
( k 1 ) b3 a31 x1 a32 x2
x3
a33
19
b- Direct method:
Where the upper and lower triangle is zeroed by using the Elimination Procedure.
2.2-2-3 Interpolation
Is a process for estimating values that lie between known data points? It has
important applications in areas such as signal and image processing.
The Idea: interpolation, given a few data points and construct a curve that goes
through those points. Depending on the data and expected trends, we may
interpolate using polynomial, exponential, trigonometric or other functions.
Alternatively: curve fitting, if we choose a function which matches the trend of the
data, without necessarily passing through the data points.
Lagrange Interpolation
The general form of this method for set of N data of inputs (x) and ouputs
(y) is:
Where
20
Newton Polynomials:
p1 ( x ) a 0 a1 ( x x 0 )
p 2 ( x ) p1 (x) a 2 ( x x 0 ) ( x x1 )
.
.
p N ( x ) p N 1 (x) a N ( x x 0 ) ( x x1 )...( x x N 1 )
In the designed program, these methods are taken as well as to the MATLAB built
in functions which will find the exact solution of the equation, and can be used for
curve fitting.
Trapezoidal rule
For n = 1, 2, 3,…
21
Where and
X0 X1 X2 X3 Xn
a h b
Simpson's rule
For n= 2, 3, 4, …
Given a set of data, estimate the derivative of data; perhaps to compute some
physical quantity, there are many methods for differentiation including forward,
backward and central difference methods.
There are different methods to solve ordinary Differential Equations, some of them
are:
Euler's method
Euler’s method is simple but not very accurate, the integral and the equation
of Euler’s method is:
This is one of the most popular algorithms for solving ordinary differential
equations. Solve for four different values of f (t, y):
k 1 hf(t n, , y n )
h 1
k 2 hf(t n , yn k1 )
2 2
h 1
k 3 hf(t n , y n k 2 )
2 2
k 4 hf(t n h , y n k 3 )
1
y n 1 y n [ k 1 2k 2 2k 3 k 4 ]
6
The proposed program includes the above mentioned methods as well as the built
in MATLAB function.
23
3.1 Introduction
In this chapter, the structure of the proposed program will be discussed as well as
some examples will be taken that describe the features of the program and its
capabilities
1. Numerical Differentiation.
2. Numerical Integration.
6. Roots finding.
The program will return to main menu when pressing Back button.
Now, several examples will be taken to illustrate some features of the designed
program:
26
For the Newton difference method
After insert data in each text box and press the execution button the result will
appear as shown in figure 3.3(b)
If we use the difference method, (see figure 3.3(c)), and after insert the data we can
see the result after pressing execute button (see figure 3.3(d)), this figure shows
both the input continuous function (which in this example sin(x)) and the output
differentiated continuous function (which in this example cos(x)), in the range of
input from 0 to 8 (in radians). The program has the ability to run more than one
method at the same time which is a powerful way for comparison issues (see figure
3.3 (e)), for the above example, one can select both methods and see the results, of
both methods at the same time.
27
Figure 3.3(c) Numerical differentiation based on difference method.
The designed program has another feature, which is the help feature, for each
operation, there is a help button containing text helps the user to identify each data
entry, and how to insert the date in each field.
Pressing numerical integration button, a menu will appear that contain the
used method of integration which are :
1- Trapezoidal Rule.
2- Simpson's Rule.
3- Symbolic Rule.
29
Figure 3.4 numerical integration operations.
checking any one of the check boxes will display the entry field belonging to the
selected method. If we select the trapezoidal method, see figure 3.4(a).
The value of a and b represent the limits of integration for the given
equation.
After inserting the data, as shown in figure 3.4(b) and pressing the execute button,
the results will be as shown in figure 3.4(c).
We can also verify the solution using the Simpson's and/or the Symbolic methods
each alone or the three operations at the same time, which a powerful way for
comparison or learning issues.
31
Figure 3.4(c) the result of the operation.
Pressing Interpolation and curve fitting button, a menu will appear that
contain the used methods, these are:
1- Lagrange interpolation.
2- Newton polynomials.
3- MATLAB function.
32
Figure 3.5 interpolation and curve fitting method.
Choosing one of the check boxes will display the data entry fields of the selected
method, we will take the MATLAB function as an example for Interpolation and
curve fitting method as in figure 3.5(a).
By pressing the execution button the program will call for the built in function
and pass the data to it then open it. The result is shown in figure 3.5(c).
We can import data into the Curve Fitting Tool with the Data GUI. We open this
GUI by clicking the Data button on the Curve Fitting Tool. The Data GUI consists
of two panes: Data sets and Smooth. See figure (3.5.d)
The data is fitted with the Fitting GUI. Open this GUI by clicking the Fitting
button on the Curve Fitting Tool. The Fitting GUI consists of two parts: the Fit
Editor and the Table of Fits. See figure 3.5(f)
When we click the New fit we note that this action always defaults to a linear
polynomial fit type. We use New Fit at the beginning of our curve fitting session,
and when we are exploring different fit types for a given data set.
When clicking the Apply button, the library model, fitted coefficients, and
goodness of fit statistics are displayed in the Results area.
36
For fits of a given type (for example, polynomials), we should use Copy Fit instead
of New Fit because copying a fit retains the current fit type state thereby requiring
fewer steps than creating a new fit each time.
The Fitting GUI is shown in figure 3.5(g) with the results of fitting the data with a
cubic polynomial.
If there is justification, you might want to exclude part of a data set from a fit.
Typically, you exclude data so that subsequent fits are not adversely affected.
An exclusion rule must be created, which captures the range, domain, or index of
the data points to be excluded. See figure 3.5(h)
37
Figure 3.5(h) excluding the data
Then we must choose one of the existing exclusion rules and then press view
button to display the exclusion rule graphically. If a data set is associated with the
exclusion rule, the data is also displayed. See figure 3.5(I)
38
4-Plotting GUI
The Plotting GUI allows to control the data sets and fits displayed by the Curve
Fitting Tool, see figure3.5(J).
3- Select the Plot results and Plot data set check boxes.
39
Figure 3.5(k) analyzing the fit
And when clicking Apply button, the analysis results is as shown in figure 3.5(m)
By clicking the Save to workspace button, one can save the extrapolated values as
a structure to the MATLAB workspace, see figure 3.5(n).
1- Euler's method.
2- Midpoint method.
3- Fourth – Order Runge – Kutta method.
4- MATLAB built in function (ode 45 and ode 23)
42
If we choosing one of the check boxes for execution and then pressing the execute
button to see the results, see figure 3.6(c)
Pressing solution of set of liner equations button, a menu will appear that
contain the most popular ways for the method which are :
43
Figure 3.7(a) solution of set of linear equations.
By choosing one of the check boxes the method window will appear, we will
take the Gauss-sidle method as an example, see figure 3.7(b).
44
After insert data and pressing the execution button figure 3.7(c) will appear.
This is the last example for the numerical analysis methods which is roots finding.
By pressing this button, a window will appear that contain four popular methods
for roots finding, these are:
45
Figure 3.8(a) Roots finding method.
47
4.1 Conclusions
A visual numerical analysis program was built based on MATLAB language and
using the Graphical User Interface environment of the MATLAB for the most
important methods in the numerical analysis, this program is simple to use and to
change, supported with help on each operation explaining what is this operation
and how it works, also the program has the ability to display more than one way
for solving problems in the same time, which is a good way for comparison and
learning issues, in this program we take into considering the numerical
differentiation, Numerical Integration, Interpolation and Curve fitting, Solution of
Ordinary differential equations, Solution of set of liner Equation and Roots
finding.
Expanding this program to deal more with the errors and warnings, also expanding
the programs to include more numerical analysis methods.
48
References
49