You are on page 1of 52

MATLAB

Lecturer:
Mr. Munther R.
Content
Chapter 1: The Desktop Window and Basic Mathematical Definitions
 Introduction
 MATLAB Windows
 Basic Arithmetic
 General Command
 Basic Mathematical Definitions
 Complex Numbers

Chapter 2: Vectors and Matrices


 Vectors and Characterizing a Vector
 Vector Dot and Cross Products
 Referencing Vector Components
 Basic Operations with Matrices
 More Basic Operations
 Special Matrix Types
 Referencing Matrix Elements
 Finding Determinants and Solving Linear Systems
 Finding the Inverse of a Matrix

Chapter 3: Control Flow Statements and M-file


 Relational and logical operators
 Input and output statements
 Control Flow Statements
 Loops
 The for Loop
 The While Loop
 Infinite Loops
 Decisions
 The if Statement
 The Switch Statement
 M-File
 Script Files
 Function Files
 Multiple inputs and outputs

Lecturer: Mr. Munther R.


1
Chapter 4: Plotting and Graphics
 Basic 2D Plotting
 2D Plotting Options
 Labels
 Adding Legends
 Line styles
 The Axis Commands
 Showing Multiple Functions on One Plot
 Subplots
 Polar and Logarithmic Plots

Chapter 5: Solving Algebraic Equations


 Solving Quadratic Equations
 Solving Higher Order Equations
 Systems of Equations
 Nonlinear Equations

Chapter 6: Derivation, Integration and Differential Equation


 Derivation
 Integration
 Differential equation

Lecturer: Mr. Munther R.


2
C
hapter 1:
The Desktop Window and
Basic Mathematical
Definitions

 Introduction
 MATLAB Windows
 The Basic Arithmetic
 General Command
 Basic Mathematical Definitions
 Complex Numbers

Lecturer: Mr. Munther R.


3
‫‪Introduction‬‬ ‫المقدمٌ‬
‫‪٠‬ؼزجص ثصٔابِظ ‪ MATLAB‬اٌجصٔابِظ األشا‪ٙ‬ص فا‪ ٟ‬األ‪ٚ‬غابغ اٌؼٍّ‪١‬اخ وجصٔابِظ ‪ ٌ ٚ‬اخ ذ ز ‪٠‬ػازدرَ ٘اسا اٌجصٔابِظ فا‪ٟ‬‬
‫ِؼظُ اٌّػبئً اٌؼٍّ‪١‬خ ‪ٚ‬اٌ‪ٕٙ‬رغ‪١‬خ ذ ‪٠ٚ‬زؼبًِ ِغ ثصاِظ رٍه اٌّػبئً ثأثػػ غصق اٌجصِغخ ذ ‪ ِٓٚ‬اٌغر‪٠‬ص ثبٌسوص ثبْ ٘اسا‬
‫اٌجصٔبِظ ‪ُ٠‬رشظ ف‪ ٟ‬أوضص اٌّؼب٘ر ‪ ٚ‬اٌىٍ‪١‬بد اٌ‪ٛ‬ال‪٠‬بد اٌّزؾرح االِص‪٠‬ى‪١‬خ ‪ ٚ‬اال‪ٚ‬شث‪١‬خ ‪ ٚ‬ثم‪١‬خ ذ‪ٚ‬ي اٌؼبٌُ ‪.‬‬

‫‪ٚ‬رؼزجص ٌ خ ‪ ٌ MATLAB‬خ ثصِغخ ػبٌ‪١‬خ األذاء رػزدرَ إلعصاء اٌؾػبثبد اٌص‪٠‬بظا‪١‬خ ‪ ٚ‬اٌ‪ٕٙ‬رغا‪١‬خ ‪ٚ‬اٌزمٕ‪١‬ابد اٌؼٍا‪َٛ‬‬
‫االخص‪ ٜ‬ذ ‪ٚ‬رم‪ َٛ‬ثؼٍّ‪١‬بٌد اٌؾػبثبد ‪ ٚ‬اظ‪ٙ‬بش إٌزبئظ ظّٓ ث‪١‬ئخ غ‪ٍٙ‬خ اٌجصِغخ ذ ‪ٚ‬رّىٕاذ ٘اس اٖ اٌٍ اخ ِآ ؽاً اٌؼر‪٠‬ار ِآ‬
‫اٌّػبئً اٌّؼمرح ِمبشٔزب ِغ ثصِغخ ثٍ بد اٌجصِغخ األخص‪ِ ٜ‬ضً ٌ خ ‪. FORTAN ٚ C‬‬

