0% found this document useful (0 votes)
13 views

1 Starting With MATLAB

Uploaded by

rfks112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

1 Starting With MATLAB

Uploaded by

rfks112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

‫‪Sebha University‬‬

‫‪Faculty of Engineering‬‬
‫‪Civil Engineering Department‬‬

‫ﻣﺎ ﻫﻲ اﻟﺒﺮﻣﺠﺔ؟‬
‫ﱠ‬
‫ﻋﻤﻠﻴﺔ ﺗﻌﻠﻴﻢ اﻟﺤﺎﺳﻮب إﺟﺮاء ﻣﻬﻤﺔ ﻣﺎ‬ ‫ُﻳﻤﻜﻨﻨﺎ اﻹﺟﺎﺑﺔ ﻋﻦ ﻫﺬا اﻟﺴﺆال ﺑﺎﻟﺠﻮاب اﻟﺒﺴﻴﻂ اﻟﺘﺎﻟﻲ‪" :‬اﻟﺒﺮﻣﺠﺔ ﻫﻲ‬

‫اﻟﺒﺮﻣﺠﺔ ﺑﺒﺴﺎﻃﺔ‪ :‬ﻫﻲ ﻣﺠﻤﻮﻋﺔ اﻷواﻣﺮ اﻟﺘﻲ ﺗﻌﻄﻴﻬﺎ ﻟﻠﺤﺎﺳﻮب‪ ،‬ﻟﻴﻘﻮم اﻷﺧﻴﺮ ﺑﺘﻨﻔﻴﺬﻫﺎ! ﻫﺬە اﻷواﻣﺮ ﺗﻜﻮن‬
‫ﻣﻜﺘﻮﺑﺔ ﺑﺸﻜﻞ “ﻏﺮﻳﺐ” ﺑﺎﻟﻠﻐﺔ اﻹﻧﺠﻠﻴﺰﻳﺔ‬

