ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.

Slides:



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

Computer Programming w/ Eng. Applications
ENG 1181 College of Engineering Engineering Education Innovation Center Microsoft Excel: Data Analysis Trendlines Formulas Functions Cell Addressing.
MATLAB ME1107 Y Yan Reference: MATLAB for Engineers by Holly Moore (Pearson Prentice Hall)
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
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
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
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.
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
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
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.
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
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.
Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 1.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Copyright © The McGraw-Hill Companies, Inc. Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 1 An Overview of MATLAB.
Chapter 1: Getting Started with MATLAB MATLAB for Scientist and Engineers Using Symbolic Toolbox.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
A Brief introduction to MatLAB ($50 - $99 for students)
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 &
ENG College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables.
Engineering Analysis – Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
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.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2.
Interduction to MATLAB Manal Alotaibi Mathematics department College of science King saud university.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
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.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Introduction to Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
Matlab for Engineers Matlab Environment Chapter 2.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
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.
Introduction to MATLAB
ECE 1304 Introduction to Electrical and Computer Engineering
Arithmetic Operations
ECE 1304 Introduction to Electrical and Computer Engineering
Introduction to MATLAB for Engineers, Third Edition
MATLAB DENC 2533 ECADD LAB 9.
Lecture 1: Introduction
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Experiment No. (1) - an introduction to MATLAB
MATLAB Introduction MATLAB can be thought of as a powerful graphing calculator but with a lot more buttons! It is also a programming language, commands.
Introduction to MATLAB
MatLab Program Used to Calculate Interactive
Introduction to Matlab
Presentation transcript:

ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction P. 1 Agenda  MATLAB Overview  The Command Window  Simple Script Files

ENG 1181 First-Year Engineering Program MATLAB Quizzes Note MATLAB quizzes are assigned in the daily assignment list Material covered on the quizzes will include the most recent preparation materials plus any previous lectures. It is ok to reference the book or slides while taking the quiz. P. 2

ENG 1181 First-Year Engineering Program Slide Format Note the upper right corner indicate pages from the MATLAB book which correspond with the material covered on the slide. P. 3 Introduction Textbook It is very important to review the chapters in the MATLAB book prior to class. They contain complementary information and valuable examples.

ENG 1181 First-Year Engineering Program P. 4 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. 1- 3

ENG 1181 First-Year Engineering Program P. 5 MATLAB’s Default Windows

ENG 1181 First-Year Engineering Program P. 6 (The Command Window opens when MATLAB is started) Command window Command prompt 8- 9

ENG 1181 First-Year Engineering Program P. 7 (The Figure Window opens automatically by the plot() command) 7 Figure window

ENG 1181 First-Year Engineering Program P. 8 (The Editor Window is opened from the file menu in the Command Window) 7 Editor window

ENG 1181 First-Year Engineering Program P. 9 (The Help Window can be opened from the Help menu of any of MATLAB windows) 8 Help window

ENG 1181 First-Year Engineering Program P. 10 WORKING IN THE COMMAND WINDOW 8- 9 Place the cursor at the command prompt (>>) to enter a command Press the ‘Enter’ key to execute the command (Line-at-a-time-execution) To correct a previously typed command press the up arrow key ( ) to recall the desired command

ENG 1181 First-Year Engineering Program MATLAB’s Working Directory P. 11 When you first start MATLAB it is a good practice to change the working directory to your N: drive or USB device. You can use the browse icon to locate the directory you want to use

ENG 1181 First-Year Engineering Program P. 12 ARITHMETIC OPERATIONS WITH SCALARS 10 Operation Addition Subtraction Multiplication Right Division Left Division Exponentiation Symbol + - * / \ ^ Example * 3 5 / 3 5 \ 3 = 3 / 5 5 ^ 3

ENG 1181 First-Year Engineering Program P. 13 ORDER OF PRECEDENCE Higher-precedence operations are executed before lower- precedence operations (like Excel or programmable calculators). If two operations have the same precedence, then the expression is executed from left to right. PRECEDENCEOPERATION FirstParentheses (innermost pair first) SecondExponentiation. ThirdMultiplication and division FourthAddition and subtraction 10

