Introduction to MATLAB

Slides:



Advertisements
Similar presentations
Introduction to Applications & Basic Features. What is MATLAB®? MATLAB® /Simulink® is a powerful software tool for: Performing mathematical computations.
Advertisements

Introduction to MATLAB Week 13 – 4/21/09. Instructor: Kate Musgrave Time: Tuesdays 3-5pm Office Hours: Tuesdays 1:30-3pm
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Introduction to MATLAB 7 for Engineers
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.
THE MATLAB ENVIRONMENT VARIABLES BASIC COMMANDS HELP HP 100 – MATLAB Wednesday, 8/27/2014
1 Chapter 1 MATLAB Primer This introductory chapter is relatively short and has as its main objective the introduction of MATLAB ® to the reader. This.
Lecture 4 MATLAB Windows Arithmetic Operators Maintenance Functions
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
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.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
CMPS 1371 Introduction to Computing for Engineers MatLab.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Introduction to Programming with RAPTOR
MATLAB Technical Computing Environment. MATLAB Matlab is an interactive computing environment that enables numerical computation and data visualization.
Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
1 Week 1: Variables, assignment, expressions READING: 1.2 – 1.4.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables.
1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
EGR 115 Introduction to Computing for Engineers Introduction to MATLAB Friday 29 August 2014 EGR 115 Introduction to Computing for Engineers.
Interduction to MATLAB Manal Alotaibi Mathematics department College of science King saud university.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
Today we will learn MATLAB Click Start  All programm  Class Software  Matlab This command window will be seen with a prompt sign >> Any command can.
Introduction to Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Variables, Operators, and Expressions
Fundamentals of Programming I Overview of Programming
ECE 1304 Introduction to Electrical and Computer Engineering
Chapter Topics The Basics of a C++ Program Data Types
Arithmetic Operations
Chapter 1: Introduction to computers and C++ Programming
ECE 1304 Introduction to Electrical and Computer Engineering
CS005 Introduction to Programming
Basic Elements of C++.
Data Types, Identifiers, and Expressions
The Selection Structure
Variables, Expressions, and IO
Basic operations in Matlab
Introduction to Programming
Intro to PHP & Variables
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 2 Applications and Data.
Basic Elements of C++ Chapter 2.
Lecture 1: Introduction
Data Types, Identifiers, and Expressions
Number and String Operations
Introduction to C++ Programming
MATH 493 Introduction to MATLAB
Use of Mathematics using Technology (Maltlab)
T. Jumana Abu Shmais – AOU - Riyadh
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Matlab.
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Experiment No. (1) - an introduction to MATLAB
Terminal-Based Programs
Introduction to MATLAB
Basics of Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
MatLab Program Used to Calculate Interactive
Introduction to Matlab
Electrical and Computer Engineering Department SUNY – New Paltz
Presentation transcript:

Introduction to MATLAB Instructor notes: These are a new set of slides which have been modified to make the teaching more interactive and example based. The number of slides in each lecture is between 20-25. The idea is to involve the students as you go along. Let the students type all the commands in the lecture along with you. This will give them a good feel of MATLAB. When you see the right arrow at the end of the slide, any further click or keyboard press will lead you to the next slide. So, you should discuss all the relevant topics of that particular slide by the time the arrow appears.

Introduction to MATLAB MATLAB is a powerful program for numerical computations, plotting and programming. Use it as a calculator. Define variables and use them in calculations. Use built-in functions. Plot graphs. Write and run computer programs. Perform symbolic mathematics. Instructor notes: These are a new set of slides which have been modified to make the teaching more interactive and example based. The number of slides in each lecture is between 20-25. The idea is to involve the students as you go along. Let the students type all the commands in the lecture along with you. This will give them a good feel of MATLAB. When you see the right arrow at the end of the slide, any further click or keyboard press will lead you to the next slide. So, you should discuss all the relevant topics of that particular slide by the time the arrow appears.

Open MATLAB now: Click on the shortcut icon or select: start / All programs / MATLAB There will be a fairly long initialization process, then following prompt should appear in the command window: >> It is recommended that the instructor not use SchoolView to control student terminals. Please open MATLAB in exactly the same way as students have on their screen. Below are the steps to get a highly readable display on your computer: To display ONLY the command window, select Desktop / Desktop Layout / Command Window Only To enlarge MATLAB fonts, select File / Preferences / Fonts and change 18 to 36. The commands which the students need to type along with you during the lecture are shown in red after the ‘>>’.