‫أرذ رػّ‪١‬خ ٘سٖ اٌٍ خ ِٓ اخزصبش اٌزؼج‪١‬ص ‪ِ) MATrix LABoratory‬دزجص اٌّصف‪ٛ‬فخ( ز ْ اٌجصٔبِظ ِصاُّ‬
‫أغبغب ٌٍزؼبًِ ِغ اٌؼٍّ‪١‬بد ػٍ‪ ٝ‬اٌّصف‪ٛ‬فبد ثشىً اوضاص ثػابغخ ‪ ٚ‬أػاغبَ ‪ .‬كما أرفقا بهاهِ اللغا أدوا ٌّؼبٌغاخ ‪ٚ‬ؽاً‬
‫اٌزطج‪١‬مبد ػٍّ‪١‬خ خبصخ ‪ِ ٖٛ٘ٚ‬ب‪٠‬ؼصف ثصإر‪ٚ‬ق االذ‪ٚ‬اد )‪ٚ (Toolboxes‬رؼزجاص ٘اسٖ األذ‪ٚ‬اد ٘بِاخ عارا ٌّػازدرَ ٘اسٖ‬
‫اٌٍ خ ذ ثبالظبفخ اٌ‪ ٝ‬اْ ثصٔبِظ ‪٠ MATLAB‬ارػُ ثضماخ أدوا التخ طا ِب لرواوم )‪(Graphical User Interface‬‬
‫‪ GUI‬اٌز‪ ٟ‬رغؼً اٌّػزدرَ ‪٠‬زؼبًِ ِغ اٌٍ خ اٌجصٔبِظ ‪ٚ‬وبُٔٗ اذاح ػٍّ‪١‬خ ِزط‪ٛ‬شح غج‪ ٗٙ١‬ثج‪١‬ئٗ ٌ بد ف‪١‬غ‪ٛ‬ي ‪.‬‬

‫‪MATLAB Windows‬‬ ‫نوافذ ‪MATLAB‬‬


‫تشغيل برنامج ‪MATLAB‬‬
‫‪٠‬زُ رش ‪ ً١‬ثصٔبِظ ثبؽر اٌطصق االرز‪١‬خ ‪:‬‬
‫‪ .1‬ثؼر رٕص‪١‬ت ثصٔبِظ ‪ MATLAB‬ػٍ‪ ٝ‬اٌؾبغجخ ذ غزظ‪ٙ‬ص ا‪٠‬م‪ٔٛ‬خ اٌجصٔبِظ ػٍ‪ ٝ‬غاطؼ اٌّىزات اٌؾبغاجخ ‪ ٚ‬ثبٌشاىً‬
‫‪٠ٚ‬زُ فزؾُٗ ػٕر إٌمص ػٍ‪٘ ٝ‬س اٖ اال‪٠‬م‪ٔٛ‬خ ثٕمصر‪ِ ٓ١‬طذ‪ٚ‬عز‪. double click ٓ١‬‬ ‫اال‪٠‬م‪ٔٛ‬خ‬
‫ومنها إلى برامج ‪ Programs‬ثم أسم البرنامج ‪MATLAB‬‬ ‫‪ .2‬أوعن طريق الذهاب إلى قائمة ‪start‬‬

‫وبعد فتح البرنامج ‪ ,‬عندها ستظهر لنا شاشة تحمل أسم البرنامج ‪ MATLAB‬ونسخة اإلصار و دار وسنة النشرر‬
‫كما في الشكل (‪ , )1‬ثم بعد ثوان قليلة ستظهر نافذة البرنامج الرئيسية والتي تكرون فري بدايرة تشر بل البرنرامج مول مررة‬
‫وّب ف‪ ٟ‬اٌشىً (‪. )2‬‬

‫الشكل ‪1‬‬

‫‪Lecturer: Mr. Munther R.‬‬


‫‪4‬‬
‫الشكل ‪2‬‬

‫مكونات نافذة ‪MATLAB‬‬


‫رزى‪ٔ ْٛ‬بفسح ‪ٚ MATLAB‬ؽػت اٌشىً (‪ ِٓ )3‬االعطاء االر‪١‬خ ‪:‬‬

‫شكل ‪3‬‬

‫‪ -1‬شص‪٠‬ػ اٌم‪ٛ‬ائُ )‪: (Menu Bar‬‬


‫‪٠ٚ‬جرا ثبفبئّخ اٌٍّف )‪ٕ٠ٚ (File‬ز‪ ٟٙ‬ثمبئّخ اٌّػبػرح )‪(Help‬‬

‫‪ -2‬شر يط األدوات (‪: )Tools Bar‬‬


‫‪٠ٚ‬عُ شِ‪ٛ‬ض ص‪ٛ‬ش‪٠‬خ ٌجؼط اال‪٠‬ؼبضاد اٌّ‪ٛ‬ع‪ٛ‬ذح شص‪٠‬ػ اٌم‪ٛ‬ائُ ٌ صض اٌ‪ٛ‬ص‪ٛ‬ي اٌػص‪٠‬غ اٌ‪ٙ١‬ب ‪.‬‬

‫‪ -3‬الدليل الحالي (‪: )Current Directory‬‬


‫‪ٚ‬اٌس‪ُ ٠ ٞ‬دجص اٌّػزدرَ ف‪ ٟ‬أ‪ ٞ‬عطء ِٓ اٌؾبغجخ ٘‪ِٛ ٛ‬ع‪ٛ‬ذ ؽبٌ‪١‬ب ‪ِٚ‬ىبْ اٌس‪ ٞ‬غزدطْ ف‪ٍِ ٗ١‬فبد اٌؼًّ ‪ ٚ‬إٌزبئظ‪.‬‬
‫‪Lecturer: Mr. Munther R.‬‬
‫‪5‬‬
‫‪ -4‬زر ابدء )‪: (Start‬‬
‫‪٠ٚ‬عُ اٌؼر‪٠‬ر ِٓ اٌصٕبذ‪٠‬ك اٌجصِغ‪١‬خ اٌغب٘طح ‪ ٚ‬اٌز‪٘ ٟ‬ا‪ ٟ‬ثاصاِظ عاب٘طح ِىز‪ٛ‬ثاخ ثٍ اخ ‪ِٚ MATLAB‬دط‪ٔٚ‬اخ‬
‫ظّٓ ٍِى‪١‬خ اٌجصٔبِظ ر‪ٛ‬فص ٌٍّػزدرَ ؽً ِػبئً اٌؼٍّ‪١‬خ اٌّؼمرح ‪ ٚ‬االوضص ش‪ٛ١‬ػب ف‪ ٟ‬اٌّغبالد اٌزطج‪١‬مبد اٌص‪٠‬بظ‪١‬خ ‪ٚ‬‬
‫االؽصبئ‪١‬خ ‪ ٚ‬اٌؼٍّ‪١‬خ ‪ ٚ‬غ‪١‬ص زٌه‪.‬‬
‫‪ -5‬نافذة اموامر )‪: )Command Window‬‬
‫‪ٔ ٟ٘ٚ‬بفسح اٌؼًّ االغبغ‪١‬خ ‪ٚ‬اٌز‪ِ ٟ‬آ خلٌ‪ٙ‬اب ‪٠‬ازُ اٌزؼبِاً ثبٌىزبثاخ ‪ٚ‬رٕف‪١‬اس اال‪ٚ‬اِاص ثشاىً ِجبشاص ا‪ ٚ‬غ‪١‬اص ِجبشاص‬
‫ٌٍؾص‪ٛ‬ي ػٍ‪ ٝ‬إٌزبئظ ‪.‬‬
‫‪ -6‬نافذة ساحة العمل (‪: (Workspace‬‬
‫‪ ٟ٘ٚ‬ػجابشح ػآ ٔبفاسح ردبغج‪١‬اخ رزا‪١‬ؼ ٌٍّػازدرَ ثؼاصض ‪ٚ‬رؾّ‪١‬اً ‪ٚ‬ؽفاط ِز ‪١‬اصاد ‪ MATLAB‬ذ از اْ رٍاه‬
‫اٌّز ‪١‬صاد غزظ‪ٙ‬ص ػٍ‪٘ ٝ‬سٖ إٌبفسٖ ثمبئّخ رؾ‪ ٞٛ‬اغُ اٌّز ‪١‬ص ‪ٚ‬ؽغُّٗ ‪ٚ‬ػرذ ث‪١‬بٔبرُٗ ‪ٚ‬صٕفُٗ‪.‬‬
‫‪ -7‬نافذة تسجيل اموامر (‪: (Command History‬‬
‫‪ٔ ٟ٘ٚ‬بفسٖ رم‪ َٛ‬ثؼصض وً ِب ‪٠‬م‪ َٛ‬ث اٗ اٌّػزدرَ ػٍ‪ ٝ‬اٌجصٔبِظ ‪.‬‬

‫‪The Basic Arithmetic‬‬ ‫الحساب االساسجٌ‬


‫أظص اٌغر‪ٚ‬ي (‪ )1‬اٌس‪٠ ٞ‬ج‪ ٓ١‬اٌؼٍّ‪١‬بد اٌؾػبث‪١‬خ ‪ ٚ‬شِ‪ٛ‬ض٘ب ف‪ ٟ‬ثصٔبِظ ‪: MATLAB‬‬

‫‪Symbol‬‬ ‫‪Operation‬‬ ‫‪ MATLAB Form‬الوظيفة‬


‫^‬ ‫‪Power or Exponential‬‬ ‫االظ‬ ‫^‪A‬‬
‫*‬ ‫‪Multiplication‬‬ ‫ػٍّ‪١‬خ اٌعصة‬ ‫‪A*B‬‬
‫‪/‬‬ ‫)‪Right division (slash or mrdivide‬‬ ‫ػٍّ‪١‬خ اٌمػّخ‬ ‫‪A/B=A÷B‬‬
‫\‬ ‫)‪Left division (backslash or mldivide‬‬ ‫ػٍّ‪١‬خ اٌمػّخ اٌؼىػ‪١‬خ‬ ‫‪A\B=B÷A‬‬
‫‪+‬‬ ‫‪Addition or unary plus‬‬ ‫ػٍّ‪١‬خ اٌغّغ أ‪ ٚ‬اشبشح اٌّ‪ٛ‬عجخ‬ ‫‪A+B‬‬
‫‪-‬‬ ‫‪Subtraction or negation‬‬ ‫ػٍّ‪١‬خ اٌطصػ أ‪ ٚ‬اشبشح اٌػبٌجخ‬ ‫‪A-B‬‬
‫=‬ ‫‪Equal‬‬ ‫ػٍّ‪١‬خ اٌّػب‪ٚ‬اح‬ ‫‪A=B‬‬
‫'‬ ‫)‪Transpose (real or complex‬‬ ‫اٌّؼى‪ٛ‬ظ ٌلػّرح ‪ ٚ‬اٌّصف‪ٛ‬فبد‬ ‫'‪A‬‬
‫جدول ‪1‬‬

‫أظص اٌغر‪ٚ‬ي (‪ )2‬اٌس‪٠ ٞ‬ج‪ ٓ١‬اٌّز ‪١‬صاد ‪ ٚ‬شِ‪ٛ‬ض٘ب ف‪ ٟ‬ثصٔبِظ ‪: MATLAB‬‬

‫‪Symbol‬‬ ‫المعنى‬
‫‪ans‬‬ ‫إٌز‪١‬غخ إٌ‪ٙ‬بر‪١‬خ الخص ػٍّ‪١‬خ ؽػبث‪١‬خ ا‪ٌّ ٚ‬ز ‪١‬ص غ‪١‬ص ِؼصف ِػجمب‬
‫‪pi‬‬ ‫إٌػجخ اٌضبثزخ ‪π‬‬
‫‪i, j‬‬ ‫اٌغسش اٌزد‪ 1 ٍٟ١‬‬
‫‪inf‬‬ ‫اٌمػّخ ػٍ‪ ٝ‬صفص‬
‫‪NaN‬‬ ‫ػٍّ‪١‬خ خبغئخ ‪ٚ‬غ‪١‬ص ِمج‪ٌٛ‬خ ِضً ‪0/0‬‬
‫‪theta‬‬ ‫اٌصِط ‪θ‬‬
‫‪phi‬‬ ‫اٌصِط ‪ϕ‬‬
‫‪eita‬‬ ‫اٌصِط ‪η‬‬
‫جدول ‪2‬‬

‫‪Lecturer: Mr. Munther R.‬‬


‫‪6‬‬
‫‪General Command‬‬ ‫أوامر شائعٌ‬
‫أظص اٌغر‪ٚ‬ي (‪ )3‬اٌس‪٠ ٞ‬ج‪ ٓ١‬أ‪ٚ‬اِص شبئؼخ االغزدراَ ف‪ ٟ‬ثصٔبِظ ‪: MATLAB‬‬

‫‪Symbol‬‬ ‫الوظيفة‬
‫‪%‬‬ ‫‪ٚ‬ظغ ِلؽظبد ر‪ٛ‬ظ‪١‬ؾ‪١‬خ‬
‫‪clc‬‬ ‫ٌزٕظ‪١‬ف ٔبفسح ‪ ِٓ Command Window‬اٌىزبثخ‬
‫‪clear‬‬ ‫ِػؼ اٌج‪١‬بٔبد اٌّرخٍخ ‪ ٚ‬إٌبرغخ ‪Workspace‬‬
‫;‬ ‫ػرَ أظ‪ٙ‬بش إٌزبئظ ػٍ‪ٔ ٝ‬بفسح ‪Command Window‬‬
‫‪format long‬‬ ‫اظ‪ٙ‬بش االشلبَ ثص‪ ١‬خ ِصارت ػشص‪٠‬خ اوضص‬
‫‪format short‬‬ ‫اظ‪ٙ‬بش االشلبَ ثص‪ ١‬خ ِصارت ػشص‪٠‬خ الً‬
‫‪format rat‬‬ ‫اظ‪ٙ‬بش االشلبَ ثص‪ ١‬خ وػص‪٠‬خ‬
‫‪ceil‬‬ ‫اػطبء اوجص ػرذ صؾ‪١‬ؼ ‪ٚ‬اٌزمص‪٠‬ت اٌ‪-∞ ٝ‬‬
‫‪floor‬‬ ‫اٌزمص‪٠‬ت اٌ‪+∞ ٝ‬‬
‫‪fix‬‬ ‫اٌزمص‪٠‬ت اٌ‪zero ٝ‬‬
‫‪help‬‬ ‫‪٠‬ؼصف اٌراٌخ ‪ٚٚ‬ظ‪١‬فز‪ٙ‬ب ‪ٚ‬و‪١‬ف‪١‬خ اغزدراِ‪ٙ‬ب‬
‫‪mod‬‬ ‫‪٠‬دصط ٔبرظ اٌمػّخ ٌٍؼرذ اٌصؾ‪١‬ؼ‬
‫‪rem‬‬ ‫‪٠‬دصط ثبل‪ ٟ‬اٌمػّخ‬
‫جدول ‪3‬‬

‫ِلؽظخ ‪ :‬عّ‪١‬غ ا‪ٚ‬اِص ‪ MATLAB‬رىزت ثبٌؾص‪ٚ‬ف االٔىٍ‪١‬ط‪٠‬خ اٌص ‪١‬صح ‪Small Letters‬‬

‫‪Basic Mathematical Definitions‬‬ ‫تعاريف اساسجات الرياضجات‬


‫‪٠‬زّ‪١‬ط ثصٔبِظ ‪ MATLAB‬ثبِزلوُٗ ِىزجخ ذ‪ٚ‬اي ش‪٠‬بظ‪١‬خ وج‪١‬صح اظبفخ اٌ‪ِ ٝ‬ىزجبرُٗ ف‪ ٟ‬اٌّغابالد اٌؼٍّ‪١‬اخ االخاص‪ ٜ‬ذ‬
‫‪ٚ‬ف‪٘ ٟ‬س اٖ اٌفمصح غزؼٍُ ػٍ‪ ٝ‬و‪١‬ف‪١‬خ وزبثخ اال‪ٚ‬اِص اٌص‪٠‬بظ‪١‬خ ػٍ‪ٔ ٝ‬بفسح ‪. Command Window‬‬

‫ِضبي ‪ : 1‬عر ؽغُ اٌىصح زاد لطص ‪.َ 2‬‬


‫اٌؾً ‪ٔ :‬ؾٓ ٔؼٍُ ثبْ ؽغُ اٌىصح ‪٠‬ؾػت ثأردراَ اٌّؼبذٌخ اٌص‪٠‬بظ‪١‬خ اٌزبٌ‪١‬خ‬
‫‪4‬‬
‫‪V   R3‬‬
‫‪3‬‬
‫;‪>> r = 2‬‬
‫‪>> V = (4/3)*pi*r^3‬‬
‫= ‪V‬‬
‫‪33.5103‬‬

‫ِضبي ‪ : 2‬اؽػت ول ِّب ‪٠‬أر‪ٟ‬‬


‫‪e 2 , 11 , log 3.2‬‬
‫اٌؾً ‪:‬‬
‫)‪>> exp(2‬‬
‫= ‪ans‬‬
‫‪7.3891‬‬

‫)‪>> A = sqrt(11‬‬
‫= ‪A‬‬
‫‪3.3166‬‬

‫‪Lecturer: Mr. Munther R.‬‬


‫‪7‬‬
‫)‪>> x=3.2; y=log(x‬‬
‫= ‪y‬‬
‫‪1.1632‬‬

‫‪Complex Numbers‬‬ ‫االعداد المركبٌ‬


‫أظص اٌغر‪ٚ‬ي (‪ )4‬اٌس‪٠ ٞ‬ج‪ ٓ١‬أ‪ٚ‬اِص االػراذ اٌّصوجخ ف‪ ٟ‬ثصٔبِظ ‪: MATLAB‬‬

‫‪Symbol‬‬ ‫المعنى‬
‫‪a+bi‬‬ ‫ص‪ ١‬خ اٌؼرذ اٌّصوت‬
‫‪real‬‬ ‫اٌغسش اٌؾم‪١‬م‪a ٟ‬‬
‫‪conj‬‬ ‫اٌغسش اٌزد‪b ٍٟ١‬‬
‫‪abs‬‬ ‫ٔصف لطص اٌؼرذ اٌّصوت ‪a 2  b 2‬‬
‫‪b‬‬
‫‪angle‬‬ ‫ضا‪٠ٚ‬خ اٌؼرذ اٌّصوت ‪tan 1‬‬
‫‪a‬‬
‫جدول ‪4‬‬

‫‪Lecturer: Mr. Munther R.‬‬


‫‪8‬‬
hapter 2:

C



Vectors and Matrices

Vectors and Characterizing a Vector


Vector Dot and Cross Products
Referencing Vector Components
 Basic Operations with Matrices
 More Basic Operations
 Special Matrix Types
 Referencing Matrix Elements
 Finding Determinants and Solving Linear Systems
 Finding the Inverse of a Matrix

Lecturer: Mr. Munther R.


9
Vectors and Characterizing a Vector ‫المتجوات و خصائص المتجى‬
‫ اٌجؼر ِٓ االػراذ‬ٞ‫ ػجبشٖ ػٓ ٔظُ أؽبذ‬: ٗ‫اٌّزغ‬
A vector is a one-dimensional array numbers
(square brackets) ‫ اٌؾصص‬ٟ‫اغطخ ػلِز‬ٛ‫ف ث‬ٛ‫بد صف‬ٙ‫ ِزغ‬ٚ‫بد اػّرح ا‬ٙ‫ّىٓ أشبء ِزغ‬٠ MATLAB ‫ ثصٔبِظ‬ٟ‫ف‬
: ٍٟ٠ ‫][ وّب‬

1. Column vector ‫متجه العمود‬

>> a=[2;1;4] by semicolon (;)


a =
2
1
4

>> a=[2 by enter (┘)


1
4]
a =
2
1
4

2. Row vector ‫متجه الصف‬

>> b=[3 5 2] by space )


b =
3 5 2

>> b=[3,5,2] by comma (,)


b =
3 5 2

: ٍٟ٠ ‫وّب‬ٚ ‫ ثبٌؼىع‬ٚ ‫ ِزغٗ صف‬ٌٝ‫ذ ا‬ّٛ‫ (') رػزدرَ ٌؼىع ِزغٗ اٌؼ‬
>> x=a'
x =
2 1 4
>> y=b'
y =
3
5
2

Lecturer: Mr. Munther R.


10
‫‪ٌ ‬غّغ ِزغ‪ٚ ٓ١١ٙ‬اظبفخ ‪ٚ‬ظصة ‪ٚ‬ػرذ صبثذ‬
‫;]‪>> a=[1;4;5‬‬
‫;]‪>> b=[2;3;4‬‬
‫‪>> c=a+b‬‬
‫= ‪c‬‬
‫‪3‬‬
‫‪7‬‬
‫‪9‬‬

‫‪>> e=4+c‬‬
‫= ‪e‬‬
‫‪7‬‬
‫‪11‬‬
‫‪13‬‬

‫‪>> f=3*c‬‬
‫= ‪f‬‬
‫‪9‬‬
‫‪21‬‬
‫‪27‬‬

‫‪ ‬ػٍّ‪١‬خ ذِظ اٌّزغ‪ٙ‬بد‬


‫;]‪>> a=[1;2;3‬‬
‫;]‪>> b=[4;5;6‬‬
‫]‪>> c=[a;b‬‬ ‫دمج تشكل اعمدج‬
‫= ‪c‬‬
‫‪1‬‬
‫‪2‬‬
‫‪3‬‬
‫‪4‬‬
‫‪5‬‬
‫‪6‬‬

‫]‪>> d=[a,b‬‬ ‫دمج تشكل صفىف‬


‫= ‪d‬‬

‫‪1‬‬ ‫‪4‬‬
‫‪2‬‬ ‫‪5‬‬
‫‪3‬‬ ‫‪6‬‬

‫‪Lecturer: Mr. Munther R.‬‬


‫‪11‬‬
‫])‪>> f=[a(2:3);b(1:2‬‬ ‫دمج جزءي تشكل اعمدج‬
‫= ‪f‬‬
‫‪2‬‬
‫‪3‬‬
‫‪4‬‬
‫‪5‬‬

‫])‪>> e=[a(2:3),b(1:2‬‬ ‫دمج جزءي تشكل صفىف‬


‫= ‪e‬‬
‫‪2‬‬ ‫‪4‬‬
‫‪3‬‬ ‫‪5‬‬

‫‪ ‬أشبء ِزغٗ ِٓ ِغبي ث‪ ٓ١‬ػرذ‪ٓ٠‬‬


‫]‪>> x=[0:2:10‬‬
‫= ‪x‬‬
‫‪0‬‬ ‫‪2‬‬ ‫‪4‬‬ ‫‪6‬‬ ‫‪8‬‬ ‫‪10‬‬

‫]‪>> y=[100:-5:80‬‬
‫= ‪y‬‬
‫‪100‬‬ ‫‪95‬‬ ‫‪90‬‬ ‫‪85‬‬ ‫‪80‬‬

‫‪ ‬االِص )‪٠ : (length‬ؼط‪ ٟ‬ػرذ ػٕبصص اٌّزغٗ‬


‫;]‪>> a=[3;4;7;-1;0;2‬‬
‫)‪>> length(a‬‬
‫= ‪ans‬‬
‫‪6‬‬
‫‪ ‬االِص )‪٠ : (max‬ؼط‪ ٟ‬أوجص ػرذ ف‪ ٟ‬اٌّزغٗ‬
‫)‪>> max(a‬‬
‫= ‪ans‬‬
‫‪7‬‬
‫‪ ‬االِص )‪٠ : (min‬ؼط‪ ٟ‬الً ػرذ ف‪ ٟ‬اٌّزغخ‬
‫)‪>> min(a‬‬
‫= ‪ans‬‬
‫‪-1‬‬
‫‪ ‬االِص )‪٠ : (sum‬ؼط‪ِ ٟ‬غّ‪ٛ‬ع ػٕبصص اٌّزغٗ‬
‫)‪>> sum(a‬‬
‫= ‪ans‬‬
‫‪15‬‬

‫‪Lecturer: Mr. Munther R.‬‬


‫‪12‬‬
Vector Dot and Cross Product ‫الضرب النقطً و االتجاهً للمتجى‬

ٟ‫ اٌعصة إٌمط‬.1
A  (a1 a 2 a3 ....a n )
B  (b1 b2 b3 ....bn )
n
A  B   ai * bi
i 1

ٟ٘‫ اٌعصة االرغب‬.2


i j k
A  B  a1 a2 a3
b1 b2 b3

>> a=[1;4;7]; b=[2;-1;5];


>> c=dot(a,b)
c =
33

>> d=cross(a,b)
d =
27
9
-9

>> x=a';y=b';
>> z=dot(x,y)
z =
33

>> w=cross(x,y)
w =
27 9 -9

Referencing Vector Components ‫استدعاء عناصر المتجى‬

>> a=[12;17;-2;0;4;5;11;15;27];
>> a(2) ‫استدعاء عىصز‬
ans =
17

Lecturer: Mr. Munther R.


13
‫)‪>> a(:‬‬ ‫استدعاء جمٍع العىاصز‬
‫= ‪ans‬‬
‫‪12‬‬
‫‪17‬‬
‫‪-2‬‬
‫‪0‬‬
‫‪4‬‬
‫‪5‬‬
‫‪11‬‬
‫‪15‬‬
‫‪27‬‬

‫)‪>> a(4:6‬‬ ‫استدعاء جزء مه العىاصز‬


‫= ‪ans‬‬
‫‪0‬‬
‫‪4‬‬
‫‪5‬‬

‫‪>> a(4)=9‬‬ ‫تغٍٍز قٍمح عىصز‬


‫= ‪a‬‬
‫‪12‬‬
‫‪17‬‬
‫‪-2‬‬
‫‪9‬‬
‫‪4‬‬
‫‪5‬‬
‫‪11‬‬
‫‪15‬‬
‫‪27‬‬

‫)‪>> a(9:-1:1‬‬ ‫عكس تزتٍة العىاصز‬


‫= ‪ans‬‬
‫‪27‬‬
‫‪15‬‬
‫‪11‬‬
‫‪5‬‬
‫‪4‬‬
‫‪9‬‬
‫‪-2‬‬
‫‪17‬‬
‫‪12‬‬
‫‪Lecturer: Mr. Munther R.‬‬
‫‪14‬‬
‫‪Basic Operations with Matrices‬‬ ‫العملجات االساسجٌ علٍ المصفوفات‬
‫اٌّصف‪ٛ‬فخ ‪ :‬ػجبشٖ ػٓ ٔظُ صٕبئ‪ ٟ‬اٌجؼر ِٓ االػراذ‬
‫‪A vector is a two-dimensional array numbers‬‬
‫ف‪ ٟ‬ثصٔبِظ ‪ّ٠ MATLAB‬ىٓ أشبء اٌّصف‪ٛ‬فبد ث‪ٛ‬اغطخ ػلِز‪ ٟ‬اٌؾصص ][ )‪ (square brackets‬وّب ‪: ٍٟ٠‬‬

‫‪>> a=[-1 6‬‬


‫]‪7 11‬‬
‫‪or‬‬
‫]‪>> a=[-1 6;7 11‬‬
‫‪or‬‬
‫]‪>> a=[-1,6;7,11‬‬
‫= ‪a‬‬
‫‪-1‬‬ ‫‪6‬‬
‫‪7‬‬ ‫‪11‬‬

‫‪ ‬ػٍّ‪١‬بد ػٍ‪ ٝ‬اٌّصف‪ٛ‬فخ‬


‫;]‪>> a=[-1 6;7 11‬‬
‫‪>> b=2+a‬‬ ‫عملٍح اضافح عدد ثاتت‬
‫= ‪b‬‬
‫‪1‬‬ ‫‪8‬‬
‫‪9‬‬ ‫‪13‬‬

‫‪>> c=3*a‬‬ ‫عملٍح ضزب تعدد ثاتت‬


‫= ‪c‬‬
‫‪-3‬‬ ‫‪18‬‬
‫‪21‬‬ ‫‪33‬‬

‫;]‪>> e=[2 1;5 7‬‬ ‫عملٍح جمع مصففتٍه‬


‫‪>> f=a+e‬‬
‫= ‪f‬‬

‫‪1‬‬ ‫‪7‬‬
‫‪12‬‬ ‫‪18‬‬

‫‪>> g=a-e‬‬ ‫عملٍح طزح مصففتٍه‬


‫= ‪g‬‬
‫‪-3‬‬ ‫‪5‬‬
‫‪2‬‬ ‫‪4‬‬

‫‪Lecturer: Mr. Munther R.‬‬


‫‪15‬‬
‫'‪>> h=a‬‬ ‫عملٍح عكس المصفىفح‬
‫= ‪h‬‬
‫‪-1‬‬ ‫‪7‬‬
‫‪6‬‬ ‫‪11‬‬

‫‪>> k=a.*e‬‬ ‫عملٍح ضزب عىاصز مصففتٍه‬


‫= ‪k‬‬
‫‪-2‬‬ ‫‪6‬‬
‫‪35‬‬ ‫‪77‬‬

‫‪>> m=a*e‬‬ ‫عملٍح ضزب مصففتٍه‬


‫= ‪m‬‬
‫‪28‬‬ ‫‪41‬‬
‫‪69‬‬ ‫‪84‬‬

‫‪>> n=a./e‬‬ ‫عملٍح قسمح عىاصز مصففتٍه‬


‫= ‪n‬‬
‫‪-0.5000‬‬ ‫‪6.0000‬‬
‫‪1.4000‬‬ ‫‪1.5714‬‬

‫‪>> p=a.\e‬‬ ‫عملٍح قسمحالعكسٍح لعىاصز مصففتٍه‬

‫= ‪p‬‬
‫‪-2.0000‬‬ ‫‪0.1667‬‬
‫‪0.7143‬‬ ‫‪0.6364‬‬

‫‪>> r=a.^2‬‬ ‫عملٍح رفع عىاصز المصفىفح الى قىج‬


‫= ‪r‬‬
‫‪1‬‬ ‫‪36‬‬
‫‪49‬‬ ‫‪121‬‬

‫‪>> w=a^2‬‬ ‫عملٍح ضزب المصفىفح مع وفسها‬


‫= ‪w‬‬
‫‪43‬‬ ‫‪60‬‬
‫‪70‬‬ ‫‪163‬‬

‫‪Lecturer: Mr. Munther R.‬‬


‫‪16‬‬
‫‪More Basic Operations‬‬ ‫عملجات اضافجٌ علٍ المصفوفٌ‬

‫;]‪>> a=[1 2 3;4 5 6;7 8 9‬‬


‫)‪>> b=flipud(a‬‬ ‫جعل االعلى اسفل تالىسثح للصفىف‬
‫= ‪b‬‬
‫‪7‬‬ ‫‪8‬‬ ‫‪9‬‬
‫‪4‬‬ ‫‪5‬‬ ‫‪6‬‬
‫‪1‬‬ ‫‪2‬‬ ‫‪3‬‬

‫)‪>> c=fliplr(a‬‬ ‫جعل الٍسار ٌمٍه تالىسثح لالعمدج‬


‫= ‪c‬‬
‫‪3‬‬ ‫‪2‬‬ ‫‪1‬‬
‫‪6‬‬ ‫‪5‬‬ ‫‪4‬‬
‫‪9‬‬ ‫‪8‬‬ ‫‪7‬‬

‫)‪>> d=diag(a‬‬ ‫ٌظهز عىاصز القطز الزئٍسً‬


‫= ‪d‬‬
‫‪1‬‬
‫‪5‬‬
‫‪9‬‬

‫)‪>> e=tril(a‬‬ ‫ٌجعل المصفىفح مثلثٍح سفلٍح‬


‫= ‪e‬‬
‫‪1‬‬ ‫‪0‬‬ ‫‪0‬‬
‫‪4‬‬ ‫‪5‬‬ ‫‪0‬‬
‫‪7‬‬ ‫‪8‬‬ ‫‪9‬‬

‫)‪>> f=triu(a‬‬ ‫ٌجعل المصفىفح مثلثٍح علىٌح‬


‫= ‪f‬‬
‫‪1‬‬ ‫‪2‬‬ ‫‪3‬‬
‫‪0‬‬ ‫‪5‬‬ ‫‪6‬‬
‫‪0‬‬ ‫‪0‬‬ ‫‪9‬‬

‫)‪>> g=rot90(a‬‬ ‫دوران المصفىفح تزاوٌح ‪09‬‬

‫= ‪g‬‬

‫‪3‬‬ ‫‪6‬‬ ‫‪9‬‬


‫‪2‬‬ ‫‪5‬‬ ‫‪8‬‬
‫‪1‬‬ ‫‪4‬‬ ‫‪7‬‬

‫‪Lecturer: Mr. Munther R.‬‬


‫‪17‬‬
Special Matrix Types ٌ‫انواع خاصٌ من المصفوف‬
(eye) ٓ١‫فخ اٌؼ‬ٛ‫ ِصف‬
>> eye(4)
ans =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

>> eye(3,4)
ans =
1 0 0 0
0 1 0 0
0 0 1 0

>> eye(4,3)
ans =
1 0 0
0 1 0
0 0 1
0 0 0
(zeros) ‫خ‬٠‫فخ اٌصفص‬ٛ‫ ِصف‬
>> zeros(4)
ans =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

>> zeros(3,4)
ans =
0 0 0 0
0 0 0 0
0 0 0 0
(ones) ‫خ‬٠‫فخ االؽبذ‬ٛ‫ ِصف‬
>> ones(3)
ans =
1 1 1
1 1 1
1 1 1
Lecturer: Mr. Munther R.
18
‫)‪>> ones(3,4‬‬
‫= ‪ans‬‬

‫‪1‬‬ ‫‪1‬‬ ‫‪1‬‬ ‫‪1‬‬


‫‪1‬‬ ‫‪1‬‬ ‫‪1‬‬ ‫‪1‬‬
‫‪1‬‬ ‫‪1‬‬ ‫‪1‬‬ ‫‪1‬‬

‫‪Referencing Matrix Elements‬‬ ‫استدعاء عناصر المصفوفٌ‬

‫;]‪>> a=[1 2 3;4 5 6;7 8 9‬‬


‫)‪>> a(2,3‬‬ ‫استدعاء عىصز‬
‫= ‪ans‬‬
‫‪6‬‬

‫)‪>> a(:,2‬‬ ‫استدعاء عمىد‬


‫= ‪ans‬‬
‫‪2‬‬
‫‪5‬‬
‫‪8‬‬

‫)‪>> a(2,:‬‬ ‫استدعاء صف‬


‫= ‪ans‬‬
‫‪4‬‬ ‫‪5‬‬ ‫‪6‬‬

‫)‪>> a(2:3,1:2‬‬ ‫استدعاء مصفىفح جزئٍح‬


‫= ‪ans‬‬
‫‪4‬‬ ‫‪5‬‬
‫‪7‬‬ ‫‪8‬‬

‫‪>> a(1,1)=-8‬‬ ‫تغٍٍز قٍمح العىصز‬


‫= ‪a‬‬
‫‪-8‬‬ ‫‪2‬‬ ‫‪3‬‬
‫‪4‬‬ ‫‪5‬‬ ‫‪6‬‬
‫‪7‬‬ ‫‪8‬‬ ‫‪9‬‬

‫][=)‪>> a(2,:‬‬ ‫حذف صف‬


‫= ‪a‬‬
‫‪-8‬‬ ‫‪2‬‬ ‫‪3‬‬
‫‪7‬‬ ‫‪8‬‬ ‫‪9‬‬

‫‪Lecturer: Mr. Munther R.‬‬


‫‪19‬‬
>> d=a([1,2,1],:) ‫وسخ صفىف‬
d =
-8 2 3
7 8 9
-8 2 3

>> f=a(:,[1,2,1]) ‫وسخ اعمدج‬


f =
-8 2 -8
7 8 7

Finding Determinants and Solving LinearSystems ٌ‫ايجاد محدد وحل معادالت الخطج‬

>> a=[1 3;4 5];


>> b=det(a)
b =
-7

‫ جد حل المعادمت الخطية امتية‬:‫مثال‬


5x  2 y  9z  44
9x  2 y  2z  11
6x  7 y  3z  5
ً‫اٌؾ‬
>> a=[5 2 -9;-9 -2 2;6 7 3];
>> b=[44;11;5];
>> x=a\b
x =
-3.9108
6.4760
-5.6224

Finding the Inverse of a Matrix ٌ‫ايجاد مقلوب المصفوف‬

>> c=inv(a) >> x=inv(a)*b


c = x =
-0.0458 -0.1579 -0.0320 -3.9108
0.0892 0.1579 0.1625 6.4760
-0.1167 -0.0526 0.0183 -5.62

Lecturer: Mr. Munther R.


20
C
hapter 3:
Control Flow Statements
and M-file

 Relational and logical operators


 Input and Output Statements
 Control Flow Statements
 Loops
 The for Loop
 The While Loop
 Infinite Loops
 Decisions
 The if Statement
 The Switch Statement
 M-File
 Script File
 Function File
 Multiple inputs and outputs

Lecturer: Mr. Munther R.


21
Relational and logical operators ٌ‫المعامالت العالقجٌ و المنطقج‬
: MATLAB ‫ ثصٔبِظ‬ٟ‫خ ف‬١‫ٓ اٌّؼبِلد اٌؼلل‬١‫ج‬٠ ٞ‫) اٌس‬5( ‫ي‬ٚ‫أظص اٌغر‬

operator ‫العالقة العالقية‬


== ‫اح‬ٚ‫اٌّػب‬
~= ‫اح‬ٚ‫اٌل ِػب‬
< ِٓ ‫اص ص‬
> ِٓ ‫اوجص‬
<= ٞٚ‫ػب‬٠ ٚ‫اص ص ِٓ ا‬
>= ٞٚ‫ػب‬٠ ٚ‫اوجص ِٓ ا‬
5 ‫جدول‬

: MATLAB ‫ ثصٔبِظ‬ٟ‫خ ف‬١‫ٓ اٌّؼبِلد إٌّطم‬١‫ج‬٠ ٞ‫) اٌس‬6( ‫ي‬ٚ‫أظص اٌغر‬

operator ‫العالقة المنطقية‬


& and ٚ
l or ٚ‫ا‬
~ not ‫ال‬
6 ‫جدول‬

Input and Output Statements ‫جمل االدخال و االخراج‬


input ‫ عٍّخ‬-1
user_entry = input('prompt')
user_entry = input('prompt', 's')

>> x=input('Enter the value of x=')


Enter the value of x=6
x =
6
>> x=input('Enter the value of x=','s')
Enter the value of x=ali
x =
ali

disp ‫ عٍّخ‬-2

>> disp('ali or 8')


ali or 8

>> d=8;
>> disp(d)
8

Lecturer: Mr. Munther R.


22
>> c='ali';
>> disp(c)
ali

>> sum=9.8;
>> disp(['sum=',num2str(sum)])
sum=9.8

error ‫ عٍّخ‬-3
error('errmsg')

>> error('the number is zero')


??? the number is zero

fprintf ‫ عٍّخ‬-4
fprintf('text %formatcode \controlcode',var1,.....)

formatcode ‫الصيغة الرقمية‬


%d ٟ‫م‬١‫ؽم‬
%e ٟ‫اؽصبئ‬
%f ٞ‫ػشص‬
%s ٟ‫ٔص‬
7 ‫جدول‬

controlcode ‫صيغة التحكم‬


\n ‫ر‬٠‫اٌجرء ثػطص عر‬
\t tab ‫رصن فصاؽ‬
8 ‫جدول‬

>> x=104.76;
>> fprintf('the value of x is = %5.3f \n',x)
the value of x is = 104.760

>> a=2;
>> d='ali';
>> fprintf('the number %d is = %s \n',a,d)
the number 2 is = ali

>> fprintf('the \t number %d is = %s \n',a,d)


the number 2 is = ali

Lecturer: Mr. Munther R.


23
>> x=[1 2 3 4 5 6];
>> y=[40.5 8.7 62.8 190.5 55.4 38.03];
>> z=[x;y];
>> fprintf('\t %d \t %6.3f \n',z)
1 40.500
2 8.700
3 62.800
4 190.500
5 55.400
6 38.030

Control Flow Statements ‫جمل السجطرة‬

Loops ‫ جمل التكرار‬-1

The for Loop ٌ‫ التكراري‬for ٌ‫ جمل‬-1-1


for index=star:step:finish
commands
end

>> for n=1:6


x(n)=sin(n*pi/10);
end
>> x
x =
0.3090 0.5878 0.8090 0.9511 1.0000 0.9511

>> for i=1:4


for j=1:4
a(i,j)=i^2+j^2;
end
end
>> a
a =
2 5 10 17
5 8 13 20
10 13 18 25
17 20 25 32

Lecturer: Mr. Munther R.


24
The while Loop ٌ‫ التكراري‬while ٌ‫ جمل‬-2-1
while expression
commands
end

>> i=1;
>> while i<7
disp(i)
i=i+1;
end
1
2
3
4
5
6

The infinite Loop ً‫ التكرار الالنوات‬-3-1


i=1;
while i>0
commands
i=i+1
end

Ctrl+C ٍٝ‫بء اٌزىصاش ٔع ػ ػ‬ٙٔ‫ٌل‬

>> i=1;
>> while i>0
disp(i)
i=i+1;
end
1
2
3
4
5
6
.
.
Ctrl+C

Lecturer: Mr. Munther R.


25
Decisions ‫ جمل التكرار‬-2

The if Statement ٌ‫ الشرطج‬if ٌ‫ جمل‬-1-2

if expression
commands
end

if expression
commands evaluated if True
else
commands evaluated if False
end

if expression 1
commands 1 evaluated if True
elseif expression 2
commands 2 evaluated if True
elseif expression 3
commands 3 evaluated if True
.
.
.
.
elseif expression n
commands n evaluated if True
else
commands other are False
end

>> x=5;
>> if x>4
disp('OK')
else
disp('NO')
end
OK

Lecturer: Mr. Munther R.


26
>> x=13;
>> if x<4
disp('OK')
elseif x<9
disp('4<x<9')
else
error('the x is greater than 9')
end
??? the x is greater than 9

>> x=7;
>> y=7;
>> if x>y
fprintf('x>y where x is %d and y is %d\n',x,y)
elseif x<y
fprintf('x<y where x is %d and y is %d\n',x,y)
elseif x==y&x>1
fprintf('x and y are positive numbers x=y= %d\n',x)
else
fprintf('x and y are negative numbers x=y= %d\n',x)
end
x and y are positive numbers x=y= 7

The switch Statement ٌ‫ الشرطج‬switch ٌ‫ جمل‬-2-2

switch index
case {expression 1}
commands 1 evaluated if True
case {expression 2}
commands 2 evaluated if True
case {expression 3}
commands 3 evaluated if True
.
.
.
case {expression n}
commands n evaluated if True
otherwise
commands other are False
end

Lecturer: Mr. Munther R.


27
>> x=3;
>> switch x
case {0}
m=x+3;
case {2}
m='ali'
case {3}
m=5*sqrt(x)
otherwise
disp('no case evaluated')
end

m =
8.6603

M-File M ‫الملف‬
ٍٝ‫ي ػ‬ٛ‫ؽص‬ٚ ‫ب‬ٙ‫م‬١‫اِص ِصاذ رطج‬ٚ‫ؼ ثىزبثخ اال‬١‫ز‬٠ ٞ‫اٌس‬ٚ MATLAB ‫ ثصٔبِظ‬ٟ‫ذح ف‬ٛ‫ع‬ِٛ ‫اع اٌٍّفبد‬ٛٔ‫ع ِٓ ا‬ٛٔ ٛ٘
ُ‫ِآ صا‬ٚ (.m) ‫ب رؾفع ثشىً ٍِف ثبِزراذ‬ٙ‫بء ِٓ وزبثز‬ٙ‫ػٕر االٔز‬ٚ ‫س‬١‫ْ رٕف‬ٚ‫اِص رىزت ثشىً وبًِ ثر‬ٚ‫ب ذ از اْ رٍه اال‬ٙ‫ٔزبئغ‬
.(Command Window) ‫اِص‬ٚ‫ ٔبفسح اال‬ٍٝ‫ص ٔزبئغٌٗ ػ‬ٙ‫سُٖ ٌزظ‬١‫رٕف‬

Script File ‫ ملف النص‬-1


‫ ٔزابئظ‬ٟ‫اس٘ب رؼطا‬١‫ػٕار رٕف‬ٚ ‫اب‬ٙ١‫صاد اٌّرخٍاخ ف‬١ ‫ْ ِؼصفاخ ثابٌّز‬ٛ‫خ رى‬١‫ رىزت ف‬ٟ‫اِص اٌز‬ٚ‫ ٍِف اخصاط فمػ ذ از اْ ا‬ٛ٘
. ‫ص‬١١ ‫ْ ر‬ٚ‫اؽرح فمػ ذ‬ٚ ‫ٌّصح‬

x=7;
y=7;
if x>y
fprintf('x>y where x is %d and y is %d\n',x,y)
elseif x<y
fprintf('x<y where x is %d and y is %d\n',x,y)
elseif x==y&x>1
fprintf('x and y are positive numbers x=y= %d\n',x)
else
fprintf('x and y are negative numbers x=y= %d\n',x)
end

: ٌٟ‫وبٌزب‬ٚ ‫اِص‬ٚ‫ ٔبفسح اال‬ٍٝ‫غخ ػ‬١‫ص إٌز‬ٙ‫ثؼر زٌه غزظ‬ٚ ‫س‬١‫ ضش اٌزٕف‬ٍٝ‫َ ثبٌع ػ ػ‬ٛ‫ثؼر اٌؾفع اٌٍّف ٔم‬

x and y are positive numbers x=y= 7

Lecturer: Mr. Munther R.


28
Function File ٌ‫ ملف الدال‬-2
‫اس٘ب‬١‫ػٕار رٕف‬ٚ ‫اب‬ٙ١‫صاد اٌّرخٍاخ ف‬١ ‫ص ِؼصفخ ثابٌّز‬١‫ْ غ‬ٛ‫خ رى‬١‫ رىزت ف‬ٟ‫اِص اٌز‬ٚ‫ اخصاط ِؼب ذ از اْ ا‬ٚ ‫ ٍِف اذخبي‬ٛ٘
‫لاذ‬ٚ ٞ‫ ا‬ٟ‫ب فا‬ٙ‫ّىٓ اغازرػبئ‬٠ ‫ ثسٌه اصجؾذ وراٌخ‬ٟ٘ٚ ‫ُ اٌّرخٍخ ذ‬١‫ ٔزبئظ ثبٌٕػجخ ٌزٍه اٌم‬ٟ‫صاد االذخبي ٌزؼط‬١ ‫رطٍت اٌّز‬
. ‫ب‬ّٙ‫ِٓ خلي اغ‬

function outvar = funcname( inpvar1,inpvar2,…. )


% helpcomments
commands
outvar=value
end

function z=sumxy(x,y)
% x is first number
% y is second number
% z is answer
z=x+y;
end

: y ٚ x ُ١‫اِص صُ ٔرخً ل‬ٚ‫ ٔبفسح اال‬ٍٝ‫( ػ‬sumxy) ‫َ ثىزبثخ اغُ اٌراٌخ‬ٛ‫ثؼر اٌؾفع اٌٍّف ٔم‬

>> sumxy(4,5)

ans =
9

>> help sumxy


x is first number
y is second number
Z is answer

Multiple inputs and outputs ‫ االدخال و االخراج المتعدد‬-3


: ‫خ‬١ٌ‫ خ اٌزب‬١‫زٌه ثبغزدراَ ٍِف اٌراٌخ زاد اٌص‬ٚ ‫غخ ذ‬١‫ّىٓ اخصاط اوضص ِٓ ٔز‬٠

function [outvar1,outvar2,outvar3,…]=funcname(inpvar1,inpvar2,…. )
% helpcomments
commands
outvar=value
end

Lecturer: Mr. Munther R.


29
function [a,b,c,d]=miltxy(x,y)
% x is first number
% y is second number
% a=x+y
% b=x-y
% c=x*y
% d=x/y
a=x+y;
b=x-y;
c=x*y;
d=x/y;
end

: y ٚ x ُ١‫اِص صُ ٔرخً ل‬ٚ‫ ٔبفسح اال‬ٍٝ‫( ػ‬miltxy) ‫َ ثىزبثخ اغُ اٌراٌخ‬ٛ‫ثؼر اٌؾفع اٌٍّف ٔم‬

>> [a,b,c,d]=miltxy(3,8)

a =
11

b =
-5

c =
24

d =
0.3750

>> help miltxy


x is first number
y is second number
a=x+y
b=x-y
c=x*y
d=x/y

Lecturer: Mr. Munther R.


30
C
hapter 4:
Plotting and Graphics

 Basic 2D Plotting
 2D Plotting Options
 Labels
 Adding Legends
 Line styles
 The Axis Commands
 Showing Multiple Functions on One Plot
 Subplots
 Polar and Logarithmic Plots

Lecturer: Mr. Munther R.


31
‫‪Basic 2D Plotting‬‬ ‫اساسجات الرسم البجانً ثنائً البعد‬
‫ٌصغُ ذاٌخ ػٍ‪ ٝ‬اٌّػز‪ (xy-plane) ٜٛ‬ثبغزدراَ ثصٔبِظ ‪٠ MATLAB‬زطٍت صلس خط‪ٛ‬اد وبٌزبٌ‪: ٟ‬‬
‫‪ -1‬رؾر‪٠‬ر اٌّر‪ (range) ٜ‬اٌصغُ ‪.‬‬
‫‪ -2‬رؼص‪٠‬ف اٌراٌخ ‪.‬‬
‫‪ -3‬اغزدراَ االِص )‪. (plot‬‬

‫] ‪The range [start : interval : end‬‬

‫ِضبي ‪ : 1‬ثبغزدراَ ‪ MATLAB‬اشغُ اٌراٌخ اٌزبٌ‪١‬خ‬


‫‪y  cos x‬‬ ‫‪0  x  10‬‬
‫;]‪>> x=[0:0.1:10‬‬
‫;)‪>> y=cos(x‬‬
‫)‪>> plot(x,y‬‬
‫)]‪0r fplot('sin(x)',[0, 10‬‬

‫‪2D Plotting Options‬‬ ‫خجارات الرسم البجانً الثنائً البعد‬

‫‪Labels‬‬ ‫‪ -1‬تسمجات‬
‫‪ ‬االِص )‪٠ : (xlabel‬ػزؼًّ الظبفخ رػّ‪١‬خ ٌٍّؾ‪ٛ‬ش االفم‪. ٟ‬‬
‫)'‪xlabel('horizontal‬‬
‫‪ ‬االِص )‪٠ : (ylabel‬ػزؼًّ الظبفخ رػّ‪١‬خ ٌٍّؾ‪ٛ‬ش اٌؼّ‪ٛ‬ذ‪. ٞ‬‬

‫‪Lecturer: Mr. Munther R.‬‬


‫‪32‬‬
ylabel('vertical')
. ٟٔ‫ب‬١‫خ ٌٍصغُ اٌج‬١ّ‫ػزؼًّ الظبفخ رػ‬٠ : (title) ‫ االِص‬
title(’text’)
.(x,y) ‫ٕخ‬١‫بد ِؼ‬١‫ ثبغزدراَ اؽراص‬ٟٔ‫ب‬١‫ اٌصغُ اٌج‬ٍٝ‫ظغ ػ‬ٛ‫خ ر‬١ّ‫ػزؼًّ الظبفخ رػ‬٠ : (text) ‫ االِص‬
text(x, y, ’text’)
.(mouse) ‫ ثبغزدراَ اٌفبشح‬ٟٔ‫ب‬١‫ اٌصغُ اٌج‬ٍٝ‫ظغ ػ‬ٛ‫خ ر‬١ّ‫ػزؼًّ الظبفخ رػ‬٠ : (gtext) ‫ االِص‬
gtext(’text’)

‫خ‬١ٌ‫ اشغُ اٌراٌخ اٌزب‬MATLAB َ‫ ثبغزدرا‬: 2 ‫ِضبي‬


y  e cos x
x
0x 4
>> x=[0:0.1:4];
>> y=exp(x).*cos(x);
>> plot(x,y), xlabel('x'), ylabel('y'),title('y=e^x cosx'),
text(1.5,-10,'The first function')

Lecturer: Mr. Munther R.


33
‫‪Adding Legends‬‬ ‫‪ -2‬اضافٌ عنوان تفسجري‬
‫‪ ‬االِص )‪٠ : (legend‬ػزؼًّ الظبفخ رػّ‪١‬بد ٌٍدط‪ٛ‬غ اٌج‪١‬بٔ‪١‬خ اٌّصغ‪ِٛ‬خ ‪.‬‬
‫)‪legend('function1','funthion2', …………, h‬‬

‫‪h‬‬ ‫موضع ‪legend‬‬


‫‪0‬‬ ‫افعً ِ‪ٛ‬ظغ‬
‫‪1‬‬ ‫اٌطا‪٠ٚ‬خ اٌؼٍ‪٠ٛ‬خ اٌ‪ّٕٝ١‬‬
‫‪2‬‬ ‫اٌطا‪٠ٚ‬خ اٌؼٍ‪٠ٛ‬خ اٌ‪١‬ػص‪ٜ‬‬
‫‪3‬‬ ‫اٌطا‪٠ٚ‬خ اٌػفٍ‪١‬خ اٌ‪ّٕٝ١‬‬
‫‪4‬‬ ‫اٌطا‪٠ٚ‬خ اٌػفٍ‪١‬خ اٌ‪١‬ػص‪ٜ‬‬
‫‪-1,-2,-3,-4‬‬ ‫ِ‪ٛ‬ظغ اٌ‪ ٝ‬خبشط اٌّؾب‪ٚ‬ش‬
‫جدول ‪9‬‬

‫ِضبي ‪ : 3‬ثبغزدراَ ‪ MATLAB‬اشغُ اٌراٌخ اٌزبٌ‪١‬خ‬


‫‪y  sinh x‬‬ ‫‪0x 2‬‬
‫;]‪>> x = [0:0.01:2‬‬
‫;)‪>> y = sinh(x‬‬
‫)‪>> plot(x,y), legend('sinhx',2‬‬

‫‪Lecturer: Mr. Munther R.‬‬


‫‪34‬‬
‫‪Line styles‬‬ ‫‪ -3‬انماط الخط‬
‫‪ّ٠‬ىٓ رؾر‪٠‬ر ّٔػ اٌدػ اٌصغُ ‪ ٌٗٔٛ ٚ‬وّب ‪: ٍٟ٠‬‬
‫)'‪plot(var1,var2,'colormarker‬‬
‫‪color‬‬ ‫اللون‬ ‫‪marker‬‬ ‫العالمة‬
‫‪w‬‬ ‫اث‪١‬ط‬ ‫‪.‬‬ ‫ٔمطخ‬
‫‪k‬‬ ‫اغ‪ٛ‬ذ‬ ‫‪o‬‬ ‫ذائصح‬
‫‪b‬‬ ‫اضشق‬ ‫‪x‬‬ ‫ػلِخ ‪x‬‬
‫‪r‬‬ ‫اؽّص‬ ‫‪+‬‬ ‫ػلِخ ‪+‬‬
‫‪c‬‬ ‫اٌطشق‪-‬االخعص‬ ‫*‬ ‫ٔغّخ‬
‫‪g‬‬ ‫اخعص‬ ‫‪s‬‬ ‫ِصثغ‬
‫‪m‬‬ ‫أشع‪ٛ‬أ‪ٟ‬‬ ‫‪d‬‬ ‫ِبظ‬
‫‪y‬‬ ‫اصفص‬ ‫ˇ‬ ‫ِضٍش ٌلغفً‬
‫^‬ ‫ِضٍش ٌلػٍ‪ٝ‬‬
‫<‬ ‫ِضٍش ٌٍ‪١‬ػبش‬
‫>‬ ‫ِضٍش ٌٍ‪ٓ١ّ١‬‬
‫‪p‬‬ ‫ٔغّخ خّبغ‪١‬خ‬
‫‪h‬‬ ‫ٔغّخ غراغ‪١‬خ‬
‫‪-‬‬ ‫خػ صّر‬
‫‪:‬‬ ‫خػ ِٕمػ‬
‫‪-.‬‬ ‫خػ شبشؽخ ِٕمطخ‬
‫‪--‬‬ ‫خػ شبشؽخ‬
‫جدول ‪11‬‬

‫;]‪>> x = [0:0.05:2‬‬
‫;)‪>> y = sinh(x‬‬
‫)'*‪>> plot(x,y,'r‬‬

‫‪Lecturer: Mr. Munther R.‬‬


‫‪35‬‬
The Axis Commands ‫اوامر المحور‬
. ٟٔ‫ب‬١‫ش اٌصغُ اٌج‬ٚ‫ػزؼًّ ٌزؾىُ ثّؾب‬٠ : (axis) ‫ االِص‬
axis([xmin xmax ymin ymax])
. ٟٔ‫ب‬١‫خ ٌٍصغُ اٌج‬٠ٛٔ‫ اٌضب‬ٚ ‫خ‬١‫ػ‬١‫غ اٌصئ‬ٛ‫ػزؼًّ ٌزؾىُ ثدط‬٠ : (grid) ‫ االِص‬
grid on , grid off , grid minor
>> x = [0:0.01:2];
>> y = sinh(x);
>> plot(x,y),axis([-1 3 -1 5]), grid on

Showing Multiple Functions on One Plot ‫رسم عدة دوال ضمن رسم بجانً واحد‬
: ‫خ‬١ٌ‫زٌه ثبؽر اٌطصائك اٌزب‬ٚ ‫اؽرح‬ٚ ‫خ‬١ٔ‫ب‬١‫ؽخ شغُ ث‬ٌٛ ّٓ‫خ ظ‬١‫بظ‬٠‫اي ش‬ٚ‫ِبد ٌر‬ٛ‫بش ػرح شغ‬ٙ‫ّىٓ اظ‬٠

. (hold) ‫ ثبغزدراَ االِص‬-1


hold on , hold off , hold all
. ٓ١‫( ٌراٌز‬plotyy) ‫ ثبغزدراَ االِص‬-2
plot(x1,f(x1),x2,f(x2))
. ‫اي‬ٚ‫( ٌؼرذح ِٓ ذ‬plot) ‫ ثبغزدراَ االِص‬-3
plot(x1,y1,'p1', x2,y2,'p2', x3,y3,'p3',... ..)

Lecturer: Mr. Munther R.


36
>> x=[-1:0.01:3];w=[1:0.02:4];
>> y=exp(x)-12;
>> z=4-w.^2;
>> hold on ,plot(x,y,'ro'),plot(w,z,'k*'),
legend('y=e^x-12','z=4-w^2',2)

>> x=[-1:0.01:3];w=[1:0.02:4];
>> plot(x,exp(x)-12,w,4-w.^2),legend('y=e^x-12','z=4-w^2',2)

Lecturer: Mr. Munther R.


37
‫>>‬ ‫;]‪x=[-1:0.01:3];w=[1:0.02:4‬‬
‫>>‬ ‫;‪y=exp(x)-12‬‬
‫>>‬ ‫;‪z=4-w.^2‬‬
‫>>‬ ‫)‪plot(x,y,'ro',w,z,'k*'),legend('y=e^x-12','z=4-w^2',2‬‬

‫‪Subplots‬‬ ‫الرسوم الفرعجٌ‬


‫‪٠ ‬ػزؼًّ االِص )‪ : subplot(m, n, p‬الظ‪ٙ‬بش ِصف‪ٛ‬فخ ِآ اٌصغا‪ َٛ‬اٌج‪١‬بٔ‪١‬اخ ٌؼارذ ِآ اٌار‪ٚ‬اي ذ رزعآّ واً خٍ‪١‬اٗ‬
‫ِٕ‪ٙ‬ب شغّب ِؼ‪ٕ١‬ب ‪ٚ‬وٍ‪ٙ‬ب رزصرت ظّٓ ٔبفسح اٌصغُ ذ از اْ )‪ّ٠ (m‬ضً ٌ‪ٛ‬ؽبد اٌصف‪ٛ‬ف ‪ّ٠ ٚ‬ضاً )‪ٌٛ (n‬ؽابد االػّارح‬
‫‪٠ ( p)ٚ‬ػزدرَ ِ‪ٛ‬ظغ شغُ اٌراٌخ ‪.‬‬

‫‪Lecturer: Mr. Munther R.‬‬


‫‪38‬‬
Script file
x1=[0:0.1:10];
y1=cos(x1);
subplot(2,2,1)
plot(x1,y1),xlabel('x1'),ylabel('y1'),title('y1=cos(x1)')
x2=[0:0.1:4];
y2=exp(x2).*cos(x2);
subplot(2,2,2)
plot(x2,y2),xlabel('x2'),ylabel('y2'),title('y2=exp(x2).*cos(x2)')
x3=[0:0.01:2];
y3=sinh(x3);
subplot(2,2,3)
plot(x3,y3), xlabel('x3'), ylabel('y3'),title('y3=sinh(x3)')
x4=[0:0.01:2];
y4=cosh(x4);
subplot(2,2,4)
plot(x4,y4),axis([-1 3 -1 5]), grid on

Lecturer: Mr. Munther R.


39
Polar and Logarithmic Plots ً‫الرسم القطبً و اللوغاريتم‬

‫خ‬١ٌ‫ اشغُ اٌراٌخ اٌزب‬MATLAB َ‫ ثبغزدرا‬: 4 ‫ِضبي‬


y  3 0    2
>> theta = [0:pi/90:2*pi];
>> r = 3*theta;
>> polar(theta,r,'yx'), title('Spiral')

‫خ‬١ٌ‫ اشغُ اٌراٌخ اٌزب‬MATLAB َ‫ ثبغزدرا‬: 5 ‫ِضبي‬


y  1  4*cos3 0    6
>> theta = [0:pi/90:6*pi];
>> r = 1+4*cos(3*theta);
>> polar(theta,r,'rh'), title('r = 1+4cos(3theta)')

Lecturer: Mr. Munther R.


40
ّٟ‫ز‬٠‫غبش‬ٌٍٛ‫بظ ا‬١‫خ ثبٌّم‬١ٌ‫ اشغُ اٌراٌخ اٌزب‬MATLAB َ‫ ثبغزدرا‬: 6 ‫ِضبي‬
y  e 10x 0  x  20
2

>> x = [0:0.1:20];
>> y = exp(–10*x.^2);
>>loglog(x,y,'r','LineWidth',3),grid on,title('y=exp(-10x^2)')

Lecturer: Mr. Munther R.


41
ّٟ‫ز‬٠‫غبش‬ٌٍٛ‫بظ اٌشجخ ا‬١‫خ ثبٌّم‬١ٌ‫ اشغُ اٌراٌخ اٌزب‬MATLAB َ‫ ثبغزدرا‬: 7 ‫ِضبي‬
y  e 10x 0  x  20
2

>> x = [0:0.1:20];
>> y = exp(–10*x.^2);
>> semilogy(x,y,'r','LineWidth',3),grid on

>> semilogx(x,y,'r','LineWidth',3),grid on

Lecturer: Mr. Munther R.


42
C
hapter 5:
Solving Algebraic Equations

 Solving Quadratic Equations


 Solving Higher Order Equations
 Systems of Equations
 Nonlinear equations

Lecturer: Mr. Munther R.


43
‫‪Solving Quadratic Equations‬‬ ‫حل معادالت التربجعجٌ‬
‫لجً اٌجرء ثزؼصف ػٍ‪ ٝ‬و‪١‬ف‪١‬اخ ؽاً اٌّؼابذالد ِآ اٌرشعاخ اٌضبٔ‪١‬اخ زاد اٌّز ‪١‬اص اٌ‪ٛ‬اؽار ثبغازدراَ ‪ MATLAB‬ذ الثار‬
‫ثبالشبشح اٌ‪ ٝ‬و‪١‬ف‪١‬خ اٌزؼبًِ ِغ ؽً اٌّؼبذالد ِٓ اٌرشعخ اال‪ٚ ٌٝٚ‬وبالر‪: ٟ‬‬

‫‪ ‬االِص )‪٠ : (solve‬ػزؼًّ ٌؾً اٌّؼبذالد ِٓ ا‪ ٞ‬ذشعخ وبٔذ ذ ثبالظبفخ اٌ‪ ٝ‬اغزدراِبرٗ االخص‪.ٜ‬‬
‫)'‪solve('equation','var‬‬
‫ِضبي ‪ : 1‬ثبغزدراَ ‪ MATLAB‬عر ؽً اٌّؼبذٌخ اٌزبٌ‪١‬خ اٌراٌخ اٌزبٌ‪١‬خ‬
‫‪x 3  0‬‬
‫)'‪>> solve('x-3=0') or solve('x-3') or solve('x-3','x‬‬
‫= ‪ans‬‬
‫‪3‬‬