ENG 1181 First-Year Engineering Program P. 14 MATLAB AS A CALCULATOR Using numbers: 8 / 2 is executed first >> / 2 ans = 11 Type and press Enter Computer response is executed first >> (7 + 8) / 2 ans = Type and press Enter Computer response 27 ^ 1 and 32 ^ 0.2 are executed first / is executed next + is executed last >> 27 ^ 1 / ^ 0.2 ans = 11 >> 27 ^ (1 / 3) + 32 ^ 0.2 ans = 5 1 / 3 is executed first ^ is executed next + is executed last 11

ENG 1181 First-Year Engineering Program P. 15 DEFINING VARIABLES A variable becomes defined when a value is assigned to it using the assignment operator, =. Once a variable is defined, the computer remembers and stores its value. The variable can then be used in further calculations, used to create new variables, passed into functions. The value of a variable can be modified at any time. >> a = 8 a = 8 >> B = 12 B = 12 Type and press Enter Computer response >> a + B ans = 20 >> a / B ans = >> d = a * B d = 96 >> sqrt(d) ans =

ENG 1181 First-Year Engineering Program P. 16 THE ASSIGNMENT OPERATOR >> x = 3 x = 3 Note: In algebra this expression (x=x+5) is invalid since it implies: 0 = 5. >> x = x + 5 x = 8 MATLAB assigns a new value to x, which is the old value (3) plus The statement: ‘x + 4 = 30’ Is not valid. Matlab does not solve for x >> x + 4 =30 ??? x + 4 = 30 But, the statement ‘x = 30 – 4’ is valid (the number 26 is assigned to x) >> x = x = 26 Assigns the value 3 to variable x.

ENG 1181 First-Year Engineering Program P. 17 RULES ABOUT VARIABLES 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. For example, 'number‘, ‘NUMBER’, and 'Number' are NOT the same variable.  It is good practice to make the variable name descriptive For example, ‘avg_score’ or ‘avgScore’ instead of ‘a’ 18

ENG 1181 First-Year Engineering Program P. 18 RULES ABOUT VARIABLES NAMES Avoid naming variables names that are used by MATLAB  Names of MATLAB functions: exp, sin, cos, sqrt, max, min, sum, etc.  Names of MATLAB-defined variables: π (accurate to 2.2 * 10 ^ -16) (the solution from the most recent calculation) Etc.  Note that e is not a MATLAB-defined variable (use exp) 18 pi ans

ENG 1181 First-Year Engineering Program P. 19 MATLAB BUILT-IN MATH FUNCTIONS exp(x)exponential (e x ) log(x) natural logarithm (log base “e”, written ln in math expressions) log10(x) base 10 logarithm (log in math expressions) sqrt(x) square root abs(x) absolute value NOTE: For trigonometric functions, x is in radians sin(x), cos(x), asin(x), acos(x), tan(x), cot(x) For each trig function there is an equivalent, e.g. sind(x) where x is in degrees

ENG 1181 First-Year Engineering Program Built-in Functions Here are some examples of using MATLAB built-in functions: P. 20 >> sin(pi / 4) ans = >> sqrt(169) ans = 13 >> log10(10000) ans = 4 MATLAB has hundreds of built-in functions (this will be discussed in future lectures). More functions are listed in section 1.5 of the text.

ENG 1181 First-Year Engineering Program Example Calculate the value of this function for x=30 degrees: cos 2 x sinx + tan(x/2) / √3x In the command window: >>x=30(Assigns the value 30 to x) >>x=x*pi/180(Converts degrees to radians) >>cos(x)^2*sin(x)+tan(x/2)/sqrt(3*x) P. 21

ENG 1181 First-Year Engineering Program Script files What if you wanted do a lengthy calculation many times. What’s the chance of a typo causing an error? MATLAB allows you to save any series of commands in a file and will execute the commands sequentially as if they were typed directly into the command window. These files are called script files.

