1 Starting With MATLAB
1 Starting With MATLAB
Faculty of Engineering
Civil Engineering Department
ﻣﺎ ﻫﻲ اﻟﺒﺮﻣﺠﺔ؟
ﱠ
ﻋﻤﻠﻴﺔ ﺗﻌﻠﻴﻢ اﻟﺤﺎﺳﻮب إﺟﺮاء ﻣﻬﻤﺔ ﻣﺎ ُﻳﻤﻜﻨﻨﺎ اﻹﺟﺎﺑﺔ ﻋﻦ ﻫﺬا اﻟﺴﺆال ﺑﺎﻟﺠﻮاب اﻟﺒﺴﻴﻂ اﻟﺘﺎﻟﻲ" :اﻟﺒﺮﻣﺠﺔ ﻫﻲ
اﻟﺒﺮﻣﺠﺔ ﺑﺒﺴﺎﻃﺔ :ﻫﻲ ﻣﺠﻤﻮﻋﺔ اﻷواﻣﺮ اﻟﺘﻲ ﺗﻌﻄﻴﻬﺎ ﻟﻠﺤﺎﺳﻮب ،ﻟﻴﻘﻮم اﻷﺧﻴﺮ ﺑﺘﻨﻔﻴﺬﻫﺎ! ﻫﺬە اﻷواﻣﺮ ﺗﻜﻮن
ﻣﻜﺘﻮﺑﺔ ﺑﺸﻜﻞ “ﻏﺮﻳﺐ” ﺑﺎﻟﻠﻐﺔ اﻹﻧﺠﻠﻴﺰﻳﺔ
ﱠ
ﻟﻐﺔ اﻟﺤﺎﺳﻮب اﻟﻄﺒﻴﻌﻴﺔ
ﱠ ّ ُ ّ ُ َ َ ﱠ ُ
ﻟﻶﻻت ﻟﻐﺔ ﻃﺒﻴﻌﻴﺔ ﻛﻤﺎ اﻟﺒﺸﺮ .ﻻ ﺗﻔﻬﻢ اﻟﺤﻮاﺳﻴﺐ ﻟﻐﺔ اﻟﺒﺸﺮ اﻟﻄﺒﻴﻌﻴﺔ ،ﻓﻠﻐﺘﻬﺎ اﻟﺨﺎﺻﺔ ﻣﻜﻮﻧﺔ ﻣﻦ أﻛﻮاد
ﱠ
ﺛﻨﺎﺋﻴﺔ ،أي أﺻﻔﺎر ) (0وواﺣﺪات ).(1
ﻛﻞ ﻣﻜﺎن ،وﺑﻮاﺳﻄﺘﻬﺎ ﻳﻤﻜﻨﻚ اﻟﺘﺤﻜﻢ ﻓﻲ اﻵﻻت ،ﻓﻤﻦ روﺑﻮت ﻳﺮﻋﻰ اﻟﻤﺮﻳﺾ إﻟﻰ آﺧﺮ ﻳﺒﺤﺚ ﺗﻈﻬﺮ اﻟﺒﺮﻣﺠﺔ ﻓﻲ ّ
ٌ ّ
ﻣﻮﻗﻊ ﻣﺒﺮﻣﺞ ﻳﻌﺪ ﻣﺎ ﺗﺮاە
ٍ ﻋﻦ اﻟﻤﺎء ﻓﻲ ﻛﻮﻛﺐ اﻟﻤﺮﻳ ــﺦ! أو ﺻﻨﺎﻋﺔ اﻷﻟﻌﺎب أو ﺗﻄﻮﻳﺮ ﻣﻮﻗﻊ إﻟﻜﺘﺮوﻧﻲ ،ﻓﻮراء ﻛﻞ
ﻋﻠﻰ اﻟﺸﺎﺷﺔ ﺑﻬﺬە اﻟﺠﻤﺎﻟﻴﺔ ،ﻟﻴﺼﺒﺢ ﺑﺈﻣﻜﺎﻧﻚ ﻗﺮاءة اﻟﻤﻘﺎﻻت وﺗﺼﻔﺢ اﻟﺼﻮر ﺑﺒﺴﺎﻃﺔ.
ّ ْ
اﻟﺴﺆال اﻷﻫﻢ اﻟﺬي ُﻳﻄ َﺮ ُح ِﻣﻦ ﻗﺒﻞ اﻟﻌﺪﻳﺪ ﻣﻦ اﻟﻤﺒﺘﺪﺋﻴﻦ :ﻣﺎﻫﻲ أﻧﺴﺐ ﻟﻐﺔ ﺑﺮﻣﺠﺔ أﺑﺪأ ﺑﺘﻌﻠﻤﻬﺎ؟!
ّ
ﻧﻮﺿﺢ ً
أﻣﺮا ﻏﺎﻳﺔ ﻓﻲ اﻷﻫﻤﻴﺔ ﻟﻨﺠﻴﺐ ﻋﻦ ﻫﺬا اﻟﺴﺆال ،وﻗﺒﻞ اﻟﺤﺪﻳﺚ ﻋﻦ )ﺟﺴﺪ( ﻟﻐﺔ اﻟﺒﺮﻣﺠﺔ؛ ﻻﺑﺪ ﻟﻨﺎ ﻣﻦ أن
ﻋﻦ )روح( ﻟﻐﺔ اﻟﺒﺮﻣﺠﺔ! أﻻ وﻫﻲ اﻟﺨﻮارزﻣﻴﺎت ).(Algorithms
Computer Programming 1 Lecture 1
Sebha University
Faculty of Engineering
Civil Engineering Department
ّ ّ
وﻳﻤﻜﻨﻨﺎ اﻟﻘﻮل ﺑﺄن اﻟﺨﻮارزﻣﻴﺎت ﻫﻲ ﻟﻐﺔ اﻟﻌﻘﻞ ،أﻣﺎ اﻟﺒﺮﻣﺠﺔ ﻓﻬﻲ ﻟﻐﺔ اﻟﺤﺎﺳﺐ!
ّ
إﻣﻼﺋﻴﺔ ﻣﻔﺮوﺿﺔ ﻋﻠﻴﻚ أﺛﻨﺎء اﻟﻜﺘﺎﺑﺔ .ﻳﻤﻜﻨﻚ أن ﺗﺮﺳﻢ اﻟﻤﺴﺄﻟﺔ، ّ
اﻟﺨﻮارزﻣﻴﺎت ﻋﻠﻰ اﻟﻮرق ،وﻻ ﺗﻮﺟﺪ ﻗﻴﻮد ﺗﻜﺘﺐ
وﺗﺘﺄﻣﻞ اﻟﺨﻄﻮات ،وﻋﻨﺪﻣﺎ ﺗﺼﻞ إﻟﻰ اﻟﺤﻞ ﺗﻘﻮم ﺑﻜﺘﺎﺑﺘﻪ ﻋﻠﻰ اﻟﺤﺎﺳﺐ ﺑﻠﻐﺔ ﺑﺮﻣﺠﺔ ّ
ﻣﻌﻴﻨﺔ. ّ
ّ
رﻳﺎﺿﻴﺔ ﻟﻀﺮب اﻟﻤﺼﻔﻮﻓﺎت، ﻓﻬﻨﺎﻟﻚ ﺧﻮارزﻣﻴﺎت ﻟﻠﺒﺤﺚ ﻋﻦ أﺻﻐﺮ رﻗﻢ ﻣﻦ ﺑﻴﻦ ﻣﺠﻤﻮﻋﺔ أرﻗﺎم ،وﺧﻮارزﻣﻴﺎت
ّ
وﺧﻮارزﻣﻴﺎت ﻹﻳﺠﺎد وﺟﻪ اﻹﻧﺴﺎن ﻓﻲ ﺻﻮرة!
اﻟﻤﺎﺗﻼب ﻫﻲ ﻟﻐﺔ ذات ﻣﺴﺘﻮى ﻋﺎﻟﻲ ﻟﻠﺤﺴﺎﺑﺎت واﻟﺒﺮﻣﺠﺔ وﺗﻤﺘﺎز ﺑﻮﺟﻮد ﺑﺮاﻣﺞ ﺗﺴﻬﻞ ﻋﻤﻠﻴﺔ اﻟﺘﻌﺎﻣﻞ ﻣﻊ ﻫﺬە اﻟﻠﻐﺔ ،وﺗﺸﻤﻞ:
-اﻟﺤﺴﺎﺑﺎت اﻟﺮﻳﺎﺿﻴﺔ.
-ﺗﻄﻮﻳﺮ اﻟﺨﻮارزﻣﻴﺎت.
-ﻣﻌﺎﻟﺠﺔ اﻟﺒﻴﺎﻧﺎت .
-اﻟﻨﻤﺬﺟﺔ واﻟﻤﺤﺎﻛﺎة وﺗﺼﻤﻴﻢ اﻟﻤﺨﻄﻄﺎت اﻷوﻟﻴﺔ ﻟﻠﻤﺸﺎرﻳ ــﻊ.
-ﺗﺤﻠﻴﻞ اﻟﺒﻴﺎﻧﺎت وﻋﺮﺿﻬﺎ.
-ﻋﻤﻞ اﻟﺮﺳﻮﻣﺎت اﻟﻬﻨﺪﺳﻴﺔ واﻟﻌﻠﻤﻴﺔ.
وﻳﻌﺘﻤﺪ اﻟﻤﺎﺗﻼب ﻋﻠﻰ ﺗﻨﺴﻴﻖ اﻟﺒﻴﺎﻧﺎت ﻓﻲ ﺻﻮرة ﻣﺼﻔﻮﻓﺎت وﻳﻜﻮن اﻟﺒﺮﻧﺎﻣﺞ ﻣﺜﻞ أﻏﻠﺐ ﻟﻐﺎت اﻟﺒﺮﻣﺠﺔ .
ﻳﺆﻣﻦ ﺑﺮﻧﺎﻣﺞ اﻟﻤﺎﺗﻼب أدوات واﺟﻬﺔ اﻟﺘﺨﺎﻃﺐ اﻟﺮﺳﻮﻣﻴﺔ Interface Graphical Userاﻟﺘﻲ ﺗﺠﻌﻠﻚ ﺗﺘﻌﺎﻣﻞ ﻣﻊ اﻟﺒﺮﻧﺎﻣﺞ
ﻋﻠﻰ اﻧﻪ أداة ﺗﻄﺒﻴﻘﻴﺔ ﻣﺘﻄﻮرة.
Introduction
MATLAB is a powerful language for technical computing.
The name MATLAB stands for MATrix LABoratory, because its basic
data element is a matrix (array).
MATLAB can be used for math computations, modeling and simulations,
data analysis and processing, visualization and graphics, and algorithm
development.
MATLAB is widely used in universities and colleges in introductory and
advanced courses in mathematics, science, and especially engineering.
History of MATLAB:
Moler, the chairman of the computer science department at the University of New Mexico, started developing MATLAB
in the late 1970s.
In the 1980’s, MATLAB was rewritten in C with more functionality (such as plotting routines)
The Mathworks, Inc. was created in 1984.
The Mathworks is now responsible for development, sale, and support for MATLAB.
R2017a
، إذ ﻳﺴﺘﺨﺪم ﻫﺬا اﻟﺒﺮﻧﺎﻣﺞ ﻓﻲ ﻣﻌﻈﻢ اﻟﻤﺴﺎﺋﻞ اﻟﻌﻠﻤﻴﺔ واﻟﻬﻨﺪﺳﻴﺔ،ﻳﻌﺘﺒﺮ ﺑﺮﻧﺎﻣﺞ اﻟﻤﺎﺗﻼب اﻟﺒﺮﻧﺎﻣﺞ اﻷﺷﻬﺮ ﻓﻲ اﻷوﺳﺎط اﻟﻌﻠﻤﻴﺔ
وﺑﻌﺪ ﻧﻤﺬﺟﺔ أي ﻣﺴﺄﻟﺔ أو ﻇﺎﻫﺮة ﻳﺄﺗﻲ ﺑﻌﺪﻫﺎ دور ﻫﺬا اﻟﺒﺮﻧﺎﻣﺞ ﻟﻴﺘﻌﺎﻣﻞ ﻣﻊ ﺗﻠﻚ اﻟﺒﺮاﻣﺞ وﻳﺤﻠﻠﻬﺎ ﺑﺄﺑﺴﻂ اﻟﻄﺮق وأﺣﺪﺛﻬﺎ
وأﻳﺴﺮﻫﺎ ﺑﺮﻣﺠﺔ
#ﺗﻄﺒﻴﻘﺎت اﻟﻤﺎﺗﻼب:
ً ً
.1ﻋﻤﻞ ﻧﻤﻮذج رﻳﺎﺿﻲ ﻟﻠﻈﺎﻫﺮة اﻟﻔﻴﺰﻳﺎﺋﻴﺔ ،ﻳﺘﻀﻤﻦ ﻋﺎدة ﺗﻌﺒﻴﺮا ﻋﻦ اﻟﻨﻈﺎم اﻟﻄﺒﻴﻌﻲ أو اﻟﻬﻨﺪﺳﻲ ﻣﻦ ﻧﺎﺣﻴﺔ
اﻟﻤﻌﺎدﻻت اﻟﺘﻔﺎﺿﻠﻴﺔ اﻟﺠﺰﺋﻴﺔ.
.3ﻳﺘﻢ ﻋﻤﻞ ﺑﺮاﻣﺞ ﺣﺎﺳﻮب ﻟﺤﻞ اﻟﻤﻌﺎدﻻت اﻟﻤﺘﻔﺮدة ﺑﺎﺳﺘﺨﺪام ﻃﺮق ﻣﺒﺎﺷﺮة أو ﻃﺮق ﺗﻜﺮارﻳﺔ.
.4ﻳﺘﻢ اﻟﺘﺤﻘﻖ ﻣﻦ اﻟﻨﻤﻮذج اﻟﺮﻳﺎﺿﻲ واﻹﺟﺮاءات اﻟﻌﺪدﻳﺔ ورﻣﻮز اﻟﺤﺎﺳﻮب ﺑﺎﺳﺘﺨﺪام ﻧﺘﺎﺋﺞ ﺗﺠﺮﻳﺒﻴﺔ أو
ﻧﻤﺎذج ُﻣﺒﺴﻄﺔ ﻳﻮﺟﺪ ﻟﻬﺎ ﺣﻠﻮل ﺗﺤﻠﻴﻠﻴﺔ دﻗﻴﻘﺔ.
Strengths of MATLAB
MATLAB is relatively easy to learn.
MATLAB code is optimized to be relatively quick when performing matrix operations.
MATLAB may behave like a calculator or as a programming language.
MATLAB is interpreted, errors are easier to fix.
Weaknesses of MATLAB
MATLAB is NOT a general purpose programming language
MATLAB is an interpreted language (making it for the most part slower than a compiled language
such as C++)
MATLAB is designed for scientific computation and is not suitable for some things (such as parsing
text- to analyze (a string of characters) in order to associate groups of characters with the syntactic units
of the underlying grammar)
Computer Programming 1 Lecture 1
Sebha University
Faculty of Engineering
Civil Engineering Department
Workspace Window
Current Folder Window
ﻣﻨﻄﻘﺔ اﻟﻌﻤﻞ
Store all
Variables
Command History
Window ﻧﺎﻓﺬة ﺗﺴﺠﻴﻞ
اﻻواﻣﺮ
Command Window ()ﻧﺎﻓﺬة اﻷواﻣﺮ: The Command Window is MATLAB 's main window and
opens when MATLAB is started.
Figure Window opens automatically after any command that draws a graph.
Editor Window: Use Editor Window to write and debug MATLAB scripts. Open with edit
command.
Help Window: Get Help Window by clicking on Help icon (question mark) in tool strip
Basic procedure:
1. At prompt (>>), type in MATLAB
command
2. Press ENTER key
3. MATLAB displays result in Command
Window, followed by a prompt
4. Repeat from step 1
Semicolon (;)
When typed at end of command, suppresses output. (Only prompt displayed at next
line)
• Useful for preventing display of large outputs
Percent sign(%)
When typed at beginning of line, MATLAB treats line as a comment and doesn’t
execute line.
clc command
• Clears Command Window display
Up and down arrows still bring back previous commands
In this chapter will only discuss arithmetic with scalars (single numbers)
• Can do arithmetic directly on numbers (like a calculator)
• Can store numbers in variables
Multiplication * 5*3
Right division / 5/3
Left division \ 5\3=3/5
Exponentiation ^ 5 ^ 3 (means 53 = 125)
Precedence order
• Same as most calculators
• Same as doing arithmetic by hand
• For multiple operations of same precedence,
MATLAB goes left to right
• Can change order by using parentheses