Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Slides:



Advertisements
Similar presentations
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Advertisements

© Paradigm Publishing, Inc Excel 2013 Level 2 Unit 2Managing and Integrating Data and the Excel Environment Chapter 7Automating Repetitive Tasks.
Introduction to Excel Formulas, Functions and References.
Developing an Excel Application
Tutorial 8: Developing an Excel Application
Introduction to Excel Chapter 2 Excel Fundamentals Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Flow Charts, Loop Structures
 2005 Pearson Education, Inc. All rights reserved Introduction.
Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction.
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
Matrix Mathematics in MATLAB and Excel
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
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.
Digital Image Processing Lecture3: Introduction to MATLAB.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
Microsoft Excel How to make a SPREADSHEET. Microsoft Excel IT is recommended that you have EXCEL running at the same time. You can try what you are reading.
MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd.
XP New Perspectives on Introducing Microsoft Office XP Tutorial 1 1 Introducing Microsoft Office XP Tutorial 1.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Working with a Database
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.
Copyright 2007, Paradigm Publishing Inc. EXCEL 2007 Chapter 7 BACKNEXTEND 7-1 LINKS TO OBJECTIVES Record & run a macro Record & run a macro Save as a macro-
Lecture 4 MATLAB Windows Arithmetic Operators Maintenance Functions
Objectives Understand what MATLAB is and why it is widely used in engineering and science Start the MATLAB program and solve simple problems in the command.
Recap Graphic Window Edit Window Start Button Matrices in MATLAB Scalar Operations Order of Operations Array Operations Matrix Addition Matrix Multiplication.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lesson 5 Using FunctionUsing Function. Objectives.
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.
McGraw-Hill/Irwin The Interactive Computing Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Excel 2002 Lesson 1 Introduction.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Input, Output, and Processing
Copyright © The McGraw-Hill Companies, Inc. Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 1 An Overview of MATLAB.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
A Brief introduction to MatLAB ($50 - $99 for students)
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
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.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
Matlab for Engineers Matlab Environment Chapter 2.
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Tutorial 2 : Matlab - Getting Started.
Microsoft Excel ( XP-2003). Return to Table of Contents Table of Contents 1_ Introduction to ExcelIntroduction to Excel 2_ Overview of the Excel.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Chapter 4 MATLAB Programming MATLAB Troubleshooting Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
McGraw-Hill/Irwin The Interactive Computing Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Excel 2002 Using Macros Lesson.
Development Environment
Topics Designing a Program Input, Processing, and Output
Release Numbers MATLAB is updated regularly
Matlab Training Session 4: Control, Flow and Functions
Chapter 4 MATLAB Programming
Variables, Expressions, and IO
Basic operations in Matlab
Lecture 1: Introduction
Digital Image Processing
Matlab.
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Topics Designing a Program Input, Processing, and Output
Experiment No. (1) - an introduction to MATLAB
Topics Designing a Program Input, Processing, and Output
Using Script Files and Managing Data
Presentation transcript:

Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

MATLAB INTERFACE Engineering Computation: An Introduction Using MATLAB and Excel

Command Window Engineering Computation: An Introduction Using MATLAB and Excel This is the command prompt – MATLAB commands are entered here

Command Window MATLAB commands enter at the prompts can be divided into two categories: – Interactive commands, in which we directly enter the instructions into the command window, and – File commands, in which we call up a series of commands stored in a MATLAB file The interactive mode is useful for quick computations, testing commands, or called up stored values More complex command sequences are stored in files for easy retrieval Engineering Computation: An Introduction Using MATLAB and Excel

Interactive Commands MATLAB can be used like a calculator, returning the answer for mathematical expressions: Note that MATLAB rounded the answer of the second calculation to 5 digits Engineering Computation: An Introduction Using MATLAB and Excel

Number Format By default, MATLAB uses a “short” format to display results. Entering the command format long causes all stored digits to be displayed: The command format short switches the output back to the default format More specific formats can be specified, but the default (short) format is sufficient for most calculations Engineering Computation: An Introduction Using MATLAB and Excel

