Arithmetic Operations

Slides:



Advertisements
Similar presentations
1 Chapter 2 Basic Elements of Fortran Programming.
Advertisements

Introduction to MATLAB 7 for Engineers
Introduction to Matlab. Entering Commands Constants and Functions >> pi ans = >> eps ans = e-016 >> sin(pi/2) ans = 1 >> log(1000) ans =
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Recap Graphic Window Edit Window Start Button Matrices in MATLAB Scalar Operations Order of Operations Array Operations Matrix Addition Matrix Multiplication.
IMS 3253: Math 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Five Fundamental Math Operations Precedence of Math.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.
Introduction to MATLAB ENGR 1181 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
MATLAB An Introduction to MATLAB (Matrix Laboratory) 1.
Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the.
Representing numbers and Basic MATLAB 1. Representing numbers  numbers used by computers do not behave the same as numbers used in mathematics  e.g.,
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 1.
Input, Output, and Processing
CMPS 1371 Introduction to Computing for Engineers MatLab.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
MATLAB Technical Computing Environment. MATLAB Matlab is an interactive computing environment that enables numerical computation and data visualization.
1 Week 1: Variables, assignment, expressions READING: 1.2 – 1.4.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
Matlab for Engineers Matlab Environment Chapter 2.
Basic operations in Matlab EE 201.  Achieve Comprehension LOL of Matlab basic operations. Class Learning Objectives 2.
Bill Tucker Austin Community College COSC 1315
Variables, Operators, and Expressions
Simple C Programs.
Today Variable declaration Mathematical Operators Input and Output Lab
Introduction to MATLAB
ECE 1304 Introduction to Electrical and Computer Engineering
Topics Designing a Program Input, Processing, and Output
Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology.
Numeric, Cell and Structural Arrays One of the strenghts of MATLAB is the capabilty to handle collection of numbers called ARRAYS. MATLAB refers to scalars,
ECE 1304 Introduction to Electrical and Computer Engineering
Built-in MATLAB Functions Chapter 3
BASIC ELEMENTS OF A COMPUTER PROGRAM
BIL 104E Introduction to Scientific and Engineering Computing
TMF1414 Introduction to Programming
Overview: Programming Concepts
Variables, Expressions, and IO
Computing with C# and the .NET Framework
Fundamental of Java Programming Basics of Java Programming
Introduction to Programming
MATLAB DENC 2533 ECADD LAB 9.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Place Value, Names for Numbers, and Reading Tables
Numbers.
Introduction to MATLAB
CSI 101 Elements of Computing Spring 2009
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Introduction to Matlab
Chapter 3: Selection Structures: Making Decisions
Topics Designing a Program Input, Processing, and Output
Experiment No. (1) - an introduction to MATLAB
Summary of what we learned yesterday
Topics Designing a Program Input, Processing, and Output
MATLAB Introduction MATLAB can be thought of as a powerful graphing calculator but with a lot more buttons! It is also a programming language, commands.
Introduction to MATLAB
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
月夜憶舍弟 戍鼓斷人行,邊秋一雁聲。 露從今夜白,月是故鄉明。 有弟皆分散,無家問死生。 寄書長不達,況乃未休兵。 杜甫
Variables in C Topics Naming Variables Declaring Variables
Introduction to MATLAB
MatLab Program Used to Calculate Interactive
Electrical and Computer Engineering Department SUNY – New Paltz
Presentation transcript:

Arithmetic Operations In this lesson we will only discuss arithmetic with scalars (single numbers). We can do arithmetic directly on numbers (like a calculator). Or, we can store numbers in variables (containers).

5 ^ 3 (means 5 to the power of 3 = 125) Arithmetic Operators Symbols for arithmetic operations are: Note: Left division is rarely used with scalars. Operation Symbol Example Addition + 5 + 3 Subtraction – 5 – 3 Multiplication * 5 * 3 Right division / 5 / 3 Left division \ 5 \ 3 = 3 / 5 Exponentiation ^ 5 ^ 3 (means 5 to the power of 3 = 125)

Arithmetic Operations Precedence Order 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

Arithmetic Operations Precedence Rules The MATLAB precedence order is The same as most calculators. The same as doing arithmetic by hand. For multiple operations of same precedence, MATLAB goes left to right. You can change order by using parentheses.

Using MATLAB as a Calculator Type in a mathematical expression. Press the Enter key. MATLAB displays the answer in the command window as ans = followed by the result. Example: >> 2 * x - 2 ans = 6 Your display may appear on more than one line and have blank lines between the text.