‫)'‪>> x=solve('x-3‬‬
‫= ‪x‬‬
‫‪3‬‬

‫ِضبي ‪ : 2‬ثبغزدراَ ‪ MATLAB‬عر ؽً اٌّؼبذٌخ اٌزبٌ‪١‬خ اٌراٌخ اٌزبٌ‪١‬خ‬


‫‪ax  3  0‬‬
‫)'‪>> solve('a*x-3‬‬
‫= ‪ans‬‬
‫‪3/a‬‬
‫‪ّ٠‬ىٓ اٌؾً ثبٌٕػجخ اٌ‪ ٝ‬اٌّز ‪١‬ص ‪a‬‬
‫)'‪>> solve('a*x-3','a‬‬
‫= ‪ans‬‬
‫‪3/x‬‬

‫ِضبي ‪ : 3‬ثبغزدراَ ‪ MATLAB‬عر ؽً اٌّؼبذٌخ اٌزبٌ‪١‬خ اٌراٌخ اٌزبٌ‪١‬خ‬


‫‪x  6x  12  0‬‬
‫‪2‬‬

‫)'‪>> s = solve('x^2-6*x-12‬‬
‫= ‪s‬‬
‫)‪3 - 21^(1/2‬‬
‫‪21^(1/2) + 3‬‬
‫ٔػزدرَ االِص ‪ double‬الظ‪ٙ‬بش إٌزبئظ ثص‪ ١‬خ ػشص‪٠‬خ‬
‫)‪>> x=double(s‬‬
‫= ‪x‬‬
‫‪-1.5826‬‬

‫‪Lecturer: Mr. Munther R.‬‬


‫‪44‬‬
‫‪7.5826‬‬
‫‪Solving Higher Order Equations‬‬ ‫حل المعادالت ذات الرتبٌ العالجٌ‬
‫‪ّ٠‬ىٓ ؽً اٌّؼبذالد زاد اٌصرجخ اٌؼبٌ‪١‬خ ثبغزدراَ ٔفع االِص )‪.(solve‬‬

‫ِضبي ‪ : 4‬ثبغزدراَ ‪ MATLAB‬عر ؽً اٌّؼبذٌخ اٌزبٌ‪١‬خ اٌراٌخ اٌزبٌ‪١‬خ‬


‫‪ x  1  x  2  0‬‬
‫‪2‬‬

‫)')‪>> s = solve('(x+1)^2*(x-2‬‬
‫= ‪s‬‬
‫‪-1‬‬
‫‪-1‬‬
‫‪2‬‬

‫‪ ‬االِص )‪٠ : (roots‬ػزؼًّ ال‪٠‬غبذ عس‪ٚ‬ش ؽً اٌّؼبذالد ِزؼرذح اٌؾر‪ٚ‬ذ )‪.(polynomial‬‬


‫)]‪roots([c1 c2 c3 c4 c5 ... ...‬‬
‫ِضبي ‪ : 5‬ثبغزدراَ ‪ MATLAB‬عر ؽً اٌّؼبذٌخ اٌزبٌ‪١‬خ اٌراٌخ اٌزبٌ‪١‬خ‬
‫‪x  6x 2  72x  27  0‬‬
‫‪3‬‬

‫))'‪>> x = double(solve('x^3-6*x^2-72*x-27‬‬
‫= ‪x‬‬
‫‪12.1229 - 0.0000i‬‬
‫‪-5.7345 - 0.0000i‬‬
‫‪-0.3884 + 0.0000i‬‬

