Introduction to Engineering gii

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

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

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

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

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

Matlab Programming Introduction 1 2

Welcome to EGR 106 Foundations of Engineering II

Introduction to Matlab

PART 1 PROGRAMMING WITH MATHLAB

Numerical Analysis First Term Dr. Selcuk CANKURT

Introduction to MatLab. Introduction to MatLab K. Craig 1

Chapter 1 Introduction to MATLAB

Computer Programming in MATLAB

AMS 27L LAB #1 Winter 2009

Introduction to MATLAB

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY

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

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

Dr Richard Greenaway

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

Computer Programming ECIV 2303 Chapter 1 Starting with MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering

MATLAB QUICK START TUTORIAL

MATLAB NOTES. Matlab designed for numerical computing. Strongly oriented towards use of arrays, one and two dimensional.

Programming in Mathematics. Mili I. Shah

Digital Image Analysis and Processing CPE

Basic stuff -- assignments, arithmetic and functions

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

Chapter 2. MATLAB Basis

Introduction to Programming

ELEMENTARY MATLAB PROGRAMMING

Part V Appendices c Copyright, Todd Young and Martin Mohlenkamp, Department of Mathematics, Ohio University, 2017

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

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

Introduction to MATLAB

Lecture 1: What is MATLAB?

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

EGR 111 Introduction to MATLAB

Programming in QBasic

2.0 MATLAB Fundamentals

EP375 Computational Physics

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

General MATLAB Information 1

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

Finding, Starting and Using Matlab

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

A GUIDE FOR USING MATLAB IN COMPUTER SCIENCE AND COMPUTER ENGINEERING TABLE OF CONTENTS

Math 2250 MATLAB TUTORIAL Fall 2005

Lab 1 - Worksheet Spring 2013

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

LAB 1 General MATLAB Information 1

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

Goals for This Lecture:

Matlab as a calculator

Matlab Introduction. Scalar Variables and Arithmetic Operators

McTutorial: A MATLAB Tutorial

ANSI C Programming Simple Programs

3.1. Chapter 3: The cin Object. Expressions and Interactivity

Basic MATLAB Tutorial

Introduction to MATLAB

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

Mathematical Operations with Arrays and Matrices

C++ Programming Lecture 11 Functions Part I

1 Introduction to Matlab

This is a basic tutorial for the MATLAB program which is a high-performance language for technical computing for platforms:

Getting to Know Maple

Lecturer: Keyvan Dehmamy

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

AN INTRODUCTION TO MATLAB

A very brief Matlab introduction

This page intentionally left blank

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

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

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

A Short Introduction to Matlab

Introduction to Matlab

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Julia Calculator ( Introduction)

What is Matlab? The command line Variables Operators Functions

Introduction to MATLAB for Numerical Analysis and Mathematical Modeling. Selis Önel, PhD

Matlab Examples. (v.01, Fall 2011, prepared by HP Huang)

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

Introduction to Python, Cplex and Gurobi

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 Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras

Introduction to MATLAB for Engineers, Third Edition

Graphing Calculator Tutorial

Laboratory 1 Octave Tutorial

WHAT IS MATLAB?... 1 STARTING MATLAB & USING THE COMMAND LINE... 1 BASIC ARITHMETIC OPERATIONS... 5 ORDER OF OPERATIONS... 7

Introduction to MATLAB Practical 1

9 Using Equation Networks

A General Introduction to Matlab

MYSQL NUMERIC FUNCTIONS

CT 229 Java Syntax Continued

Introduction to MATLAB

Introduction to MATLAB

Interactive Computing with Matlab. Gerald W. Recktenwald Department of Mechanical Engineering Portland State University

Engineering Problem Solving with C++, Etter/Ingber

MATLAB The first steps. Edited by Péter Vass

A Brief Introduction to MATLAB

Chapter 3. built in functions help feature elementary math functions data analysis functions random number functions computational limits

Transcription:

25.108 Introduction to Engineering gii Dr. Jay Weitzen Lecture Notes I: Introduction to Matlab from Gilat Book MATLAB - Lecture # 1 Starting with MATLAB / Chapter 1 Topics Covered: 1. Introduction. 2. MATLAB windows. 3. Arithmetic operations with scalars. 4. Working in the command window. 5. Defining variables (scalars). 1-3 MATLAB is a software for numerical computations, visualization and programming. The name MATLAB stands for MATrix LABoratory. Its basic data element is an array (explained later). With MATLAB you can: Use it as a calculator. Define variables and use them in calculations. Use built-in functions (sin, cos, max, min, etc.). Plot graphs. Write and run computer programs. Do symbolic calculations. 1

MATLAB windows: 6 Command Window: Main window that opens when MATLAB is started. It has the command prompt». All commands can be typed in this window. Used for running programs written by user. Figure Window: Contains graphs created by graphics commands. This window opens automatically. Editor Window: Used for writing and editing programs. This window is opened from the File menu in the Command Window. Help Window: Contains help information. This window is opened from the Help menu in any of the previous windows. Command Window (The Command Window opens when MATLAB is started) 6 Command prompt p Figure Window (The Figure Window opens automatically by the plot command) 7 2