‫ﱠ‬
‫ﻟﻐﺔ اﻟﺤﺎﺳﻮب اﻟﻄﺒﻴﻌﻴﺔ‬
‫ﱠ‬ ‫ّ‬ ‫ُ‬ ‫ّ‬ ‫ُ‬ ‫َ‬ ‫َ‬ ‫ﱠ‬ ‫ُ‬
‫ﻟﻶﻻت ﻟﻐﺔ ﻃﺒﻴﻌﻴﺔ ﻛﻤﺎ اﻟﺒﺸﺮ‪ .‬ﻻ ﺗﻔﻬﻢ اﻟﺤﻮاﺳﻴﺐ ﻟﻐﺔ اﻟﺒﺸﺮ اﻟﻄﺒﻴﻌﻴﺔ‪ ،‬ﻓﻠﻐﺘﻬﺎ اﻟﺨﺎﺻﺔ ﻣﻜﻮﻧﺔ ﻣﻦ أﻛﻮاد‬
‫ﱠ‬
‫ﺛﻨﺎﺋﻴﺔ‪ ،‬أي أﺻﻔﺎر )‪ (0‬وواﺣﺪات )‪.(1‬‬

‫‪Computer Programming 1‬‬ ‫‪Lecture 1‬‬


‫‪Sebha University‬‬
‫‪Faculty of Engineering‬‬
‫‪Civil Engineering Department‬‬

‫ﻣﺪﺧﻞ إﻟﻰ ﻟﻐﺎت اﻟﺒﺮﻣﺠﺔ‪:‬‬


‫ُ‬ ‫ﱡ‬ ‫ﱠ‬ ‫ﱠ‬ ‫ﱠ‬ ‫ﱠ‬ ‫ُ ً‬ ‫َ‬
‫ﺴﺘﺨﺪم ﻟﻐﺔ أﻗﺮب إﻟﻰ ﻟﻐﺎﺗﻨﺎ اﻟﺒﺸﺮﻳﺔ‪ ،‬ﻛﺎﻟﻌﺮﺑﻴﺔ واﻹﻧﺠﻠﻴﺰﻳﺔ واﻟﻔﺮﻧﺴﻴﺔ‪ ،‬ﻟﻠﺘﺤﺪث إﻟﻰ اﻵﻻت اﻟﻤﺘﺤﺪﺛﺔ ﺑﺎﻷرﻗﺎم‬ ‫ِ‬ ‫ﻧ‬
‫ﱡ ُ‬ ‫ّ‬ ‫ُ‬
‫ﺗﻨﻈﻴﻤﺎ وﻳﻨﺒﻐﻲ أن ﺗﻜﻮن ُﻣﻤﻜﻨﺔ اﻟﺘﻌﻠﻢ ﻛﻠﻴﺎ‪.‬‬
‫ً‬ ‫ّ‬
‫اﻟﻄﺒﻴﻌﻴﺔ‪ ،‬إﻻ أﻧﻬﺎ أﻛﺜﺮ‬ ‫ﱠ‬
‫اﻟﺜﻨﺎﺋﻴﺔ‪ .‬ﻟﻐﺎت اﻟﺒﺮﻣﺠﺔ أﻗﺮب إﻟﻰ ﻟﻐﺎﺗﻨﺎ‬

‫ﻛﻞ ﻣﻜﺎن‪ ،‬وﺑﻮاﺳﻄﺘﻬﺎ ﻳﻤﻜﻨﻚ اﻟﺘﺤﻜﻢ ﻓﻲ اﻵﻻت‪ ،‬ﻓﻤﻦ روﺑﻮت ﻳﺮﻋﻰ اﻟﻤﺮﻳﺾ إﻟﻰ آﺧﺮ ﻳﺒﺤﺚ‬ ‫ﺗﻈﻬﺮ اﻟﺒﺮﻣﺠﺔ ﻓﻲ ّ‬
‫ٌ ّ‬
‫ﻣﻮﻗﻊ ﻣﺒﺮﻣﺞ ﻳﻌﺪ ﻣﺎ ﺗﺮاە‬
‫ٍ‬ ‫ﻋﻦ اﻟﻤﺎء ﻓﻲ ﻛﻮﻛﺐ اﻟﻤﺮﻳ ــﺦ! أو ﺻﻨﺎﻋﺔ اﻷﻟﻌﺎب أو ﺗﻄﻮﻳﺮ ﻣﻮﻗﻊ إﻟﻜﺘﺮوﻧﻲ‪ ،‬ﻓﻮراء ﻛﻞ‬
‫ﻋﻠﻰ اﻟﺸﺎﺷﺔ ﺑﻬﺬە اﻟﺠﻤﺎﻟﻴﺔ‪ ،‬ﻟﻴﺼﺒﺢ ﺑﺈﻣﻜﺎﻧﻚ ﻗﺮاءة اﻟﻤﻘﺎﻻت وﺗﺼﻔﺢ اﻟﺼﻮر ﺑﺒﺴﺎﻃﺔ‪.‬‬
‫ّ‬ ‫ْ‬
‫اﻟﺴﺆال اﻷﻫﻢ اﻟﺬي ُﻳﻄ َﺮ ُح ِﻣﻦ ﻗﺒﻞ اﻟﻌﺪﻳﺪ ﻣﻦ اﻟﻤﺒﺘﺪﺋﻴﻦ‪ :‬ﻣﺎﻫﻲ أﻧﺴﺐ ﻟﻐﺔ ﺑﺮﻣﺠﺔ أﺑﺪأ ﺑﺘﻌﻠﻤﻬﺎ؟!‬

‫ّ‬
‫ﻧﻮﺿﺢ ً‬
‫أﻣﺮا ﻏﺎﻳﺔ ﻓﻲ اﻷﻫﻤﻴﺔ‬ ‫ﻟﻨﺠﻴﺐ ﻋﻦ ﻫﺬا اﻟﺴﺆال‪ ،‬وﻗﺒﻞ اﻟﺤﺪﻳﺚ ﻋﻦ )ﺟﺴﺪ( ﻟﻐﺔ اﻟﺒﺮﻣﺠﺔ؛ ﻻﺑﺪ ﻟﻨﺎ ﻣﻦ أن‬
‫ﻋﻦ )روح( ﻟﻐﺔ اﻟﺒﺮﻣﺠﺔ! أﻻ وﻫﻲ اﻟﺨﻮارزﻣﻴﺎت )‪.(Algorithms‬‬
‫‪Computer Programming 1‬‬ ‫‪Lecture 1‬‬
‫‪Sebha University‬‬
‫‪Faculty of Engineering‬‬
‫‪Civil Engineering Department‬‬

‫ً‬ ‫ً‬ ‫ً‬ ‫اﻟﺨﻮارزﻣﻴﺔ ﺑﻜﻞ ﺑﺴﺎﻃﺔ ﻣﺎ ﻫﻲ إﻻ‪ :‬ﺧﻄﻮات إﻧﺠﺎز ّ‬


‫ّ‬
‫ﻣﻬﻤ ٍﺔ ﻣﺎ! ﻓﺄﻧﺖ ﻣﺜ ﺗﺘﺒﻊ ﻧﻤﻄﺎ ﻣﻌﻴﻨﺎ ﻛﻞ ﻳﻮم ﻟﻠﺬﻫﺎب إﻟﻰ ﻣﺪرﺳﺘﻚ‪،‬‬
‫ّ ً‬
‫وﺧﻄﻮات ﻣﻌﻴﻨﺔ ﻣﻦ أﺟﻞ إﻋﺪاد ﺷﻴﺊ ﻣﺘﻜﺮر ﺑﺸﻜﻞ ﻳﻮﻣﻲ!‬
‫ٍ‬ ‫أو ﻣﻜﺎن ﻋﻤﻠﻚ‪،‬‬

‫ﻓﺎﻟﺨﻮارزﻣﻴﺎت ﻟﻴﺴﺖ ﻓﻘﻂ ً‬


‫ﺣﻜﺮا ﻋﻠﻰ ﻣﺠﺎل اﻟﺒﺮﻣﺠﺔ‪ ،‬ﺑﻞ ﺗﺴﺘﻌﻤﻞ ﻓﻲ اﻟﻌﺪﻳﺪ ﻣﻦ اﻟﻤﺠﺎﻻت اﻷﺧﺮى‪ ،‬ﻛﺎﻟﺮﻳﺎﺿﻴﺎت‪،‬‬
‫واﻟﻔﻴﺰﻳﺎء‪ ،‬واﻟﻄﺐ وﻏﻴﺮ ذﻟﻚ‪.‬‬

‫ّ‬ ‫ّ‬
‫وﻳﻤﻜﻨﻨﺎ اﻟﻘﻮل ﺑﺄن اﻟﺨﻮارزﻣﻴﺎت ﻫﻲ ﻟﻐﺔ اﻟﻌﻘﻞ‪ ،‬أﻣﺎ اﻟﺒﺮﻣﺠﺔ ﻓﻬﻲ ﻟﻐﺔ اﻟﺤﺎﺳﺐ!‬

‫ّ‬
‫إﻣﻼﺋﻴﺔ ﻣﻔﺮوﺿﺔ ﻋﻠﻴﻚ أﺛﻨﺎء اﻟﻜﺘﺎﺑﺔ‪ .‬ﻳﻤﻜﻨﻚ أن ﺗﺮﺳﻢ اﻟﻤﺴﺄﻟﺔ‪،‬‬ ‫ّ‬
‫اﻟﺨﻮارزﻣﻴﺎت ﻋﻠﻰ اﻟﻮرق‪ ،‬وﻻ ﺗﻮﺟﺪ ﻗﻴﻮد‬ ‫ﺗﻜﺘﺐ‬
‫وﺗﺘﺄﻣﻞ اﻟﺨﻄﻮات‪ ،‬وﻋﻨﺪﻣﺎ ﺗﺼﻞ إﻟﻰ اﻟﺤﻞ ﺗﻘﻮم ﺑﻜﺘﺎﺑﺘﻪ ﻋﻠﻰ اﻟﺤﺎﺳﺐ ﺑﻠﻐﺔ ﺑﺮﻣﺠﺔ ّ‬
‫ﻣﻌﻴﻨﺔ‪.‬‬ ‫ّ‬

‫ّ‬
‫رﻳﺎﺿﻴﺔ ﻟﻀﺮب اﻟﻤﺼﻔﻮﻓﺎت‪،‬‬ ‫ﻓﻬﻨﺎﻟﻚ ﺧﻮارزﻣﻴﺎت ﻟﻠﺒﺤﺚ ﻋﻦ أﺻﻐﺮ رﻗﻢ ﻣﻦ ﺑﻴﻦ ﻣﺠﻤﻮﻋﺔ أرﻗﺎم‪ ،‬وﺧﻮارزﻣﻴﺎت‬
‫ّ‬
‫وﺧﻮارزﻣﻴﺎت ﻹﻳﺠﺎد وﺟﻪ اﻹﻧﺴﺎن ﻓﻲ ﺻﻮرة!‬

‫‪Computer Programming 1‬‬ ‫‪Lecture 1‬‬


‫‪Sebha University‬‬
‫‪Faculty of Engineering‬‬
‫‪Civil Engineering Department‬‬

‫اﻟﻤﺎﺗﻼب ﻫﻲ ﻟﻐﺔ ذات ﻣﺴﺘﻮى ﻋﺎﻟﻲ ﻟﻠﺤﺴﺎﺑﺎت واﻟﺒﺮﻣﺠﺔ وﺗﻤﺘﺎز ﺑﻮﺟﻮد ﺑﺮاﻣﺞ ﺗﺴﻬﻞ ﻋﻤﻠﻴﺔ اﻟﺘﻌﺎﻣﻞ ﻣﻊ ﻫﺬە اﻟﻠﻐﺔ ‪ ،‬وﺗﺸﻤﻞ‪:‬‬
‫‪ -‬اﻟﺤﺴﺎﺑﺎت اﻟﺮﻳﺎﺿﻴﺔ‪.‬‬
‫‪ -‬ﺗﻄﻮﻳﺮ اﻟﺨﻮارزﻣﻴﺎت‪.‬‬
‫‪ -‬ﻣﻌﺎﻟﺠﺔ اﻟﺒﻴﺎﻧﺎت ‪.‬‬
‫‪ -‬اﻟﻨﻤﺬﺟﺔ واﻟﻤﺤﺎﻛﺎة وﺗﺼﻤﻴﻢ اﻟﻤﺨﻄﻄﺎت اﻷوﻟﻴﺔ ﻟﻠﻤﺸﺎرﻳ ــﻊ‪.‬‬
‫‪ -‬ﺗﺤﻠﻴﻞ اﻟﺒﻴﺎﻧﺎت وﻋﺮﺿﻬﺎ‪.‬‬
‫‪ -‬ﻋﻤﻞ اﻟﺮﺳﻮﻣﺎت اﻟﻬﻨﺪﺳﻴﺔ واﻟﻌﻠﻤﻴﺔ‪.‬‬

‫وﻳﻌﺘﻤﺪ اﻟﻤﺎﺗﻼب ﻋﻠﻰ ﺗﻨﺴﻴﻖ اﻟﺒﻴﺎﻧﺎت ﻓﻲ ﺻﻮرة ﻣﺼﻔﻮﻓﺎت وﻳﻜﻮن اﻟﺒﺮﻧﺎﻣﺞ ﻣﺜﻞ أﻏﻠﺐ ﻟﻐﺎت اﻟﺒﺮﻣﺠﺔ ‪.‬‬
‫ﻳﺆﻣﻦ ﺑﺮﻧﺎﻣﺞ اﻟﻤﺎﺗﻼب أدوات واﺟﻬﺔ اﻟﺘﺨﺎﻃﺐ اﻟﺮﺳﻮﻣﻴﺔ ‪ Interface Graphical User‬اﻟﺘﻲ ﺗﺠﻌﻠﻚ ﺗﺘﻌﺎﻣﻞ ﻣﻊ اﻟﺒﺮﻧﺎﻣﺞ‬
‫ﻋﻠﻰ اﻧﻪ أداة ﺗﻄﺒﻴﻘﻴﺔ ﻣﺘﻄﻮرة‪.‬‬

‫‪Computer Programming 1‬‬ ‫‪Lecture 1‬‬


Sebha University
Faculty of Engineering
Civil Engineering Department

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.

 In industry the software is used in research, development, and design.


 The standard MATLAB program has tools (functions) that can be used
to solve common problems.
 In addition, MATLAB has optional toolboxes that are collections of
specialized programs designed to solve specific types of problems.

Computer Programming 1 Lecture 1


Sebha University
Faculty of Engineering
Civil Engineering Department

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

،‫ إذ ﻳﺴﺘﺨﺪم ﻫﺬا اﻟﺒﺮﻧﺎﻣﺞ ﻓﻲ ﻣﻌﻈﻢ اﻟﻤﺴﺎﺋﻞ اﻟﻌﻠﻤﻴﺔ واﻟﻬﻨﺪﺳﻴﺔ‬،‫ﻳﻌﺘﺒﺮ ﺑﺮﻧﺎﻣﺞ اﻟﻤﺎﺗﻼب اﻟﺒﺮﻧﺎﻣﺞ اﻷﺷﻬﺮ ﻓﻲ اﻷوﺳﺎط اﻟﻌﻠﻤﻴﺔ‬
‫وﺑﻌﺪ ﻧﻤﺬﺟﺔ أي ﻣﺴﺄﻟﺔ أو ﻇﺎﻫﺮة ﻳﺄﺗﻲ ﺑﻌﺪﻫﺎ دور ﻫﺬا اﻟﺒﺮﻧﺎﻣﺞ ﻟﻴﺘﻌﺎﻣﻞ ﻣﻊ ﺗﻠﻚ اﻟﺒﺮاﻣﺞ وﻳﺤﻠﻠﻬﺎ ﺑﺄﺑﺴﻂ اﻟﻄﺮق وأﺣﺪﺛﻬﺎ‬
‫وأﻳﺴﺮﻫﺎ ﺑﺮﻣﺠﺔ‬

Computer Programming 1 Lecture 1


‫‪Sebha University‬‬
‫‪Faculty of Engineering‬‬
‫‪Civil Engineering Department‬‬

‫‪ #‬ﺗﻄﺒﻴﻘﺎت اﻟﻤﺎﺗﻼب‪:‬‬

‫‪ -1‬ﻓﻲ اﻟﻤﺠﺎل اﻷﻛﺎدﻳﻤﻲ ‪:‬‬


‫‪ -‬ﻋﻤﻞ اﻟﺘﻔﺎﺻﻴﻞ واﻟﺘﻜﺎﻣﻞ‪.‬‬
‫‪ -‬ﺣﻞ اﻟﻤﻌﺎدﻻت اﻟﺠﺒﺮﻳﺔ ‪Algebraic Equations‬‬
‫‪ -‬ﺣﻞ اﻟﻤﻌﺎدﻻت اﻟﺘﻔﺎﺿﻠﻴﺔ ذات اﻟﺮﺗﺐ اﻟﻌﻠﻴﺎ ‪Differential Equations‬‬
‫‪ -‬ﻋﻤﻞ اﻟﺘﻔﺎﺿﻞ اﻟﺠﺰﺋﻲ ‪ ،‬وﻋﻤﻠﻴﺎت اﻟﻜﺴﺮ اﻟﺠﺰﺋﻲ ‪Partial fraction‬‬
‫‪ -2‬اﻟﻤﺠﺎﻻت اﻟﺘﻄﺒﻴﻘﻴﺔ ‪:‬‬
‫‪ -‬أﻧﻈﻤﺔ اﻟﺘﺤﻜﻢ ﻓﻲ ﺟﻤﻴﻊ اﻟﻤﺠﺎﻻت اﻟﻬﻨﺪﺳﻴﺔ ‪Control System‬‬
‫‪ -‬ﻣﻌﺎﻟﺠﺔ اﻟﺼﻮر‬
‫‪ -‬ﻣﺤﺎﻛﺎة اﻹﻟﻜﺘﺮوﻧﺎت‬
‫‪ -‬ﻣﺠﺎل ﺻﻨﺎﻋﺔ اﻟﺴﻴﺎرات‬
‫‪ -‬ﻣﺠﺎل اﻟﻄﻴﺮان واﻟﺪﻓﺎع اﻟﺠﻮي‬
‫‪ -‬ﻣﺠﺎل اﻟﺮوﺑﻮت‬
‫وﻫﻨﺎك اﻟﻜﺜﻴﺮ واﻟﻜﺜﻴﺮ ﻣﻦ اﻟﺘﻄﺒﻴﻘﺎت اﻟﻬﻨﺪﺳﻴﺔ ‪.‬‬
‫‪Computer Programming 1‬‬ ‫‪Lecture 1‬‬
‫‪Sebha University‬‬
‫‪Faculty of Engineering‬‬
‫‪Civil Engineering Department‬‬

‫اﻟﻤﻴﻜﺎﻧﻴﻜﺎ اﻟﺤﺎﺳﻮﺑﻴﺔ )‪(Computational Mechanics‬‬


‫ﻳﺘﺒﻊ ﻋﻠﻤﺎء ﻣﻦ داﺧﻞ ﻣﺠﺎل اﻟﻤﻴﻜﺎﻧﻴﻜﺎ اﻟﺤﺎﺳﻮﺑﻴﺔ ﻗﺎﺋﻤﺔ ﻣﻦ اﻟﻤﻬﺎم ﻟﺘﺤﻠﻴﻞ ﻋﻤﻠﻴﺘﻬﻢ اﻟﻤﻴﻜﺎﻧﻴﻜﻴﺔ ُ‬
‫اﻟﻤﺴﺘﻬﺪﻓﺔ‪:‬‬

‫ً‬ ‫ً‬
‫‪ .1‬ﻋﻤﻞ ﻧﻤﻮذج رﻳﺎﺿﻲ ﻟﻠﻈﺎﻫﺮة اﻟﻔﻴﺰﻳﺎﺋﻴﺔ‪ ،‬ﻳﺘﻀﻤﻦ ﻋﺎدة ﺗﻌﺒﻴﺮا ﻋﻦ اﻟﻨﻈﺎم اﻟﻄﺒﻴﻌﻲ أو اﻟﻬﻨﺪﺳﻲ ﻣﻦ ﻧﺎﺣﻴﺔ‬
‫اﻟﻤﻌﺎدﻻت اﻟﺘﻔﺎﺿﻠﻴﺔ اﻟﺠﺰﺋﻴﺔ‪.‬‬

‫ﻣﻨﺎﺳﺒﺔ ﻟﻠﺤﻮﺳﺒﺔ اﻟﺮﻗﻤﻴﺔ‪.‬‬


‫ٍ‬ ‫أﺷﻜﺎل‬
‫ٍ‬ ‫‪ .2‬ﺗﺤﻮﻳﻞ اﻟﻤﻌﺎدﻻت اﻟﺮﻳﺎﺿﻴﺔ إﻟﻰ‬

‫‪ .3‬ﻳﺘﻢ ﻋﻤﻞ ﺑﺮاﻣﺞ ﺣﺎﺳﻮب ﻟﺤﻞ اﻟﻤﻌﺎدﻻت اﻟﻤﺘﻔﺮدة ﺑﺎﺳﺘﺨﺪام ﻃﺮق ﻣﺒﺎﺷﺮة أو ﻃﺮق ﺗﻜﺮارﻳﺔ‪.‬‬

‫‪ .4‬ﻳﺘﻢ اﻟﺘﺤﻘﻖ ﻣﻦ اﻟﻨﻤﻮذج اﻟﺮﻳﺎﺿﻲ واﻹﺟﺮاءات اﻟﻌﺪدﻳﺔ ورﻣﻮز اﻟﺤﺎﺳﻮب ﺑﺎﺳﺘﺨﺪام ﻧﺘﺎﺋﺞ ﺗﺠﺮﻳﺒﻴﺔ أو‬
‫ﻧﻤﺎذج ُﻣﺒﺴﻄﺔ ﻳﻮﺟﺪ ﻟﻬﺎ ﺣﻠﻮل ﺗﺤﻠﻴﻠﻴﺔ دﻗﻴﻘﺔ‪.‬‬

‫‪Computer Programming 1‬‬ ‫‪Lecture 1‬‬


Sebha University
Faculty of Engineering
Civil Engineering Department

Computer Programming 1 Lecture 1


Sebha University
Faculty of Engineering
Civil Engineering Department

Computer Programming 1 Lecture 1


Sebha University
Faculty of Engineering
Civil Engineering Department

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

Chapter l Starting with MATLAB


Icons
 Window Action icon – the icon showing a down arrow with a
circle around it, i.e., It is in the upper, right corner of most
MATLAB windows
 Help icon – the question-mark icon ( ) in the Resources
Section of the desktop toolbar
 Layout icon – ( ) in the Environment Section of the desktop
toolbar
Computer Programming 1 Lecture 1
Sebha University
Faculty of Engineering
Civil Engineering Department

This chapter describes:


 MATLAB windows
 Command Window in detail
 How to do basic arithmetic
 Create basic variables
 Introductory script files

Computer Programming 1 Lecture 1


Sebha University
Faculty of Engineering
Civil Engineering Department

• 1.1 Starting MATLAB, MATLAB Windows

Workspace Window
Current Folder Window

‫ﻣﻨﻄﻘﺔ اﻟﻌﻤﻞ‬

Store all
Variables

Command History
Window ‫ﻧﺎﻓﺬة ﺗﺴﺠﻴﻞ‬
‫اﻻواﻣﺮ‬

Command Window ‫ﻧﺎﻓﺬة اﻷواﻣﺮ‬


Computer Programming 1 Lecture 1
Sebha University
Faculty of Engineering
Civil Engineering Department

MATLAB windows Window Purpose (‫)اﻟﻐﺮض‬


Command Window Main window, enters variables, runs
programs.
Figure Window Contains output from graphic
commands.
Editor Window Creates and debugs script and
function files.
Help Window Provides help information.
Command History Window Logs commands entered in the
Command Window.
Workspace Window Provides information about the
variables that are stored.
Current Folder Window Shows the files in the current folder.

Computer Programming 1 Lecture 1


Sebha University
Faculty of Engineering
Civil Engineering Department

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.

Computer Programming 1 Lecture 1


Sebha University
Faculty of Engineering
Civil Engineering Department

Editor Window: Use Editor Window to write and debug MATLAB scripts. Open with edit
command.

Computer Programming 1 Lecture 1


Sebha University
Faculty of Engineering
Civil Engineering Department

Help Window: Get Help Window by clicking on Help icon (question mark) in tool strip

Computer Programming 1 Lecture 1


Sebha University
Faculty of Engineering
Civil Engineering Department

Command Window is MATLAB’s main window.


Use it to:
Execute commands (‫)ﺗﻨﻔﻴﺬ اﻷواﻣﺮ‬
Open other windows (‫)ﻓﺘﺢ ﻧﻮاﻓﺬ أﺧﺮى‬
Run programs that you’ve written (‫)ﺗﺸﻐﻴﻞ اﻟﺒﺮﻧﺎﻣﺞ اﻟﺬي ﺗﻢ ﻛﺘﺎﺑﺘﻪ‬
Manage the MATLAB software (‫)إدارة اﻟﺒﺮﻧﺎﻣﺞ‬

Computer Programming 1 Lecture 1


Sebha University
Faculty of Engineering
Civil Engineering Department

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

Computer Programming 1 Lecture 1


Sebha University
Faculty of Engineering
Civil Engineering Department

Notes on Command Window


 To start a command, make sure cursor is next to prompt
 MATLAB won’t respond until you press ENTER
• It then executes only last command
• Commands before last one may still be visible, but MATLAB doesn’t execute them
 Can type several commands in same line by putting a comma between commands
• Hard to read, so don’t do this often
 If command too long to fit on line, can continue to next line by typing ellipsis (3
periods, i.e., … ) and then pressing ENTER

Computer Programming 1 Lecture 1


Sebha University
Faculty of Engineering
Civil Engineering Department

When cursor is in bottom command line:


•  key moves cursor one character to left
•  key moves cursor one character to right
•  key recalls preceding command
•  key recalls command that follows one being displayed.

Computer Programming 1 Lecture 1


Sebha University
Faculty of Engineering
Civil Engineering Department

 PAGE-UP key moves up to previous commands in a window-size at a time.


 PAGE-DOWN key moves down to previous commands in a window-size
at a time.
 BACKSPACE key deletes character to left of cursor.
 DELETE key deletes character to right of cursor.

Computer Programming 1 Lecture 1


Sebha University
Faculty of Engineering
Civil Engineering Department

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

Computer Programming 1 Lecture 1


Sebha University
Faculty of Engineering
Civil Engineering Department

Command History Window


• Shows previous commands, including ones from previous MATLAB sessions
• Double-clicking on command puts it in Command Window and executes it
• Can drag command to Command Window, make changes in command, then execute it
• To clear one or more commands, select the lines to delete, right click, choose Delete
Selection
• To clear entire history, right click, select Clear Command History

Computer Programming 1 Lecture 1


Sebha University
Faculty of Engineering
Civil Engineering Department

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

Computer Programming 1 Lecture 1


Sebha University
Faculty of Engineering
Civil Engineering Department

Symbols for arithmetic are:

Operation Symbol Example


Addition + 5+3
Subtraction – 5–3
Left division rarely used with scalars

Multiplication * 5*3
Right division / 5/3
Left division \ 5\3=3/5
Exponentiation ^ 5 ^ 3 (means 53 = 125)

Computer Programming 1 Lecture 1


Sebha University
Faculty of Engineering
Civil Engineering Department

Order in which MATLAB does arithmetic


Precedence Mathematical Operation

First Parentheses. For nested parentheses, the


innermost are executed first.
Second Exponentiation.
Third Multiplication, division (equal precedence).
Fourth Addition and subtraction.

Computer Programming 1 Lecture 1


Sebha University
Faculty of Engineering
Civil Engineering Department

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

Computer Programming 1 Lecture 1


Sebha University
Faculty of Engineering
Civil Engineering Department

Can use MATLAB as a (very expensive!) calculator


1. Type in mathematical expression
2. Press Enter key
3. MATLAB displays answer in Command Window as ans = followed by the result
Your display may appear on more than one line and have blank lines between text

Computer Programming 1 Lecture 1


Sebha University
Faculty of Engineering
Civil Engineering Department

Can control display of numbers with format command


• Once enter command, format stays the same until another format command
• Default format is fixed point with four digits to right of decimal point
• fixed-point means decimal point always between one’s-digit and one-tenth’s digit
• Format only affects display of numbers. MATLAB always computes and saves numbers
in full precision

Computer Programming 1 Lecture 1


Sebha University
Faculty of Engineering
Civil Engineering Department

Some types of formatting

Computer Programming 1 Lecture 1

You might also like