[go: up one dir, main page]

0% found this document useful (0 votes)
124 views49 pages

Matlab Project

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 49

1.1 What is MATLAB?

MATLAB (Matrix Laboratory) is an interactive software system for numerical


computations and graphics. As the name suggests, MATLAB is especially
designed for matrix computations: solving systems of linear equations, computing
Eigen values and eigenvectors, factoring matrices, and so forth. In addition, it has a
variety of graphical capabilities, and can be extended through programs written in
its own programming language. Many such programs comes with the system; a
number of these extend MAT Lab's capabilities to nonlinear problems, such as the
solution of initial value problems for ordinary differential equations. The
MATLAB is a high – performance language for technical computing. MATLAB is
an interactive system and has an interactive workspace [1].

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.

Programming in MATLAB is achieved through a script language using M – files


(files with the extension .m). Interest in the script language usage has significantly
increased over the last years due to a combination of the following factors [2]:

a- An increasingly large library of mathematical functions is available. These


functions are the building blocks for complex programs. They allow achieving
a significant reduction in the development cycle while maintaining robustness
and overall quality in user – friendly aspects.

b- Evolving language constructs and features provide advanced expressive


power.

c- All programs written through M – files becomes automatically portable on


all computers platforms supported by MATLAB.
1
d- Support for sparse matrices allows programming computationally efficient
large-scale applications.

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.

1.2 Aim of the project

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

The MATLAB Graphical User Interface

2.1-1 Introduction

The MATLAB Graphical User Interface development environment provides a set


of tools for creating graphical user interfaces (GUIs). These tools greatly simplify
the process of designing and building GUIs. In this chapter a brief introduction of
how to use the MATLAB GUI will be discussed.

2.1-2 What Is GUIDE?

GUIDE, the MATLAB Graphical User Interface development environment,


provides a set of tools for creating graphical user interfaces (GUIs). These tools
greatly simplify the process of designing and building GUIs. You can use the
GUIDE tools to

a. Lay out the GUI


Using the GUIDE Layout Editor, you can lay out a GUI easily by clicking
and dragging GUI components -- such as panels, buttons, text fields, sliders,
menus, and so on -- into the layout area.
b. Program the GUI
GUIDE automatically generates an M-file that controls how the GUI
operates. The M-file initializes the GUI and contains a framework for all the
GUI callbacks -- the commands that are executed when a user clicks a GUI
component. Using the M-file editor, you can add code to the callbacks to
perform the functions you want.

2.1-3 Starting GUIDE

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

From the Quick Start dialog, you can

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.

2.1-4 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.

Figure 1.2 layouts with a blank GUI template display


4
You can lay out your GUI by dragging components, such as push buttons, pop-up
menus, or axes, from the component palette, at the left side of the Layout Editor,
into the layout area. For example, if you drag a push button into the layout area, it
appears as in figure 1.3.

Figure 1.3 showing push button in the layout area


You can also use the Layout Editor to set basic properties of the GUI components.

2.1-5 GUIDE Templates

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.4 Quick starting of GUI with Uicontrol


5
Clicking OK opens the template in the Layout Editor, as shown in figure 1.5.

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.

2.1-6 Running a GUI

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.

Figure 1.6 showing values

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

Figure 1.7 Adding components to the Layout Editor

2- Add other components to the GUI.

a. Two static text

b. Two chick boxes

c. Two panels

Arrange the components as shown in Figure 1.5. Resize the axes component to
approximately 2-by-2 inches.

2.1-8 Selecting Components from the Component Palette

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.

2.1-8-2 Toggle Button

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.

2.1-8-3 Check Box

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.

2.1-8-4 Radio Button

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.

2.1-8-5 Edit Text

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.

2.1-8-6 Static Text

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.

2.1-8-8 List Box

List boxes display a list of items and enable users to select one or more items.

2.1-8-9 Pop-Up Menu

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.

2.1-8-12 Button Group

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.

2.1-8-13 ActiveX Component

ActiveX components enable you to display ActiveX controls in your GUI.

2.1-9 GUI FIG-Files and M-Files

GUIDE stores a GUI in two files, which are generated the first time you save or
run the GUI:

1- A FIG-file, with extension .fig, which contains a complete description of


the GUI layout and the components of the GUI: push buttons, menus, axes,
and so on.
2- An M-file, with extension .m, which contains the code that controls the GUI,
including the callbacks for its components.

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.

2.1-10 Programming the GUI 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:

