VARIABLES Storing numbers:

Similar documents
Variables are used to store data (numbers, letters, etc) in MATLAB. There are a few rules that must be followed when creating variables in MATLAB:

Dr Richard Greenaway

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah)

Basic stuff -- assignments, arithmetic and functions

AMS 27L LAB #1 Winter 2009

Introduction to MATLAB

Introduction to Engineering gii

SIMPLE INPUT and OUTPUT:

EGR 111 Introduction to MATLAB

Section 1: Numerical Calculations

Chapter 1 Introduction to MATLAB

Computer Programming in MATLAB

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

MATLAB The first steps. Edited by Péter Vass

the Enter or Return key. To perform a simple computations type a command and next press the

Edward Neuman Department of Mathematics Southern Illinois University at Carbondale

Matlab Programming Introduction 1 2

Digital Image Analysis and Processing CPE

General Information. There are certain MATLAB features you should be aware of before you begin working with MATLAB.

Matlab as a calculator

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

Getting Started. Chapter 1. How to Get Matlab. 1.1 Before We Begin Matlab to Accompany Lay s Linear Algebra Text

LAB 1 General MATLAB Information 1

Starting with a great calculator... Variables. Comments. Topic 5: Introduction to Programming in Matlab CSSE, UWA

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

You just told Matlab to create two strings of letters 'I have no idea what I m doing' and to name those strings str1 and str2.

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras

Introduction to MATLAB

ELEMENTARY MATLAB PROGRAMMING

Laboratory 1 Octave Tutorial

Math 2250 MATLAB TUTORIAL Fall 2005

AMS 27L LAB #2 Winter 2009

Introduction to MATLAB

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY

Introduction to Matlab to Accompany Linear Algebra. Douglas Hundley Department of Mathematics and Statistics Whitman College

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

2 Making Decisions. Store the value 3 in memory location y

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Lecture 2 FORTRAN Basics. Lubna Ahmed

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Introduction to Matlab

Introduction to MATLAB

Starting MATLAB To logon onto a Temple workstation at the Tech Center, follow the directions below.

University of Alberta

FORTRAN Basis. PROGRAM LAYOUT PROGRAM program name IMPLICIT NONE [declaration statements] [executable statements] END PROGRAM [program name]

Introduction to Matlab

Julia Calculator ( Introduction)

General MATLAB Information 1

MATLAB/Octave Tutorial

ME 121 MATLAB Lesson 01 Introduction to MATLAB

MATLAB QUICK START TUTORIAL

CITS2401 Computer Analysis & Visualisation

Step by step set of instructions to accomplish a task or solve a problem

Chapter 2. Outline. Simple C++ Programs

Exercise: Inventing Language

To start using Matlab, you only need be concerned with the command window for now.

Lecture 1: What is MATLAB?

Our Strategy for Learning Fortran 90

Introduction to MATLAB Programming

Introduction to MATLAB

Matlab Introduction. Scalar Variables and Arithmetic Operators

McTutorial: A MATLAB Tutorial

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP)

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

Programming in MATLAB

Finding, Starting and Using Matlab

Introduction to MATLAB. Computational Probability and Statistics CIS 2033 Section 003

1 Week 1: Basics of scientific programming I

EP375 Computational Physics

ME1107 Computing Y Yan.

Introduction to Matlab

Variable Definition and Statement Suppression You can create your own variables, and assign them values using = >> a = a = 3.

Script started on Thu 25 Aug :00:40 PM CDT

Matlab Tutorial. Get familiar with MATLAB by using tutorials and demos found in MATLAB. You can click Start MATLAB Demos to start the help screen.

MATLAB TUTORIAL WORKSHEET

Math 144 Activity #4 Connecting the unit circle to the graphs of the trig functions

A very short introduction to Matlab and Octave

A Guide to Using Some Basic MATLAB Functions

Lab 1 Intro to MATLAB and FreeMat

AN INTRODUCTION TO MATLAB

9/4/2018. Chapter 2 (Part 1) MATLAB Basics. Arrays. Arrays 2. Arrays 3. Variables 2. Variables

PART 1 PROGRAMMING WITH MATHLAB

Getting to Know Maple

1 Introduction to MATLAB

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

Experiment 1: Introduction to MATLAB I. Introduction. 1.1 Objectives and Expectations: 1.2 What is MATLAB?

Lecturer: Keyvan Dehmamy

