Matlab- Command Window Operations, Scalars and Arrays

Similar documents
ME313 Homework #5. Matlab- Functions

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

7 Control Structures, Logical Statements

ENGR 1181 MATLAB 02: Array Creation

Matlab notes Matlab is a matrix-based, high-performance language for technical computing It integrates computation, visualisation and programming usin

Array Creation ENGR 1181 MATLAB 2

Physics 326G Winter Class 2. In this class you will learn how to define and work with arrays or vectors.

EGR 102 Introduction to Engineering Modeling. Lab 05A Managing Data

EGR 111 Introduction to MATLAB

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

MATLAB = MATrix LABoratory. Interactive system. Basic data element is an array that does not require dimensioning.

RELATIONAL AND LOGICAL OPERATORS

FreeMat Tutorial. 3x + 4y 2z = 5 2x 5y + z = 8 x x + 3y = -1 xx

Introduction to MATLAB

Matlab Introduction. Scalar Variables and Arithmetic Operators

LAB 2 VECTORS AND MATRICES

Matlab (Matrix laboratory) is an interactive software system for numerical computations and graphics.

MATLAB. Devon Cormack and James Staley

MATLAB for beginners. KiJung Yoon, 1. 1 Center for Learning and Memory, University of Texas at Austin, Austin, TX 78712, USA

Introduction to Matlab

CITS2401 Computer Analysis & Visualisation

MATLAB COURSE FALL 2004 SESSION 1 GETTING STARTED. Christian Daude 1

Chapter 1: An Overview of MATLAB

Array Creation ENGR 1187 MATLAB 2

D-BAUG Informatik I. Exercise session: week 5 HS 2018

Introduction to MATLAB

How to Use MATLAB. What is MATLAB. Getting Started. Online Help. General Purpose Commands

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:

MATLAB Project: Getting Started with MATLAB

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

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

Introduction to MATLAB for Engineers, Third Edition

Array Accessing and Strings ENGR 1187 MATLAB 3

Using the fprintf command to save output to a file.

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

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

EGR 111 Loops. This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times.

MATLAB Project: Getting Started with MATLAB

CME 192: Introduction to Matlab

ECE Lesson Plan - Class 1 Fall, 2001

MATLAB GUIDE UMD PHYS401 SPRING 2011

Array Accessing and Strings ENGR 1181 MATLAB 3

MATLAB GUIDE UMD PHYS375 FALL 2010

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5

Introduction to MATLAB 7 for Engineers

MATLAB GUIDE UMD PHYS401 SPRING 2012

Vector: A series of scalars contained in a column or row. Dimensions: How many rows and columns a vector or matrix has.

(Creating Arrays & Matrices) Applied Linear Algebra in Geoscience Using MATLAB

SIMPLE INPUT and OUTPUT:

Computer Vision. Matlab

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.

MATLAB/Octave Tutorial

Introduction to Matlab

MATLAB Tutorial III Variables, Files, Advanced Plotting

EEE161 Applied Electromagnetics Laboratory 1

MATLAB Demo. Preliminaries and Getting Started with Matlab

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

MATLAB Tutorial Matrices & Vectors MATRICES AND VECTORS

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

Dr Richard Greenaway

1 Introduction to Matlab

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

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.

Introduction to Programming for Biology Research

MATLAB: The Basics. Dmitry Adamskiy 9 November 2011

MATLAB - Lecture # 4

EE 301 Signals & Systems I MATLAB Tutorial with Questions

Dr Richard Greenaway

Lecture 1: Hello, MATLAB!

VARIABLES Storing numbers:

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

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

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

CMAT Language - Language Reference Manual COMS 4115

Slightly revised version of Scilab and Matrices by David Arnold, College of the Redwoods, August 17, 1998.

Control Structures. CIS 118 Intro to LINUX

Desktop Command window

Computer Packet 1 Row Operations + Freemat

LAB 2: Linear Equations and Matrix Algebra. Preliminaries

Solve, RootOf, fsolve, isolve

AN INTRODUCTION TO MATLAB

1 Introduction to MATLAB

Laboratory 1 Octave Tutorial

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Basic concepts. Chapter Toplevel loop

Algorithms and Data Structures

MATLAB Lecture 1. Introduction to MATLAB

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

EGR 111 Loops. This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times.

Basics: vectors, matrices, arrays

University of Alberta

Introduction to Matlab

TUTORIAL MATLAB OPTIMIZATION TOOLBOX

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

Digital Image Processing

MATLAB TUTORIAL WORKSHEET

Matlab Tutorial: Basics

1 Introduction to MATLAB

IDL Lab #6: The IDL Command Line

Transcription:

1 ME313 Homework #1 Matlab- Command Window Operations, Scalars and Arrays Last Updated August 17 2012. Assignment: Read and complete the suggested commands. After completing the exercise, copy the contents of the command window into Word or Adobe, and save as a pdf. Turn in a hardcopy of the pdf. Organization of Matlab software: Upon opening Matlab, multiple windows will appear. Depending on which version of Matlab you are using, the screen will look like this The window at the left, with the command prompt >>, is the command window. One uses the command window by typing commands after the prompt. Scalar variables and expressions: Matlab is a programming language. A programming language functions by executing commands in sequence. The information flow for each command is right-to-left, meaning that the = symbols is referred to as the assignment operator. For example, at the command line, type x=5. The command line and response after the command line will be >> x=5 5