1- Initializes the GUI


2- Contains code to perform tasks before the GUI appears on the screen, such as
creating data or graphics
3- contains the callback functions that are executed each time a user clicks a GUI
component

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

Defining the Data Fields in the Opening Function

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.

guidata(hObject, handles); % Save the updated structure

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 code combines three commands:

a. The get command obtains the current value of the slider.


b. The num2str command converts the value to a string.
c. The set command resets the String property of the edit text to the updated
value.

Setting the Slider Value from the Edit Text Callback

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.

val = str2double ( get ( handles.edit1 , ' String ' ) ) ;

% Determine whether val is a number between 0 and 1

if isnumeric ( val ) & length ( val ) == 1 & ...

val >= get ( handles.slider1 , ' Min ' ) & ...

val <= get ( handles.slider1 , ' Max ' )

set ( handles.slider1 , ' Value ' , val ) ;

else

% Increment the error count, and display it

handles.number_errors = handles.number_errors+1;

guidata ( hObject , handles); % store the changes

set ( handles.edit1 , ' String ' , ...


12
[ ' You have entered an invalid entry ' , ...

num2str ( handles.number_errors ) , ' times. ' ] );

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:

2.1-11 Layout Editor Preferences

You can set preferences for the Layout Editor by selecting Preferences from the
File menu as shown in figure 1.9.

Figure 1.9 layout editor preferences

13
2.1-12 Displaying the Property Inspector

You can display the Property Inspector by any of the following actions:

a. Double-clicking on a component in the Layout Editor


b. Selecting Property Inspector in the View menu
c. Right-clicking on a component and selecting Inspect Properties from the
context menu

This displays the Property Inspector, as shown in figure 1.10.

To change a property, click in the field to the right of the property name, delete its
current value, and type a new value.

Figure 1.10 property inspector windows

2.1-13 Some Commonly Used Properties

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.

GUIDE uses the Tag property to

a. Construct the name of the generated callback (e.g., close_button_Callback)


when you run or save the GUI
b. Set corresponding callback properties to point to the callback.
c. Add a field to the handles structure containing the handle of the object
(e.g., handles.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

2.2-1 What is the numerical analysis?

Numerical Analysis studies algorithms that have many applications in engineering,


and sciences (e.g. physics, biology, chemistry), in models of the atmosphere, the
environment and the economy as well as in many other areas.

2.2-2 Where is numerical analysis useful?

The numerical analysis is useful in [3,4]:-

1- Engineering: simulating physical system from industry or the environment,


e.g. design / analysis of buildings / bridges / aircraft / auto mobiles, weather
prediction, and prediction of traffic flow.

2- Biology: simulation of biological processes e.g. spread f disease, population


growth, blood flow / circulation / nerve firing. (Computational genetics often
uses discrete algorithms, drawing from combinatory, rather than numerical
analysis).

3- Computer graphics: image compression, representation of curves / surface /


lighting.

4- Economics / Finance: modeling / simulation of financial data.

5- Scientific computing: design of algorithms for high performance / parallel


computing.

6- Social behavioral sciences: simulate of social processes.

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:

f(x) = x3- 7x2 -10=0

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 ( pn1 )) and ( pn2 , 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:

bn1  an1
pn  an1  f ( an1 )
f ( bn1 )  f ( an1 )
 Secant method

It is an efficient method in term of work and does not require knowledge of


the derivation and the iterative formula of this method is:

pn  2  pn  1
p n  pn - 1  f ( p n  1 )
f ( pn  2 )  f ( pn  1 )

 Newton-Raphson method.

Is the fastest in converge when it does converge?, it requires prior


knowledge to the approximate location of the root, and the iterative formula
of this method is:

17
f ( pn1 )
pn  pn-1 
f ( pn1 )

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.

2.2-2-2 Solution of System of Linear Equations

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

Then, these equations can be rewritten in the form:


 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

1- If m  n  there exist a solution but not unique

Example x + y = 2

2- If m  n  may be there is no solution

Example

2x – 3y = 1 . . . . . (1)

x+y=0 . . . . . (2)
18
5x – y = 3 . . . . . (3)

3- If m  n  A square matrix 

There exists a solution if the inverse of A exists

Such set of equations can be solved either by direct (exact) methods or iterative
(numerical) methods:

a- Iterative method:

Such as Jacobi method and Gauss – Seidel method

The convergence condition is

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

And that for the Gauss – Seidel method is:

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:

Such as Forward Substitution in which the upper triangle of the matrix A is


zeroed, Backward Substitution in which the lower triangle of the matrix A
is zeroed, and Gauss Jordon method, where, in this method, the augmented
matrix is converted as shown
a 11 a 12 a 13 c1  1 0 0 s1 
   
a 21 a 22 a 23 c2   0 1 0 s2  
a 31 a 32 a 33 c 3  0 0 1 s 3 
 
x 1  s1
x 2  s2
x 3  s3

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.

Some methods of interpolation includes:

 Lagrange Interpolation

The general form of this method for set of N data of inputs (x) and ouputs
(y) is:

Where

20
 Newton Polynomials:

These polynomials may be constructed recursively:

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 )

