You are on page 1of 25

1

MATLAB PROGRAMMING
Dr Alaa Elsisi

Dr Alaa Elsisi 7/24/2021


2 Course Content

 Introduction  Loops

 Variables and Arithmetic  User Defined Functions 2


Operations  Algorithm Design and
 Logic Relations & Complex Flowcharts
Numbers  Graphical User Interface
 Arithmetic & Trigonometric (GUI)
Functions  Polynomial Operations
 User Defined Functions 1  Symbolic Operations
 Arrays
 Branching and if statement
 Figures and Plotting

Dr Alaa Elsisi 7/24/2021


3
3 Importance of Programming for
Engineer

 Learning basic computer programming skills is


important for engineering students in their early
years of college education, why?.
 If you're a scientist or engineer, programming
can enable you to work 10 to 100 times faster.
 You can come up with more creative solutions
than your colleagues who don't know how to
program.

Dr Alaa Elsisi 7/24/2021


4
4 MATLAB Software

 One of most important application for engineers and


scientists.
 Written in C language and supported with Java.
 Has all programming languages features.
 Has a module for plotting figure and shapes.
 A lot of built-in engineering functions in all fields.
 A useful graphical used interface.

Dr Alaa Elsisi 7/24/2021


5
5 MATLAB Software

 Matlab License (Please Contact IT) or https://www.mathworks.com/store/


 https://octave-online.net/

Dr Alaa Elsisi 7/24/2021


6
6 How to Run the Program

 From start Menu or by using MATLAB Icon

Dr Alaa Elsisi 7/24/2021


7
7 Main Window

Typical windows application features


(Menu or Ribbon)
command window
workspace
current directory
command history
Scripts (M Files) or the Editor
Dr Alaa Elsisi 7/24/2021
8
8 Main Window

current directory command window work space

Dr Alaa Elsisi 7/24/2021


9
9 Ribbons

Dr Alaa Elsisi 7/24/2021


10
10 Command Window

Input Commands Line by Line

Dr Alaa Elsisi 7/24/2021


11
11 Work Space

Display Variables
Change their
Values
Delete Them

Dr Alaa Elsisi 7/24/2021


12
12 Current Directory

Working
Directory
(Folder)
Different Files
and
Functions
Dr Alaa Elsisi 7/24/2021
13
13 Script or Editor

Write a
Program.
Create a
Function.

Dr Alaa Elsisi 7/24/2021


14
14 Help

Dr Alaa Elsisi 7/24/2021


15
15 Textbook

Stephen J. Chapman, Essentials of Matlab


Programming, 2nd Edition;
Chapra, S.C., Applied Numerical Methods
with Matlab for Engineer and Scientists, 4th
ed., McGraw-Hill, New York
 Essential MATLAB for Engineers and Scientists

Dr Alaa Elsisi 7/24/2021


16
16 Introduction Cont.

Examine Arithmetic

A period in front of the *, /, and ^, respectively, does not change the results
because the multiplication, division, and exponentiation is done with single
numbers. (An explanation for the need for these symbols is provided later
when we deal with arrays of numbers.)
Dr Alaa Elsisi 7/24/2021
17
17 Introduction Cont.

Examine Variables

Notice that, in addition to doing the arithmetic with variables with


assigned values, several commands separated by semicolons (or
commas) can be put on one line.

Dr Alaa Elsisi 7/24/2021


18
18 Introduction Cont.

Examine Mathematical functions


MATLAB has all of the usual mathematical functions found on a
scientific electronic calculator, like sin, cos, and log (meaning the
natural logarithm). See Appendix C for many more examples.

Dr Alaa Elsisi 7/24/2021


19
19 Introduction Cont.

Examine Functions and commands


MATLAB has numerous general functions; Try.
• date: and calendar.
• clc: for clear command window).
• Help: is one you will use a lot.
Functions: usually return with a value (e.g., the
date),
Commands: tend to change the environment in
some way (e.g., clearing the screen or saving
some statements to the workspace).
Dr Alaa Elsisi 7/24/2021
20
20 Introduction Cont.

Examine Vectors
Variables such as a and b that were used in
above are called scalars; they are single-valued.
MATLAB also handles vectors (generally referred
to as arrays),

Dr Alaa Elsisi 7/24/2021


21
21 Introduction Cont.

Examine Vectors

Dr Alaa Elsisi 7/24/2021


22
22 Introduction Cont.

Examine Linear equations


A simple example is finding the solution to two
simultaneous equations:

Dr Alaa Elsisi 7/24/2021


23
23 Introduction Cont.

Examine Editor

Dr Alaa Elsisi 7/24/2021


24
24 Introduction Cont.

Examine Editor Cont..


% Example of one of the matrix % The is the difference between
inversion methods available in the exact and predict unitary
MATLAB % matrix:
clear;clc
difference = IPredicted - IM
% Let us consider the following
arbitrarily selected matrix: for m = 1:3
A =magic(3) for n = 1:3
% Let us evaluate its inverse as if difference(m,n) < eps;
follows: IPredicted(m,n) = IM(m,n);
AI = inv(A) end
% Let us check that it is an inverse: end
IPredicted = A * AI end
% This is the exact unitary matrix:
IPredicted
IM = eye(3)
IPredicted == IM
Dr Alaa Elsisi 7/24/2021
25
25 Introduction Cont.

Mexican hat Example

Dr Alaa Elsisi 7/24/2021

You might also like