2 The meaning of this action is that the number 5 has been placed in a storage location named x, i.e., the information 5 was assigned by the = symbol to be placed in the variable x. This is the meaning of the assignment operator terminology. The storage location x, known as a variable, has been created by the assignment operator. When Matlab is initially opened, no variables exist. They are created by commands. Assignment operator and computational expressions: One can use the assignment operator to perform computations. For example, now type the command y=x^2. The command line and response should look like >> y=x^2 y = 25 The meaning of the command is take the value stored in the variable x, square it, and then store it in the new variable y. The response is to display the value of the variable y. When down and left-to-right information flow is violated, Matlab will stop and display an error message. Type the command y=z. The command and response will be >> y=z??? Undefined function or variable 'z'. An error message, which should appear in red font, was encountered because the numerical value of z has not yet been defined. Error messages are very useful for debugging code (take my word for it). Listing existing variables: An extremely useful command is whos. This command lists the variables that have been created in a session. Type the command whos. The response should be x 1x1 8 double y 1x1 8 double The result is a table of the variables that exist in the current session. The first column contains a list of the names of the variables that have been created. We see that x and y exist, but z does not. The second column gives the dimension each variable. At this point, the variables have been scalars, indicated by the 1x1 dimension. Later, we will consider arrays and other array abstractions, which may be thought of as generalized

3 matrices. The third and fourth columns list the number of bytes that each variable uses to store the numerical information, and the variable type, i.e., whether it is a floating point number (double), or integer, among some of the options. Clearing variables in a session: Sometimes it is necessary to clear (erase, delete) the defined variables in a session. This is peformed with the clear command. Type the command clear, followed by the command whos. The output should look like >> clear >> Nothing appears after the whos command because the variables x and y have been deleted or erased from the session. Assigning values to vector and square matrix arrays: In the previous examples, the variables x and y were scalars. That is because they stored only one number in each variable. A variable with a single name may store many numbers. Variables can be arrays, in particular column vectors, row vectors, and square matrices are the most elemental examples. Consider the row vector { 1 2.3-5}. The dimensions of this vector are 1x3, i.e., one row and three columns. A variable can be defined to store this row vector. At the command prompt, type x(1,1)=, followed by whos. The output should look like >> x(1,1)=1 1 x 1x1 8 double The response is, as before, a scalar variable named x, that has a value of one, and the whos command shows that x is a scalar. Now type x(1,2)=2.3, followed by whos. The response should be >> x(1,2)=2.3 1.0000 2.3000 Now the response shows two values for x, 1.0000 and 2.3000, and the whos command lists the dimensions of the variable x as 1x2. The syntax x(1,1) and

4 x(1,2) refers to specific elements of the variable named x. Each element of an array is identified by its row and column location. The first number in the parenthesis refers to the row, and the second number in the parenthesis refers to the column location of an individual element in the array. The command x(1,1)=1 assigns the value of one to the element of x located in the first row and first column. The command x(1,2)=2.3 assigns the value of 2.3 to the element of x located in the first row and second column. An aspect of the procedure just described is that Matlab increases the size of the variable to encompass the information that is assigned. Note that the whos command after the assignment x(1,1)=1 command indicates that the dimension of x is 1x1. After the assignment x(1,2)=2.3, the whos command reveals that the dimension of x is 1x2. Matlab automatically expands the size of the array to store increasing amounts of data. As an exercise, define a matrix z and z1 to have the values 3 5.2 10 8 z 12 1 0, z1 2. 6 8.3 0.06 1 Your output should look like z = -3.0000 5.2000 10.0000 12.0000-1.0000 0 6.0000 8.3000-0.0600 and z1 = 8 2-1 Type the whos command. Your output should now look like Alternate method to assign values of arrays: There is another way to assign values to array elements. Type the following command x1=[ 1 2.3], followed by whos. You should obtain the following result

5 >> x1=[1 2.3] x1 = 1.0000 2.3000 x1 1x2 16 double A new array x1 has been created. The array x1 has same dimensions as x, and has identical elements. The syntax of the statement x1=[ 1 2.3] is to enclose the array in square brackets, and separate each column element by a space. Unless otherwise indicated, the elements will be placed in the first row by default. Now consider the statement q=[-3 5.2 10 ; 12-1 0 ; 6 8.3-0.06], followed by the whos command. The response at the command window should be >> q=[-3 5.2 10 ; 12-1 0 ; 6 8.3-0.06] q = -3.0000 5.2000 10.0000 12.0000-1.0000 0 6.0000 8.3000-0.0600 q 3x3 72 double x1 1x2 16 double A new variable q has been created, which is a 3x3 matrix that has elements identical to those of z created earlier. The syntax of the statement q=[-3 5.2 10 ; 12-1 0 ; 6 8.3-0.06] is again to enclose the array in square brackets, separate column elements with a space, but now indicate a change to a new row with the semicolon ; character. Thus, to assign array elements in the manner, enclose the numerical values with square brackets, separate column entries with a space, and indicate movement to a new row with the semi-colon ;.