Where ai are the coefficients of the polynomial which can be found by


divided, forward or backward difference formula.

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.

 The MATLAB Curve Fitting Toolbox?

The Curve Fitting Toolbox is a collection of graphical user interfaces (GUIs)


and M-file functions built on the MATLAB technical computing
environment, which can do many works on a given data, like finding the
optimum curve fitting to the data, make some statistical calculation on data,
manipulating data, and more

2.2-2-4 The Numerical Integration

Numerical integration is used to determine integrals that cannot be solved using


analytical methods.

Two famous methods for numerical integration are:

 Trapezoidal rule

The general form of Trapezoidal rule is:

For n = 1, 2, 3,…

21
Where and

X0 X1 X2 X3 Xn

a h b

 Simpson's rule

The general form of Simpson's rule is:

For n= 2, 3, 4, …

 MATLAB Symbolic Integration

In MATLAB, there is a built in function which is integrate the function


symbolically, which is an amazing think although it is so difficult in use.

2.2-2-5 Numerical Differentiation

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.

The general form for the forward difference formula is:

Another way for differentiation is to differentiate continuous functions, although it


internally based on discrete difference. Both methods are taken into consideration
in the designed package.

2.2-2-6 Ordinary Differential Equations

The ODE solvers are designed to handle ordinary differential equations. An


ordinary differential equation contains one or more derivatives of a dependent
22
variable y with respect to a single independent variable t usually referred to as
time. The derivative of y with respect to t is denoted as y' the second derivative as
y'' and so on. Often y (t) is a vector, having elements.

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:

 Fourth order Range Kutta method

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 )

We can then integrate from tn to tn +h using:

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

3.2 program structure

 The program will start when pressing start button.


 The designed program consist of :

1. Numerical Differentiation.

2. Numerical Integration.

3. Interpolation and Curve fitting.

4. Solution the Ordinary Differential equation.

5. Solution of system of linear Equation.

6. Roots finding.

 The program will return to main menu when pressing Back button.

Figure 3.1 shows the Numerical analysis Tools:

Figure (3.1) numerical analysis tools


24
3.3 Running the program

One can run the program by different ways:-

 From the debug in the tool bar of the (m.File).


 By pressing the icon in either (m.File) or (fig.File).
 Through writing the name of the program in the work space, but before that
we need to put the program on the main directory of MATLAB
 By pressing F5 from the (m.File).

In either ways, figure 3.2 will appear.

Figure (3.2) main menu


Pressing start button will show another figure which contains the numerical
analysis tools as shown in figure 3.1.

Now, several examples will be taken to illustrate some features of the designed
program:

Example 1(Numerical Differentiation):

 Pressing Numerical Differentiation button, a window will appear that


contain two popular ways which are :

1- Numerical differentiation based on Newton Interpolation method.


2- Numerical differentiation based on Difference method see figure 3.3
25
Figure 3.3 numerical differentiation operations
When the user choose one of the check box's of the numerical differentiation
methods another window will appear asking to enter the data related with the
selected method (see figure 3.3(a)).

Figure 3.3(a) numerical differentiation based on Newton interpolation method

26
 For the Newton difference method

Input the equation represent the equation needs to be derived

Input the value of h is the step size

x1 and x2 are the boundaries of the function to be derived

After insert data in each text box and press the execution button the result will
appear as shown in figure 3.3(b)

Figure 3.3(b) the result of Newton operation.

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.

Figure 3.3(d) the result of difference operation.


28
Figure 3.3(e) the result in both ways.

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.

 By pressing the back button figure 3.1 will appear.

Example2 (numerical Integration):

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

As shown in figure 3.4

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

Figure 3.4(a) trapezoidal integration method


30
 For the trapezoidal operation :