Variables In the previous examples, the answer was stored in MATLAB with the variable name ans Often, we want to store our input and output values with our own variable names Let’s calculate the volume of a sphere. We will call the diameter of the sphere dia: Engineering Computation: An Introduction Using MATLAB and Excel

Variables When the expression is entered, the value of the variable is echoed to the screen: Note that here the “=“ sign is called an assignment operator The variable (dia) is assigned a value of 3 Engineering Computation: An Introduction Using MATLAB and Excel

Variables Now let’s assign a new variable Vol to the value calculated from the volume formula: Again, the variable’s value is printed to the screen Engineering Computation: An Introduction Using MATLAB and Excel

Workspace Window The Workspace Window shows all currently assigned variables, and their current values Note the columns for Min and Max: MATLAB is especially good for working with arrays, and treats a scalar as a 1 X 1 array Engineering Computation: An Introduction Using MATLAB and Excel

Command History Window Your most recent entries to the Command Window are stored in the Command History Window, making it easy to retrace your steps Engineering Computation: An Introduction Using MATLAB and Excel

Changing a Variable Let’s change the value of the diameter of the sphere to 6 inches: Note that the value of Vol has not changed. Unlike a spreadsheet, where all cells containing formulas update automatically, in a programming language the variables are not reassigned unless we issue instructions to reassign them Engineering Computation: An Introduction Using MATLAB and Excel

Updating the Volume We could re-enter the formula for Vol, but here is a handy shortcut: From the command prompt, clicking the up arrow key allows you to scroll through the most recent entries. Two clicks of the key displays the assignment of Vol, and pressing Enter reassigns its value. Engineering Computation: An Introduction Using MATLAB and Excel

Suppressing Screen Display At the end of a command, adding a semicolon causes the display to the screen to be skipped: Semi-colons are entered at the ends of most lines in MATLAB files, since we usually don’t want intermediate results displayed to the screen Engineering Computation: An Introduction Using MATLAB and Excel

Displaying Variable Values Entering a variable name at the command prompt displays its value to the screen: Engineering Computation: An Introduction Using MATLAB and Excel

Variable Names MATLAB variable name are case sensitive! This is very important to remember. Suppose we enter the volume formula as: We get an error, since MATLAB does not recognize Dia. If fact, we could assign different variables the names dia, DIA, Dia, etc., and MATLAB would treat them as unrelated variables Engineering Computation: An Introduction Using MATLAB and Excel

Clearing Variables To clear the value of a variable from memory, use the “clear” command: To clear all values, use “clear all” Clearing variables is a good idea at the beginning of programs to ensure that results from previous calculations are not used accidentally (especially with arrays - more on this later) Engineering Computation: An Introduction Using MATLAB and Excel

Hierarchy of Operations Note that our equation for volume required parentheses only around “dia/2”: The exponent operation is done first. Therefore, without the parentheses, the number 2 would be cubed first. MATLAB, like Excel and other computation software, performs calculations in this order: exponentials; multiplication/division; addition/subtraction Engineering Computation: An Introduction Using MATLAB and Excel

Formulas in MATLAB Adding parentheses, even when not required, can often help you to organize formulas Spaces are ignored, and can also be used to make formulas more readable Engineering Computation: An Introduction Using MATLAB and Excel

Functions in MATLAB There are many built-in functions in MATLAB. As an example, consider sin: The value in parentheses is the argument of the function; many functions required multiple arguments, separated by commas Engineering Computation: An Introduction Using MATLAB and Excel

Help With Functions To learn more about a function, type “help” followed by the function name: From the “see also” section, we discover that there is a function called sind, which finds the sine of an angle that is input in degrees rather than radians Engineering Computation: An Introduction Using MATLAB and Excel

More Help You can type “help” at the command prompt to call up a menu of help topics Also, clicking the Help button opens a window where you can browse topics or search for particular keywords Engineering Computation: An Introduction Using MATLAB and Excel

MATLAB Files The interactive mode is good for quick calculations, but frequently used and/or complex algorithms are stored in files MATLAB uses a file extension of.m, and MATLAB files are often called “m-files” Before getting started with m-files, it is important to consider the locations where your files will be stored Engineering Computation: An Introduction Using MATLAB and Excel