Editor Window (The Editor Window is opened from the file menu in the Command Window) 7 Help Window (The Help Window can be opened from the Help menu of any of MATLAB windows) 8 WORKING IN THE COMMAND WINDOW 8-9 To type a command the cursor must be placed after the command prompt (>>). Once a command is typed, and the Enter keyis pressed, the command is executed. (Only the last command is executed. Everything executed before is unchanged) It is not possible to go back to a previous line and make a correction. A previously typed command can be recalled to the command prompt with the up-arrow key. 3

ARITHMETIC OPERATIONS WITH SCALARS 10 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 NOTE: For scalars the arithmetic operations are the usual ones. For vectors and matrices the arithmetic operations can either follows matrix algebra rules, or can be performed on element-by-element basis (discussed in the next lectures). ORDER Of PRECEDENCE (The order in which operations are executed by the computer) 10 Higher-precedence operations are executed before lowerprecedence operations. If two operations have the same precedence, then the expression is executed from left to right. PRECEDENCE First Second Third Fourth OPERATION Parentheses, starting with the innermost pair. Exponentiation. Multiplication and division (equal precedence). Addition and subtraction (equal precedence). USING MATLAB AS A CALCULATOR 11 Using numbers: >> 7+8/2 11 8/2 is executed first >> (7+8)/2 7.5000 7+8 is executed first >> 4+5/3+2 7.6667 5/3 is executed first 4

5^3/2 62.5000>> 5^3 is executed first, /2 is executed next. 11 >> 27^(1/3)+32^0.2 5 1/3 is executed first, ^ is executed next, + is executed last. >> 27^1/3+32^0.2 11 27^1 and 32^0.2 is executed first, /3 is executed next, + is executed last. DISPLAY FORMATS The format command controls how output numbers appear on the screen. Input numbers can be written in any format. 12-13 format short (the default) 41.4286 Fixed-point with 4 decimal digits. format long 41.42857142857143 Fixed-point with 14 decimal digits. format short e 4.1429e+001 Scientific notation with 4 decimal digits. format long e 4.142857142857143e+001 Scientific notation with 15 decimal digits. Format bank 41.43 Two decimal digits. MATLAB has several other formats in which numbers can be displayed. MATLAB BUILT-IN MATH FUNCTIONS In addition to arithmetic operations, MATLAB can be used to calculate elementary math functions. The most common ones are: sin(x) x in radians exp(x) exponential cos(x) x in radians log(x) natural logarithm tan(x) x in radians cot(x) x in radians log10(x) base 10 logarithm sqrt(x) square root The inverse is: asin(x), i ( ) abs(x) absolute value acos(x), etc. Examples: >> sin(0.78539) >> sqrt(169) >> log10(10000) 0.7071 13 4 MATLAB has hundreds of built-in functions (this will be discussed in future lectures). 13-15 5

THE ASSIGNMENT OPERATOR 15-17 In MATLAB, the = sign is called the ASSIGNMENT OPERATOR. The ASSIGNMENT OPERATOR assigns a value to a variable. Variable = A value, or a computable value The left hand side can only be one variable. The right hand side can be a specific value, or a computable expression (an expression that includes values and/or previously defined variables). THE ASSIGNMENT OPERATOR For example, if you type: >> x = 3 MATLAB assigns the value of 3 to x. x = 3 If then you type: >> x = x + 5 x = 8 MATLAB assigns a new value to x, which is the old value 3 plus 5. (In mathematics this expression has no meaning since it implies: 0 = 5.) For example, the statement: x + 4 = 30 is not valid. MATLAB does not solve for x, but the statement: x = 30 4 is valid (the number 26 is assigned to x.) 15-17 15- DEFINING VARIABLES A variable is defined by typing a variable name followed by the assignment operator (equal sign) and then a value, or a mathematical expression. >> a=8 a = 8 >> B=12 B = 12 Once a variable is defined, the computer remembers and stores its value. The variable can then be used in further calculations. >> a+b 20 >> a/b 0.6667 >> B/a 1.5000 >> B^a 429981696 6

15- Variables can also be used to define new variables >> d=a*b d = 96 Once in existence, variables can be used in functions >> sqrt(d) 9.7980 A previously defined variable can be redefined and reassigned a new value. RULES ABOUT VARIABLES NAMES Variable names can be up to 63 characters long. Variable name can contain letters, digits, and the underscore character. Variable name must begin with a letter. MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters. For example, A and a are not the same variable. AVOID USING NAMES OF FUNCTIONS FOR VARIABLES. Once a function name is used to define a variable, the function can not be used. This means that variables should not be named sin, cos, exp, tan, sqrt,., etc. OR: max, min, sum, det,., etc. 7

PREDEFINED VARIABLES MATLAB has several variables that are predefined. These variables can be redefined to have any other value. It is probably better not to use the predefined variables as variable names. Some of the predefined variables are: pi ( ), eps (the smallest difference between two numbers) inf (infinity) i (square root of 1) j (square root of 1) ans (the value of the most recent calculation) Typing these variables gives: >> pi 3.1416 >> sin(pi/4) 0.7071 >> eps 2.2204e-016 >> inf Inf >> i 0 + 1.0000i SOME USEFUL COMMANDS 9, 19 When these commands are typed in the Command Window they either provide information, or perform a task. ; When a semicolon is typed at the end of a line, the computer does not displays the output. >> abc=37 >> def=23; abc = >> 37 clc Clears the command window. clear Removes all variables from memory. clear x y z Clears only variables x, y and z. who Lists the variables currently in memory. clf Clears the Figure Window. % typing % at the beginning of a line designates the line as a comment, which is not executed. 8