Command Window (where commands are entered) Workspace (variable values) Current Directory (lists file, etc.) Command prompt Command History (what has been typed) Modified by R.C.Busick 1/28/11, Hi 221, 247-6223 Instructor notes: The command window is the primary interface between the user and Matlab. It is the control center for using Matlab. Commands will be invoked here. User written programs will be run from here; this includes script files and function files which will be covered in detail later on. The command prompt is displayed in the command window. All commands must be entered at the command prompt. Matlab is an environment and a language all in one. From the command window various environment manipulation tasks may be done as well. For example the cd path_name command that is common in Unix and Dos can be used from the command window to change the current working directory. The ls (dir) command can be used to display the contents of the current working directory. Added 8/06/07 by Dick Busick to describe different MATLAB panes. Instructor should step through the various panes to describe their use. Note that it is possible to "toggle" between Current Directory and Workspace Command Window (where commands are entered)

MATLAB’s Working Directory When you first start MATLAB it is a good practice to change the working directory to your Z: drive or USB device. Keep all of your files in the same folder so you do not have to constantly change your directory Modified 01/28/11 by R.C.Busick, HI 221 , 247-6223 Instructor note: If the establishment of the "new" operating directory is not made prior to running the editor, any saved files may be placed in a different directory from that of the command window, resulting in great confusion!

Order of Precedence Higher-precedence operations are executed before lower-precedence operations (like Excel or graphing calculators). If two operations have the same precedence, then the expression is executed from left to right. PRECEDENCE OPERATION First Parentheses (innermost pair first) Second Exponentiation. Third Multiplication and division Fourth Addition and subtraction Instructor notes: The order of precedence is very important and the kind of thing that students may not initially pay attention to since it doesn’t seem very exciting, especially those who have not had any programming before. But, it is important and very important in terms of programming. The order of precedence indicates the order of operations…that is the sequence of steps that commands or any of the various operations will be executed in. Even though Matlab (seemingly) processes an entire equation and then moves on to the next, it actually breaks equations down into their smallest components and executes the components in a particular order. Parentheses have the highest order of precedence. Consequently expressions enclosed within parentheses are evaluated first. If there are parentheses nested within other sets of parentheses, then the innermost ones will be evaluated first. It might be important to point out that if students want to make sure that an operation with a normally low order of precedence executes first or earlier, an easy way to accomplish that is to include the lower operation within parentheses. <If students use parentheses for everything, it increases readability, and eliminates confusion> The second highest order of operation is exponentiation. Be careful where parenthesis are placed. The results can be very different. For example: 64^(1/2) is significantly different than 64^1/2 After exponentiation in order of precedence comes multiplication and division. And finally, the lowest order of operation is addition and subtraction. It is also important to note that if there are operations of the same precedence effectively occurring at the same level, then they will be evaluated from left to right.

Precedence of Operations Task : Find the average of 7 and 8 >> 7 + 8 / 2 ans = 11 …not what we wanted  >> (7+8)/2 ans = 7.500 … correct! 

Precedence of Operations Task: Add the cube root of 27 to the 5th root of 32 >> 27 ^ 1/3 + 32 ^ 0.2 ans = 11 … not what we wanted  >> 27 ^ (1/3) + 32 ^ 0.2 5

Variable Definition >> x = 5 x = 5 >> x = x + 5 10 This statement does not make any mathematical sense but in programming language it changes the value of ‘x’ to a new value.

Calculations with Variables We can use the variables defined previously to do calculations. Define the following: a=8, my_var =12 >> a + my_var ans = 20 >> a * my_var^2 1152

Rules About Variable Names Can be up to 63 characters long. Must begin with a letter. Can contain letters, digits, and the underscore character (no spaces). MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters. Avoid naming variables with names that are used by MATLAB: exp, sin, cos, sqrt, length, mean, max, min… Modified 01/28/11 by R.C.Busick, HI 221 , 247-6223 Instructor notes: Matlab is case sensitive. Matlab distinguishes between upper and lower case. So, even if the only difference between two variable names is the case of one (or more letters), then the variables are completely separate variables.

Examples of Variable Names Acceptable: My_name MyName myname1 Unacceptable: 1Name starts with numeral My Name spaces not allowed My-Name special characters not allowed

Built-In Math Functions: Examples exp(x) exponential (ex) log(x) natural logarithm (log base 'e') log10(x) base 10 logarithm (log) sqrt(x) square root abs(x) absolute value Instructor notes: Matlab has numerous built in arithmetic functions. Some commonly used ones appear on this slide, but there are too many to list. It’s important to remind people that the trig functions are in radians. It’s also important to point out that log(x) is the natural logarithm and log10(x) is the base 10 logarithm. There are functions for rounding values as listed on page 15 of the text that can be very useful. There are statistical functions. Functions for summing vectors (which will be discussed later on)