Formatting the Display You can control the display of numbers with the format command. Once a command is entered, the format stays the same until another format command is issued. The default format is fixed point with four digits to the right of the decimal point. fixed-point means decimal point always between one’s-digit and one-tenth’s digit. The format command only affects the display of numbers. MATLAB always computes and saves numbers in full precision.

Style Result Example short (default) Short fixed decimal format, with 4 digits after the decimal point. 3.1416 long Long fixed decimal format, with 15 digits after the decimal point for double values, and 7 digits after the decimal point for single values. 3.141592653589793 shortG The more compact of short fixed decimal or scientific notation. If you are displaying a matrix with a wide range of values, consider using shortG. bank Currency format, with 2 digits after the decimal point. 3.14

Command Description Example format short Fixed-point with 4 decimal digits for: 0.001 ≤ number ≤ 1000 Otherwise display format short e >> 290/7 ans = 41.4286 format long Fixed-point with 15 decimal digits for: Otherwise display format long e 41.428571428571431 format short e Scientific notation with 4 decimal digits 4.1429e+001 format long e Scientific notation with 15 decimal digits 4.142857142857143e+001 format compact Eliminates empty lines to allow more lines with information displayed on the screen format loose Adds empty lines (opposite of compact)

MATLAB Built-In Functions MATLAB expressions can include functions. You can think of a function as a black box that, in general, takes inputs, does some computations with them, and produces outputs. function Inputs Outputs function y tan(y/x) x

MATLAB Built-In Functions A function Has a name. Can have zero or more arguments (inputs). Can produce zero or more outputs. y = sqrt(x) name argument output

MATLAB Built-In Functions A function’s arguments can be numbers, or variables (explained in the next section), or expressions involving numbers, variables, or functions. sqrt(64) argument is a number sqrt(a) argument is the variable a atan( y/sqrt(3^2+y^2) ) argument is an expression that has a number (3), a variable (y), and a function (sqrt)

Elementary Math Functions sqrt(x) – square root nthroot(x,n) – nth real root exp(x) – e to the power of x abs(x) – absolute value log(x) – natural log (base e) log10(x) – log base 10 factorial(x) – x! See Table 1-3 of text book for details.

Elementary Trig Functions sin(x) – sine (x in radians) sind(x) – sine (x in degrees) cos(x) – cosine (x in radians) cosd(x) – cosine (x in degrees) tan(x) – tangent (x in radians) tand(x) – tangent (x in degrees) cot(x) – cotangent (x in radians) cotd(x)- cotangent (x in degrees) See Table 1-4 for of text book for details.

Rounding Functions round(x) – round to nearest integer fix(x) – round toward zero (transforms a double into an integer) ceil(x) – round toward infinity floor(x) – round toward minus infinity rem(x,y) – remainder after x is divided by y (also called modulus) sign(x) – returns 1 if x is positive, -1 if x is negative, zero if x is zero See Table 1-5 for of text book for details.

Functions Examples round(A) – Positive elements with a fractional part of 0.5 round up to the nearest positive integer. Negative elements with a fractional part of -0.5 round down to the nearest negative integer. a = -1.9 round(a) a= -2.0000 0 , a=-0.2 round(a) a=0, a=3.4 round(a) a= 3.0000, a=5.6 round(a) a=6.0000 fix(A) – rounds the elements of A toward zero, resulting in an array of integers a = -1.9 fix(a) a= -1.0000 , a=-0.2 fix(a) a=0, a=3.4 fix(a) a= 3.0000, a=5.6 fix(a) a=5.0000 ceil(A) – rounds the elements of A to the nearest integers greater than or equal to A. a = -1.9 ceil(a) ans = -1.0000 , a = -0.2000 ceil(a) ans =0 , a = 3.4000 ceil(a) ans = 4.0000 floor(A) – rounds the elements of A to the nearest integers less than or equal to A A = -1.9 floor(A) ans = -2.0000, A = -0.2 floor(A) ans = -1.0000, A = 3.4 floor(A) ans = 3.0000 rem(X,Y) – Remainder after division of X by Y. X = 23; Y = 5; R = rem(X,Y) R=3, X = 1:5; Y = 3; R = rem(X,Y) R= 1 2 0 1 2 X = [1 2 3;4 5 6;7 8 9]; Y = [9 8 7;6 5 4;3 2 1]; R = rem(X,Y) R = 1 2 3 4 0 2 1 0 0

Variables MATLAB allows you to assign a value to a variable A=5 Should be read as A is assigned a value of 5 Use the variables in subsequent calculations

