Computer Programming Course code : EEN 170 Comm. Hours: 2hr theory & 2hr practical Goals of This Chapter 1. Familiarizing ourselves with the Matlab Environment 2. Learning basic operations 3. Learning to use Matlab as a simple calculator 4. Writing our first script 5. Learning to use the Help!!! Computer Structure - Hardware CPU Input devices Memory Storage devices Output devices Computer Structure - Software Software Operating system Gives services to software: Hardware Access input/output devices Memory allocations And much more… 5 From Thoughts to Program I want to add one and one Human language 1+1 mambo-jumbo 010011010011 compiler Matlab language Intermediate language Computer language Operating System 6 WHAT IS MATLAB? Matlab is a powerful computing system for handling the calculations involved in scientific and engineering problems. An interactive program that is suitable for running computations, drawing graphs and much more (graphical interface). A programming language for technical computing (scripts). The name MATLAB stands for MATrix LABoratory. THE MATLAB DESKTOP Command Window To execute commands in the MATLAB environment Command line Matlab as a calculator: Lets run our first command: 1+1 9 Command Window Previous Command line Output (answer) Current Command line The output is displayed Get the previous command by pressing up arrow keyboard Can clear window using the command clc 10 Workspace Window Displays all the defined variables Workspace Window All workspace variables Variable value 11 Command History Window Displays record of the commands used Command History Window History Try drag the command to the command window 12 The semicolon ( ; ): If a semicolon (;) is typed at the end of a command the output of the command is not displayed. Typing %: When the symbol % (percent) is typed at the beginning of a line, the line is designated as a comment. This means that when the Enter key is pressed the line is not executed. The clc command: The clc command (type clc and press Enter) clears the Command Window. Clear Removes all variables from the memory. clear x y z Removes only variables x, y, and z from the memory. who Displays a list of the variables currently in the memory. Whos Displays a list of the variables currently in the memory and their sizes together with information about their bytes and class ARITHMETIC OPERATIONS WITH SCALARS The symbols of arithmetic operations are: Operation Addition Symbol + Example 5+3 Subtraction – 5–3 Multiplication * 5 *3 Right division / 5/3 Left division \ 5\3 =3/5 Exponentiation ^ 5 ^ 3 ( means 53 = 125) Order of Precedence MATLAB executes the calculations according to the order of precedence displayed below. Precedence Mathematical Operation First Parentheses. For nested parentheses, the innermost are executed first. Second Exponentiation. Third Multiplication, division (equal precedence). Fourth Addition and subtraction. If two or more operations have the same precedence, the expression is executed from left to right. Lets Try Matlab as a Calculator I want to make a fruit salad for 10 people. Ingredients for 5 people: 6 oranges 5 bananas 3 apples 1 melon What is the total num of fruits needed? How much will it cost to make the salad? Answer: Price list: orange 1.5 banana 1.2 apple 1 melon 5 17 DISPLAY FORMATS The user can control the format in which MATLAB displays output on the screen ELEMENTARY MATH BUILT-IN FUNCTIONS MATLAB has a very large library of built-in functions. A function has a name and an argument in parentheses Some commonly used elementary MATLAB mathematical built-in functions Trigonometric math functions Rounding functions The Assignment Operator In MATLAB the = sign is called the assignment operator. The assignment operator assigns a value to a variable. Variable_name = A numerical value, or a computable expression Rules About Variable Names A variable can be named according to the following rules: • Must begin with a letter. • Can be up to 63 characters long. • Can contain letters, digits, and the underscore character. • Cannot contain punctuation characters (e.g., period, comma, semicolon). • MATLAB is case sensitive: it distinguishes between uppercase and lowercase letters. For example, AA, Aa, aA, and aa are the names of four different variables. • No spaces are allowed between characters (use the underscore where a space is desired). • Avoid using the name of a built-in function for a variable (i.e., avoid using cos, sin, exp, sqrt, etc.). Once a function name is used to define a variable, the function cannot be used. Variable Assignment max_grade = 100; Variable identifier (name): • Letters • Digits • Underscore 1. Can’t start with a number 2. Case sensitive!!! 3. Can’t be a keyword Value Assignment operator Keywords and Predefined Variables There are 20 words, called keywords, that are reserved by MATLAB for various purposes and cannot be used as variable names. These words are: break Elseif otherwise try Keywords pi Eps Inf i ,j NaN case end parfor while catch classdef for function persistent return continue global spmd else if switch The number π. The smallest difference between two numbers. Equal to 2^(–52), Used for infinity. Defined as , which is: 0 + 1.0000i. Stands for Not-a-Number. Used when MATLAB cannot determine a numeric value. Example: 0/0. valid