Current Directory At the top of the screen is the name of the current directory, where by default your new m-files will be stored. A list of MATLAB files in the current directory can be displayed by clicking the “Current Directory” tab (this window can be toggled between the Workspace and Current Directory) Engineering Computation: An Introduction Using MATLAB and Excel

Current Directory You may want to store your MATLAB files for each class or project in a specific folder If so, create the directory in Windows and then browse to it from the MATLAB interface to set it as the current directory Engineering Computation: An Introduction Using MATLAB and Excel

Path Files that you create in the new folder will run as long as that folder is set as the current directory in MATLAB However, if another folder is set as the current directory, files from the folder that you just created will not run unless its address is added to the MATLAB path The path is a list of locations that MATLAB searches to find files Engineering Computation: An Introduction Using MATLAB and Excel

Path When you enter a command at the prompt, MATLAB looks for a file matching the command name, beginning with the first location in the path. To add you new folder to the Path, select File: Set Path… Engineering Computation: An Introduction Using MATLAB and Excel

Path Select Add Folder… Browse to your desired folder and click OK… Engineering Computation: An Introduction Using MATLAB and Excel

Path Click Save and Close. Note that your new folder is now the first location searched by MATLAB Engineering Computation: An Introduction Using MATLAB and Excel

M-File Example Consider our sphere example. Let’s find the weight of the sphere by calculating the volume, and then multiplying by the specific weight of the sphere’s material Since we plan to repeat this calculation repeatedly, we will write the formulas in an M-File Engineering Computation: An Introduction Using MATLAB and Excel

m-file Example We open the m-file Editor by clicking on this icon: In the Editor, the lines are automatically numbered. This is very helpful in debugging Note that we have left a semi-colon off the second line, so that the weight will be printed to the screen Engineering Computation: An Introduction Using MATLAB and Excel

m-file Example The m-file is saved to the Current Directory, with the name “WtSphere.m” (the.m is added automatically) Engineering Computation: An Introduction Using MATLAB and Excel

m-file Example After assigning values to the diameter dia and the specific weight SpWt, typing the name of the file executes it: Engineering Computation: An Introduction Using MATLAB and Excel

Adding Comments Text following a % sign in a m-file is added to document the program. Adding comments is important for being able to comprehend and debug a program Engineering Computation: An Introduction Using MATLAB and Excel

Context-Specific Help The MATLAB Editor often displays help when it detects possible errors. In this case, we meant to leave off the semicolon, so we can ignore the message Engineering Computation: An Introduction Using MATLAB and Excel

MATLAB Functions In addition to the built-in functions of MATLAB, users can write their own functions Functions are m-files, with a special first line: For our sphere, the output variable will be WT, the weight. The function name will be WtSphere, and the input variables will be the dia and SpWt Therefore, the first line will be: function Wt = WtSphere(dia,SpWt) Engineering Computation: An Introduction Using MATLAB and Excel

Function Example Here is the rest of the function file: Note that we added a semicolon to the last line; the output variable (WT) will be printed to the screen when we execute the function Engineering Computation: An Introduction Using MATLAB and Excel

Function Example From the command prompt, when we call for the function, we must enter the arguments of dia and SpWt: The comments immediately following the first line are displayed when we ask for help with this function: Engineering Computation: An Introduction Using MATLAB and Excel

Why Use Functions? At first glance, it might appear that the function is exactly the same as the M-File, with only an extra line added. So why use the function? When we called the first M-file, we defined the diameter and specific weight before we called the M-file. In other words, variables stored in memory were passed into the M- file For example, suppose we clear all the variables before running the function: Engineering Computation: An Introduction Using MATLAB and Excel

Why Use Functions? We note that dia and SpWt, variables used within the function, are not stored in memory. Only the temporary variable ans is stored: With a function, only the arguments are passed into the file, and only the outputs are passed out. This means that in a long program, we don’t have to worry about overwriting previously assigned variables. This makes functions more flexible for including in more lengthy algorithms. Engineering Computation: An Introduction Using MATLAB and Excel