Defining Scalar Variables A variable is a name that is assigned a numerical value Once assigned, can use variable in expressions, functions, and MATLAB statements and commands. Can read the variable (get its value). Can write to the variable (set its value).

Assigning Values to Variables = (equals sign) is MATLAB’s assignment operator. It evaluates the expression on its right side and stores the resulting value in the variable on its left side Create the variable called a and store the value 3 in it >> a = 3 MATLAB acknowledges that it has created a and set it to 3 a = 3

Assignment Operator Example: >> a = 3 a = 3 >> b = 10 * a + 5 b = 35 Create a variable and store a number in it Create a variable and store the value of an expression made up of a variable, numbers, and addition and multiplication

How Assignment Works Think of = as meaning “assign to” or “store in” but not meaning “equals”! Why? x = x + 6 has no meaning in math because it implies that 0 = 6. x = x + 6 is perfectly fine in MATLAB because it means “take whatever is in x (the x on the right side), add 6 to that and store the result back into x (the x on the left side which really is the same container (variable) named x)”.

Assignment Operator Examples >> x = x + 6 x = 9 >> x = 2 * x 18 ; at end prevents MATLAB from displaying value of x. takes what’s in x (3), adds 6 to it to get 9, then stores 9 back into x. now x’s value is 9 takes what’s in x (9), multiplies it by 2 to get 18, then stores 18 back into x now x’s value is 18

Assignment Operator Warning A variable must have a value before you use it in an expression. >> x = 3; >> x + 2 ans = 5 >> x + y % assume y is undefined ??? Undefined function or variable 'y' comment

The Value of a Variable >> x = 3; >> y = 10 * x; >> z = y ^ 2; >> y y = 30 >> z z = 900 To find out the value of a variable, just type it and press ENTER.

Multiple Assignments >> a=12, B=4; C=(a-B)+40-a/B*10 a = 12 C = You can do multiple assignments on one line by separating them with a comma or semicolon. If semicolon, no display for that assignment. >> a=12, B=4; C=(a-B)+40-a/B*10 a = 12 C = 18

Changing the Value of a Variable To change the value of a variable, just assign it the new value. >> ABB=72; >> ABB=9; >> ABB ABB = 9 Only the last value is kept! One value at a time in a scalar variable. The original value of 72 is gone!

Variables and Functions You must define a variable (give it a value) before you can use it in an argument of a function. >> sqrt( x ) % assume x undefined ??? Undefined function or variable 'x' >> x = 144; >> sqrt( x ) x = 12

Naming Variables All names must start with a letter They may contain letters, numbers and the underscore ( _ ) Names are case sensitive There are certain keywords you can’t use

Variable Name Rules Must begin with a letter. Can be up to 63 characters long. Can contain letters, digits, and underscores (_). Can’t contain punctuation, e.g., period, comma, semicolon. Avoid using the name of a built-in function as the name of a variable, e.g., don’t call a variable exp or sqrt.

Variable Name Rules MATLAB is case-sensitive, and does not consider an upper-case letter in a variable name to be the same as its lower-case counterpart, MTV, MTv, mTV, and mtv are four different variable names!

Variable Name Rules A variable name cannot contain a space. Two common alternatives: Use an underscore in place of a space, ex: speed_of_light. Capitalize the first letter of every word, ex: SpeedOfLight.

Variable Name Rules MATLAB Keywords A keyword or reserved word is a word that has special meaning to MATLAB. There are 20 keywords: break, case, catch, classdef, continue, else, elseif, end, for, function, global, if, otherwise, parfor, persistent, return, spmd, switch, try, while They appear in blue when typed in the editor/debugger window (type iskeyword to see the list). They can’t be used as variable names.

Use the iskeyword function for a list of keywords ans = 'break' 'case' 'catch' 'classdef' 'continue' 'else' 'elseif' 'end‘ 'for‘ 'function' 'global' 'if' 'otherwise' 'parfor' 'persistent' 'return‘ ‘spmd’ 'switch' 'try' 'while' Keywords are not acceptable variable names

You can reassign function names MATLAB will let you use built-in function names as variables – but it’s a really bad idea sin = 3 changes sin from a function to a variable name clear sin resets sin back to a function

Which of these names are allowed in MATLAB? test Test if my-book my_book Thisisoneverylongnamebutisitstillallowed? 1stgroup group_one zzaAbc z34wAwy?12# sin log

Which of these names are allowed in MATLAB? test Test if my-book my_book Thisisoneverylongnamebutisitstillallowed? 1stgroup group_one zzaAbc z34wAwy?12# sin log x x x x x x bad idea