Built-In Math Functions In MATLAB, trigonometric functions use radians. So for these examples, x is in radians: sin(x), cos(x), asin(x), acos(x), tan(x), cot(x) MATLAB also has equivalent functions where x is in degrees: sind(x), cosd(x), tand(x), etc Instructor notes: Matlab has numerous built in arithmetic functions. Some commonly used ones appear on this slide, but there are too many to list. It’s important to remind people that the trig functions are in radians. It’s also important to point out that log(x) is the natural logarithm and log10(x) is the base 10 logarithm. There are functions for rounding values as listed on page 15 of the text that can be very useful. There are statistical functions. Functions for summing vectors (which will be discussed later on)

Built-In Math Functions: Examples Task: calculate sin(∏ / 2). Note: ∏ is defined by MATLAB as pi. >> sin(pi/2) ans = 1

Built-In Math Functions: Examples Task: calculate e2 >> e ^ 2 ??? Undefined function or variable 'e' How do we fix this? >> exp(2) ans = 7.3891

Built-In Math Functions: Examples Task: calculate sin2x, where x = ∏ /4 >> x = pi/4 x = 0.7854 >>sin(2*x) ans = 1

Built-In Math Functions: Examples Task: Using (cos2xsin2x + tan(x/2)) / e3x , find the value for x = 30 degrees. >> x = 30 x = 30 >> x = x * pi / 180 0.5236

Built-In Math Functions: Examples Task: Using (cos2xsin2x + tan(x/2)) / e3x , find the value for x = 30 degrees. >> (cos(2*x)* sin(x)^2 + tan(x/2)) / exp(3*x) ans = 0.0817

Saving a Script File Once the script file is completed, it must be saved. In our class use Save As to your Z:\ or your own USB drive. This should be the same as the working directory you specified when you started MATLAB. ALWAYS SAVE YOUR WORK. Back it up in multiple places for safety! To the cloud!!

Saving a Script File Valid File Names Prob1a.m problemone.m Invalid File Names Prob-1a.m Special Characters not allowed Prob 1a.m Spaces not allowed 1aProb.m Cannot start with number Modified 01/26/11 by R.C.Busick, HI 221 , 247-6223 Instructor notes: The file should be saved to a floppy/zip disk or possibly the Temp/Tmp directories. Remind students that if they use the Temp/Tmp directories, they copy the file to their own floppy/zip disks before leaving. Work can also be saved on their N: drive.

Useful Programming Commands To clear the command window: clc >> clc To clear all variables previously defined: clear >> x = 5 x = 5 >> clear >> x ??? Undefined function or variable 'x' Modified 01/28/11 by R.C.Busick, HI 221 , 247-6223

The Display Function: disp() This function displays a message to the command window. It can be a string of text or variable contents. To display your message as text, use single quotes around your message: >> disp(‘Brutus Buckeye’) Brutus Buckeye

The Display Function: disp() To display any variable contents, do not use quotes: >> x = 5 x = 5 >> disp(x)

>> y = 7; …what happens when you type this? Suppressing Output This is good practice! You don't need all of your program displayed in the command window, just the relevant info. >> x = 6 x = 6 >> y = 7; …what happens when you type this?

Script File Example Pythagoras' Theorem: Hypotenuse2 = Height2 + Base2 Create a script file and name it Pyth.m Clear the command window Clear all variables from memory Display your name and seat number Declare height of triangle as 3 units Declare base of triangle as 4 units Find the hypotenuse of the triangle Hypotenuse A basic example to give students an idea of putting everything they learnt in this lecture in one place. Give students time to write their program and then go through the program on the next slide. Height Base

How To Turn In All Assignments You must print two things for every MATLAB assignment: Script file Command window output … if you do not turn in both, you will lose points  This gives the students an idea of how they should submit their homeworks. They need to submit 2 things – a print of the script file and a print of the command window.

Example Script File Printout Example Command Window Printout This gives the students an idea of how they should submit their homeworks. They need to submit 2 things – a print of the script file and a print of the command window.

Self Check… Do you understand these concepts? Arithmetic Operations with scalars: 5+3, 5*3 , 5^3 Precedence of operations: 7+8/2 vs. (7+8)/2 Variable assignment: a=8, b=12 Calculations with variables: a+b, a*b Important commands: clc, clear Use of semi colons (;) to suppress output Display function: disp(‘Brutus Buckeye’) Mathematical functions: sin(pi/2) , exp(2) Calculate the value of (cos2xsinx + tan(x/2)) / exp(3x) for x = 30 degrees By the end of this lecture, students should be able to understand the examples on this slide.

Homework Assignment MAT-01 Introduction to Matlab