Matlab- Command Window Operations, Scalars and Arrays

Part #1. A0B17MTB Matlab. Miloslav Čapek Filip Kozák, Viktor Adler, Pavel Valtr

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline

Introduction to MATLAB

Introduction to MATLAB

Introduction to MATLAB

CT 229 Java Syntax Continued

12 whereas if I terminate the expression with a semicolon, the printed output is suppressed.

Accelerating Information Technology Innovation

MATLAB Lesson I. Chiara Lelli. October 2, Politecnico di Milano

Transcription:

VARIABLES Storing numbers: You may create and use variables in Matlab to store data. There are a few rules on naming variables though: (1) Variables must begin with a letter and can be followed with any combination of letters, numbers, or underscores only (2) Variable names must not exceed 63 characters in length (3) Variables are case sensitive. The variable Apple is not the same as the variable apple Examples of acceptable variables: a, b, c, apple, Apple, ApPLE89_3, ILIKECHEESE Examples of unacceptable variables: 89, 8fish, Fis h, pizza! You should choose variable names that are descriptive. For example, use the variable numapple is a variable that stores information about the number of apples. You can check if a variable is acceptable with the isvarname command. This is just one of many built in commands in Matlab/Octave. > isvarname pizza ans = 1 > isvarname pizza! ans = 0 (Note: 0 = no, 1 = yes) Matlab/Octave, unlike other languages such as Fortran, we do not need to declare the data type of variables (INTEGER, REAL, ETC.). All variables in Matlab/Octave are assumed to store floating point data unless otherwise stated (there will be times where we will want to force a variable to be an integer). In fact, you do not even need to declare variables. They are created when you assign them a value. The equals sign (=) means assign the quantity on the right to the variable on the left not equals. > b = 2 b = 2 The value on the right side is assigned to the variable b. Think of b as a box that can hold numbers. You can use b to perform computations. > b + 5 ans = 7 > b * 5 ans = 10 You can use the value of b to calculate the value of another variable, c. > b 1

b = 2 > c = b + 5 c = 7 (2 + 5 is evaluated, and the resulting answer is assigned to c) You can use the value of multiple variables to get the value of another variable, cow. > cow = b * c (2 * 7 is evaluated, and the results answer is assigned to cow) cow = 14 I emphasize that the right side of the equals sign is evaluated first, then the result is assigned to the variable on the left side. > cow = cow + 1 cow = 15 This equation is wrong algebraically, but computationally it is fine. The right side (14+1) is evaluated first (14+1 is 15), then assigned to cow. You can check out what variables have been created with the who command. > who Variables in the current scope: ans b c cow You can get more detailed information about the variables with the whos command. This shows the variables in your workspace. > whos Variables in the current scope: Attr Name Size Bytes Class ==== ==== ==== ===== ===== ans 1x1 8 double b 1x1 8 double c 1x1 8 double cow 1x1 8 double There is a lot of information here. I will explain what it all means as the course goes on. The concept of variable scope will become important when we learn about functions. For now, just notice how all the variables are considered 1x1 arrays that can store double precision data (that is, all variables contain floating-point numbers). You can suppress the output with a semi-colon. > 2 * c ans = 14 2

> 2 * c; > 2 * c ans = 14 What if you try to use a variable that hasn t been used yet? > z = y * 2 (y hasn t been used yet) error: `y' undefined near line 19 column 5 > y = 9 y = 9 > z = y * 2 z = 18 Matlab/Octave is case sensitive. > t = 6.6 t = 6.6 > z = T * 2 error: `T' undefined near line 19 column 5 > z = t * 2 z = 13.200 Variables will exist until you exit Octave or erase them with the clear command. > clear > whos (nothing appears since all the variables are gone) If your screen gets too messy, you can use the clc command. Limits on numerical data: There is a limit to the size of the numbers you can store in memory. Largest double-precision floating-point number: 1.7977e+308 Smallest double-precision floating-point number: 2.2251e-308 Largest integer: 2147483647 Smallest integer: -2147483648 Sometimes you run into problems when you do mathematical operations with very large and very small numbers > x = 1e200 x = 1.0000e+200 > y = 1e-200 3