ENG 1181 First-Year Engineering Program Example with Script file Suppose we created a script file called trig_calc which contained the following: Line 1: x=x*pi/180 Line 2: cos(x)^2*sin(x)+tan(x/2)/sqrt(3*x) Then we would only have to type the following into the command window: >>x=30 >>trig_calc The result would be displayed in the command window

ENG 1181 First-Year Engineering Program SCRIPT FILE  Using a script file is convenient because it can be…  edited (corrected and/or changed)  executed many times with reliable results  saved, then opened at a later date  sent to others to use  Script files are also called M-files because the extension.m is used when they are saved

ENG 1181 First-Year Engineering Program THE M-FILE EDITOR/DEBUGGER WINDOW The commands of the script file are typed line-by-line. The lines are numbered automatically A new line starts when the Enter key is pressed. The Run Icon

ENG 1181 First-Year Engineering Program EXAMPLE OF A SCRIPT FILE % Example of a script file % This file calculates the square of x x = sin(pi / 4) y = x ^ 2 Define x Store result in y Typing ‘%' at the beginning of a line designates the line as a comment  comments are not executed 79

ENG 1181 First-Year Engineering Program SAVING A SCRIPT FILE  Once the script file is completed, it must be saved. In our class use Save As to your N:\ or USB drive. This should be the same as the working directory you specified when you started MATLAB.  The name of the script file follows the rules for names of variables in MATLAB  Can be up to 63 characters long.  Must begin with a letter.  Can only contain letters, digits, and the underscore character (no spaces).  Case sensitive. 78

ENG 1181 First-Year Engineering Program RUNNING A SCRIPT FILE  A script file can be executed from the Editor Window by clicking on the Run icon (this saves the m-file before running).  A script file can also be executed from the Command Window by typing its name and pressing Enter Refer pp in the book about other ways to change the Current Directory or the Search Path.

ENG 1181 First-Year Engineering Program EXAMPLE OF RUNNING A SCRIPT FILE % Example of a script file % This file calculates the square of x x = sin(pi / 4) y = x ^ 2 Script file: >> Test1 x = y = >> Command window: Enter script file name MATLAB response Save script file as Test1

ENG 1181 First-Year Engineering Program SOME USEFUL COMMANDS 10, 19 Clears the command window Clears all variables from memory Clears only variables x, y and z Careful, this command only clears x >> clc >> clear >> clear x y z >> clear x, y, z >> x = 5 x = 5 >> x = 5; >> Typing a semi-colon (;) at the end of a line of input prevents the output from being displayed in the command window

ENG 1181 First-Year Engineering Program OUTPUT FROM A SCRIPT FILE  When a script file runs, output that is generated is displayed in the Command Window  Output is displayed automatically if a statement does not end with a semicolon. Many times you don’t want all outputs to be displayed in the command window.  Output can also be displayed intentionally by using the disp() command

ENG 1181 First-Year Engineering Program THE disp() COMMAND disp(A) disp(‘text’) string disp() adds a ‘line feed’ to the end of it’s input. So, any following output appears on a new line. Displays the value of the variable A. Displays the text (string) that is enclosed within the single quotes

ENG 1181 First-Year Engineering Program DISPLAY FORMATS The format command controls how output numbers appear on the screen. Input numbers can be typed in any format. >> format short (default) Fixed-point with 4 decimal digits >> format long Fixed-point with 15 decimal digits >> format short e e+001 Scientific with 4 decimal digits >> format bank Two decimal digits (like money) Note: These commands affect the output only they do not affect the precision of the number in memory Other formatting options are discussed in section 1.4 of the text >> format short >> format long >> format short e >> format bank

ENG 1181 First-Year Engineering Program Homework Example clc disp ('Student, Joe') disp ('EG167, Seat 99') disp ('Assignment 1') disp ('Problem 4') prob4a = cos(5*pi … … Script File Student, Joe EG167, Seat 99 Assignment 1 Problem 4 prob4a = … Command Window Submit a printout of both the Script File and the Command Window