‫;]‪>> p = [1 -6 -72 -27‬‬


‫)‪>> x = roots(p‬‬
‫= ‪x‬‬
‫‪12.1229‬‬
‫‪-5.7345‬‬
‫‪-0.3884‬‬

‫ِضبي ‪ : 6‬ثبغزدراَ ‪ MATLAB‬عر ؽً اٌّؼبذٌخ اٌزبٌ‪١‬خ اٌراٌخ اٌزبٌ‪١‬خ‬


‫‪x  5x 3  4x 2  5x  6  0‬‬
‫‪4‬‬

‫)]‪>> x = roots([1 -5 4 -5 6‬‬


‫= ‪x‬‬
‫‪4.2588‬‬
‫‪-0.1876 + 1.1076i‬‬
‫‪-0.1876 - 1.1076i‬‬
‫‪1.1164‬‬

‫‪Lecturer: Mr. Munther R.‬‬


‫‪45‬‬
Systems of Equations ‫حل مجموعٌ المعادالت‬
.(solve) ‫خ ثبغزدراَ ٔفع االِص‬١‫ػخ ِؼبذالد اٌدط‬ّٛ‫ّىٓ ؽً ِغ‬٠

‫خ‬١ٌ‫خ اٌراٌخ اٌزب‬١ٌ‫ عر ؽً اٌّؼبذٌخ اٌزب‬MATLAB َ‫ ثبغزدرا‬: 7 ‫ِضبي‬


w  x  4 y  3z  5
2w  3x  y  z  1
w  2x  5 y  4z  3
w  3z  9
>> eq1 = 'w+x+4*y+3*z=5';
>> eq2 = '2*w+3*x+y-z=1';
>> eq3 = 'w+2*x-5*y+4*z=3';
>> eq4 = 'w-3*z=9';
>> [w,x,y,z]=solve(eq1,eq2,eq3,eq4);
>> double([w,x,y,z])
ans =

11.2895 -6.7895 -0.4474 0.7632

Nonlinear equations ٌ‫المعادالت الالخطج‬


. ‫خ‬١‫اؽر ٌٍّؼبذالد اٌلخط‬ٚ ‫غبذ عسش‬٠‫ػزؼًّ ال‬٠ : (fzero) ‫ االِص‬
fzero(@(x) fun(x),x0)
‫خ ٌغسش اٌّؼبذٌخ‬١‫ّخ االثزرائ‬١‫ ل‬x0 ْ‫از ا‬
‫خ‬١ٌ‫خ اٌراٌخ اٌزب‬١ٌ‫ عر ؽً اٌّؼبذٌخ اٌزب‬MATLAB َ‫ ثبغزدرا‬: 8 ‫ِضبي‬
sin x  3x 2  47  0
>> x1=fzero(@(x) sin(x)-3*x^2+47,1)
x1 =
3.9282

>> x2=fzero(@(x) sin(x)-3*x^2+47,-1)


x2 =
-3.9896

Lecturer: Mr. Munther R.


46
hapter 6:

C Derivation, Integration
and Differential Equation

 Derivation
 Integration
 Differential equation

Lecturer: Mr. Munther R.


47
Derivation ‫االشتقاق‬
. ‫صاد‬١ ‫ف اٌّز‬٠‫ػزؼًّ ٌزؼص‬٠ : (syms) ‫ االِص‬
syms x y z theta ... ...
. (n) ‫( ثصرجخ اشزمبق‬var) ٌٝ‫غبذ ِشزمخ اٌراٌخ ثبٌٕػجخ ا‬٠‫ػزؼًّ ال‬٠ : (diff) ‫ االِص‬
diff('fun',var,n) or diff(fun,var,n)
. ‫ُ ِرخٍخ‬١‫صاد اٌراٌخ ثم‬١ ‫ط ػٓ ِز‬٠ٛ‫ػزؼًّ ٌٍزؼ‬٠ : (subs) ‫ االِص‬
subs(fun,{var1,var2,... ..},{val1,val2,... ..})
‫خ‬١ٌ‫اي اٌزب‬ٚ‫ عر ِشزمخ اٌر‬MATLAB َ‫ ثبغزدرا‬: 1 ‫ِضبي‬
y x 2
find y'
g  sin10t find y' , y" & y " 4
z 2z
z  x 2  y sin x  xy 3 find &
y x 2
>> syms x t y
>> y=x^2;
>> g=sin(-10*t);
>> z=x^2+y*sin(x)+x*y^3;
>> y1=diff(y)
y1 =
2*x

>> g1=diff(g)
g1 =
(-10)*cos(10*t)

>> g2=diff(g,2)
g2 =
100*sin(10*t)

>> subs(g2,{t},{4})
ans =
74.5113

>> z1=diff(z,y)
z1 =
3*x*y^2 + sin(x)

>> z2=diff(z,x,2)
z2 =
2 - y*sin(x)

Lecturer: Mr. Munther R.


48
Integration ‫التكامل‬
. (var) ٌٝ‫غبذ رىبًِ اٌراٌخ ثبٌٕػجخ ا‬٠‫ػزؼًّ ال‬٠ : (int) ‫ االِص‬
int('fun',var) or int(fun,var)
‫ ِغ اٌزىبًِ اٌّؾرذ‬ٌٟ‫ ٔػزؼًّ االِص اٌزب‬
int(fun,var,a,b)
(Double Integration) ٟ‫ ِغ اٌزىبًِ اٌضٕبئ‬ٌٟ‫ ٔػزؼًّ االِص اٌزب‬
Int(int(fun,var1,a,b),var2,c,d)
(triple Integration) ٟ‫ ِغ اٌزىبًِ اٌضلص‬ٌٟ‫ ٔػزؼًّ االِص اٌزب‬
Int(Int(int(fun,var1,a,b),var2,c,d)),var3,e,f)

‫خ‬١ٌ‫اي اٌزب‬ٚ‫ عر رىبًِ اٌر‬MATLAB َ‫ ثبغزدرا‬: 2 ‫ِضبي‬


1
 1 e x
dx
5

 x sin xdx
1

>> syms x
>> int(1/(1+exp(x)))
ans =
x - log(exp(x) + 1)

>> s=int(x*sin(x),1,5)
s =
cos(1) - 5*cos(5) - sin(1) + sin(5)

>> double(s)
ans =

-2.67843

‫خ‬١ٌ‫ عر رىبًِ اٌراٌخ اٌزب‬MATLAB َ‫ ثبغزدرا‬: 3 ‫ِضبي‬


x  5 y  xy 3dxdy
2

x  5 y  xy 3dydx
2

>> syms x y
>> s1=int(int(x^2+5*y+x*y^3,x),y)
s1 =
(x*y*(8*x^2 + 3*x*y^3 + 60*y))/24

s2 =
(x*y*(8*x^2 + 3*x*y^3 + 60*y))/24

Lecturer: Mr. Munther R.


49
‫خ‬١ٌ‫ عر رىبًِ اٌراٌخ اٌزب‬MATLAB َ‫ ثبغزدرا‬: 4 ‫ِضبي‬
 cos

 
0
r sin  drd 

>> syms r theta


>> h=int(int(r*sin(theta),r,theta,cos(theta)),theta,0,pi)
h =
7/3 - pi^2/2

>> A=double(h)
A =
-2.6015

‫خ‬١ٌ‫ عر رىبًِ اٌراٌخ اٌزب‬MATLAB َ‫ ثبغزدرا‬: 5 ‫ِضبي‬


3 x 1 y
2 2

 
0 1 0
dzdydx

>> syms x y z
>> V=int(int(int(1,z,0,1+y^2),y,-1,x^2),x,0,3)
V =
820/7

>> double(V)
ans =
117.1429

Differential Equations ٌ‫المعادالت التفاضلج‬


. ‫خ‬١ٍ‫اي اٌزفبظ‬ٚ‫غبذ ؽً اٌر‬٠‫ػزؼًّ ال‬٠ : (dsolve) ‫ االِص‬
dsolve('eq1','eq2',...,'cond1','cond2',...,'var')

‫خ‬١ٌ‫خ اٌزب‬١ٍ‫اي اٌزفبظ‬ٚ‫ عر ؽً اٌر‬MATLAB َ‫ ثبغزدرا‬: 6 ‫ِضبي‬


2
d y dy
2
2  5sin 7x
dx dx
y '  ay
>> dsolve('D2y+2*Dy=5*sin(7*x)', 'x')
ans =
C1 - (10*cos(7*x))/371 - (5*sin(7*x))/53 + C2/exp(2*x)

>> dsolve('Dy=a*y')
ans =

C1*exp(a*t)
Lecturer: Mr. Munther R.
50
‫خ‬١ٌ‫خ اٌزب‬١ٍ‫اي اٌزفبظ‬ٚ‫ عر ؽً اٌر‬MATLAB َ‫ ثبغزدرا‬: 7 ‫ِضبي‬
dy x
 y , y (0)  2
dx x  5
d 2y
 y  0, y (0)  1 y '(0)  2
dx 2
>> dsolve('Dy=y*x/(x-5)','y(0)=2','x')
ans =
-(2*exp(x + 5*log(x - 5)))/3125

>> dsolve('D2y–y=0','y(0)= –1','Dy(0)=2','x')


ans =
exp(x)/2 - 3/(2*exp(x))

Lecturer: Mr. Munther R.


51

You might also like