y = 1.0000e-200 > z = x/y z = Inf > z = y/x z = 0 > z = (y/x)*x z = 0 (the true answer is 1.0000e+400) (the true answer is 1.0000e-400) (The true answer is 1.0000e-200. Remember statements are evaluated left to right) VARIABLES Characters: You can store character data in variables too. Make sure to put single quotes around the text. > a = banana a = banana (the variable a stores the word banana ) > b = chimp b = chimp (the variable b stores the word chimp ) > c = chimp error: `c' undefined near line 29 column 5 (Whoops, there is no variable called chimp.) > d = 5.6 d = 5.6 > b = 44 (variables can change the type of data stored in them) b = 44 > whos Variables in the current scope: Attr Name Size Bytes Class ==== ==== ==== ===== ===== a 1x6 6 char b 1x2 2 char d 1x1 8 double Notice that b and d are character variables. The arrays are bigger than 1x1. Each letter is stored in a separate space in memory. We will discuss this shortly. VARIABLES Logical: We will learn about this type of variable later. 4

BUILT-IN VARIABLES AND FUNCTIONS: Matlab/Octave contains a large library of commonly used variables and functions. The variable pi is reserved for π (3.141592654 ), i and j are reserved for imaginary numbers, and so on. > pi ans = 3.1416 (many more digits are stored, but only 4 digits right of the decimal are displayed) > pi*3 ans = 9.4248 > degrees = 90 degrees = 90 > radians = degrees * (pi/180) radians = 1.5708 (this is π/2) There are many different types of functions. Almost all functions are given one or more arguments. For example, the trigonometric function sin(x) has one argument, x, which must store number data. The arguments of trigonometric functions are in radians. > sin(0.) ans = 0 > cos(0.) ans = 1 > sin(90.) ans = 0.89400 (remember that arguments of trigonometric functions must be in radians) > sin(3.141592654) ans = -4.1021e-010 (very close to 0) > sin(pi/2) ans = 1 Some other functions: sqrt(x) calculates the square root of x exp(x) calculates e x abs(x) calculates x See Chapter 3 for a longer list of functions. Matlab/Octave has many functions that allow you to do common tasks with a single command. Other languages, such as Fortran and C, may not have these built-in functions and are less userfriendly. For example, in Matlab/Octave mean(x) finds the average value of 1 or more numbers stored in x. However, in this class we will not be using many of these functions because I want you to understand the thinking behind the functions. 5

Using function names as variable names: You may use function names as names for variables. In general this is a bad idea because it may confuse the user. > pi ans = 3.1416 > 3*pi ans = 9.4248 > pi = 7 pi = 7 > 3*pi ans = 21 > sin = 4 sin = 4 > sin(4) error: A(I): Index exceeds matrix dimension. There are certain keywords that are not allowed to be used for variables. Use the command iskeyword to see the keywords GETTING HELP: Matlab/Octave have a lot of documentation built-in that can help you if you need more information. For example, if you need help with the sin( ) function, > help sin `sin' is a built-in function -- Mapping Function: sin (X) Compute the sine for each element of X in radians. See also: asin, sind, sinh There is a TON of information and tutorials online too. Google is your friend. 6

CREATING M-FILES: So far we have only been executing simple commands at the command line. What if you want to make a small change to a code that has a lot of commands without having to type those commands over again every time you run your code? You can create an M-File a simple text file that ends with a.m extension. You can make simple text files using text editors like Notepad and Wordpad, or at the command line using the edit command. > edit filename.m You should see a text editor on the screen (sometimes there will be a lot of extra text). Erase any extra text and start typing your commands. For example, an M-File file might look like this: x = 5; y = 7 c = x*y Save your program as filename.m (where filename could be anything you wish). To run your M-File, type the filename minus the.m extension at the command line. > filename (notice there is not.m extension) y = 7 (x = 5 is suppressed due to the use of a semi-colon) c = 35 You may open and edit your M-File later if needed. It is a good idea to use the clear command at the beginning of your M-File. This will remove all variables from memory. clear; COMMENTARY: The percent symbol (%) is used to create commentary. There are two reasons you should heavily comment your code: (1) Another user may want to use your code and will need to understand what is going on; (2) When your code becomes long, you may need to remember exactly what your code is doing. Anything after the % will be ignored by Matlab/Octave. In mfile.m: % hello (nothing happens) % x = 5 (same) 7

y = 7 z = 99 % hello hi = 12%3 At command line: > mfile y = 7 z = 99 hi = 12 The variable x is never assigned a value, the text hello is ignored by the program, and the 3 is cut off of 123 due to the percent sign. 8