Input the equation here we enter the equation to be integrated.

The value of a and b represent the limits of integration for the given
equation.

The value of N represent the number of slicing of the period

 For the Simpson's operation :

The same data entry as the trapezoidal rule

 For the Symbolic operation :

It depends only on insert equation and gives the integrated 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.

Figure 3.4(b) inserting data to the operation.

31
Figure 3.4(c) the result of the operation.

 By pressing the back button figure 3.1 will appear

Example 3(Interpolation and curve fitting):

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

As shown in figure 3.5

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

Figure 3.5(a) the MATLAB function method.


33
Inserting the data in the text boxes as shown in figure 3.5(b)

Figure 3.5(b) inserting data.

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

Figure 3.5 (c) Opening cftool


34
1- Importing the Data:

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)

Figure 3.5(d) import the data to the cftool


We select from the data set the (y vs. x) and then click view button to Preview the
data, see figure 3.5(e)

Figure 3.5 (e) Preview the data


35
2- Fitting the Data

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)

Figure 3.5(f) fitting the data


From the Fit Editor we specify the fit name, the current data set, the exclusion rule,
and the type of fit. The Table of Fits allows you to keep track of all the fits, their
data sets, and fit results for the current session.

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.

Figure 3.5(g) fitting the data

3- Excluding the data

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)

Figure 3.5(I) excluding the data

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

Figure 3.5(J) plotting the GUI

5- Analyzing the Fit

We can evaluate (interpolate or extrapolate), differentiate, or integrate a fit over a


specified data range with the Analysis GUI. Open this GUI by clicking the
Analysis button on the Curve Fitting Tool, to analysis the fit we must do this:

1- Enter the appropriate MATLAB vector in the Analyze at Xi field.

2- Select the Evaluate fit at Xi check box.

3- Select the Plot results and Plot data set check boxes.

4- Click the Apply button. See figure 3.5(k)

39
Figure 3.5(k) analyzing the fit

And when clicking Apply button, the analysis results is as shown in figure 3.5(m)

Figure 3.5(m) Analysis the fit


40
6- Saving the Analytical Results

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

Figure 3.5(n) saving the Analysis Results

 By pressing the back button figure 3.1 will appear

Example 4(solution of ordinary differentiation):

 Pressing Solution of Ordinary Differentiation button, a menu will appear


showing the designed method, these are:

1- Euler's method.
2- Midpoint method.
3- Fourth – Order Runge – Kutta method.
4- MATLAB built in function (ode 45 and ode 23)

As shown in figure 3.6(a).

Figure 3.6(a) the solution of ordinary differential equations.


41
Choosing one of the check boxes for execution or one can execute more than one
operation in the same time and show the result, for example, selecting all methods
and entering the specified data for each method see figure 3.6(b),

Figure 3.6(b) displaying all methods as well as the help

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)

Figure 3.6(c) show the result of the operation

 By pressing the back button figure 3.1 will appear

Example 5(solution of set of linear equations):

 Pressing solution of set of liner equations button, a menu will appear that
contain the most popular ways for the method which are :

1- Exacts method (MATLAB built in method).


2- Gauss-seidel.
3- Gauss Jordan.

As shown in figure 3.7(a)

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

Figure 3.7(b) Gauss-seidel method.

44
After insert data and pressing the execution button figure 3.7(c) will appear.

Figure 3.7(c) the result of the method.

 By pressing the back button figure 3.1 will appear

Example 6(roots finding):

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:

1- N-R (Newton – Raphson ) method.


2- Secant method.
3- False-Position method.
4- MATLAB built in function.

See figure 3.8(a)

45
Figure 3.8(a) Roots finding method.

By choosing one of these check boxes figure 3.8(b) will appear.

Choosing the MATLAB function as an example.

Figure 3.8(b) MATLAB function method.


46
By pressing the execution button the result will appear as shown in figure 3.8(c).

Figure 3.8(c) the result of the operation

 By pressing the back button figure 3.1 will appear

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.

4.2 Suggested Future Work

Expanding this program to deal more with the errors and warnings, also expanding
the programs to include more numerical analysis methods.

48
References

1- MATLAB help Product Books


2- John H. Mathews, Kurtis D. Fink, "Numerical Methods using MATLAB",
1999 (Third edition).
3- Gerald, “Applied Numerical Analysis”, 1996
4- Burden, Richard L. & Faires, J. Douglas, “Numerical Analysis”, 2000 (7th
edition).

49

You might also like