EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start...

22
EGR 102 Introduction to Engineering Modeling 1 Lab 03B Introduction to MatLab EGR 102 - Fall 2018

Transcript of EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start...

Page 1: EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version

EGR 102Introduction to Engineering

Modeling

1

Lab 03B

Introduction to MatLab

EGR 102 - Fall 2018

Page 2: EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version

Overview

• Introduction to MATLAB (interface, workspace, command window..).

• Executing basic commands

• Creating / Assigning Variables

• Arrays and vectors

• Accessing built-in functions.

• MATLAB help

2EGR 102 - Fall 2018

Page 3: EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version

MATLAB History• Invented in the late 1970s by Cleve Moler, then Chair

of Comp Science at Univ of New Mexico

• Designed to give student access to alternative matrix program instead of Fortran

• Joined with Jack Little and Steve Bangert to write MATLAB in C, founded The MathWorks in 1984

• Originally adopted by controls engineers, has spread to many other disciplines

3EGR 102 - Fall 2018

Page 4: EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version

MATLAB in Engineering• MATLAB is one of several programming tools

available to engineers• It will be the primary focus of this course

• The name is short for Matrix Laboratory• Name indicates how it functions. Uses data entered as matrices

according to the user’s directions

• Offers much more advanced mathematical capabilities than Excel

• Available on all Engineering computers and for personal use from the MSU Computer Store

4EGR 102 - Fall 2018

Page 5: EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version

Opening MATLAB

• To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version of MATLAB. • It is recommended you create a shortcut.

• Clicking on the icon should open the window shown on the following slides• These next few slides will help you become familiar with the program’s

interface

5EGR 102 - Fall 2018

Page 6: EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version

Command Window

6

• Space used to run/execute commands

• Displays results, messages, errors

• Think of it like a scratch pad

• Command Prompt: >> where commands are typed & executed

EGR 102 - Fall 2018

Page 7: EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version

Workspace

7

• Saves your variables and values

• This data may be utilized in further executions (commands) in the Command Window

EGR 102 - Fall 2018

Page 8: EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version

Command History

8

• Records the commands issued in Command Window

• You can double-click on any previous command to re-execute it in the Command Window

EGR 102 - Fall 2018

Page 9: EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version

Current Folder

9

• Displays existing data and files in your current working folder

• NOTE: Always switch out of C:\Temp

• NOTE: To execute a user-defined function or load data (later in the course), your current folder must be the same as the folder where the data/files are stored

EGR 102 - Fall 2018

Page 10: EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version

Changing the Interface

• Utilize the ‘Layout’ drag-down menu• Modules can be docked and undocked from the program

interface• (click the arrow at the top-right of each module)

10EGR 102 - Fall 2018

Page 11: EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version

Basic Commands• A command is a portion of code that causes the

program to perform some action

• Commands should be entered next to the prompt, >>• The code will only run when you tell it to by hitting enter

• We will only run one command at a time for now

• MATLAB recognizes the standard arithmetic operators and follows the order of operations• These are (…) ^ * / + -

• Must use operators for every arithmetic operation

• Results will be ans = value

11EGR 102 - Fall 2018

Page 12: EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version

Operators Cheat sheetArithmetic Operators:

+ ------ positive- ------ negative+ ------ addition- ------ subtraction* ------ multiplication/ ------ division

^ ------ exponent

12EGR 102 - Fall 2018

Page 13: EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version

Basic Commands

13

Each command should be entered next to a command prompt >>

Without a defined name, MATLAB defaults to ans

Include an operator for every math operation you want performed

Like in Excel, parentheses can be used to manipulate the order of operations

Execute commands one at a time in the command window

EGR 102 - Fall 2018

Page 14: EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version

Variables in MATLAB• Variables can be assigned usable names in MATLAB

• Names must come before the value:

A = 123 and not 123 = A• Names cannot start with a number and cannot be already used by the

program

• Variable names and values are stored for you in the workspace area

14EGR 102 - Fall 2018

Page 15: EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version

Variables in MATLAB• There are 3 major variable types

recognized in MATLAB• Scalar-a single value; one row and one column

• Vector-has either a single row or column of values

• Array or Matrix-has multiple rows and columns of values

• You can manually create arrays or vectors by using brackets, commas, and semicolons• Brackets define the set of values, commas separate

columns, and semicolons separate rows

15EGR 102 - Fall 2018

Page 16: EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version

Variables

16

Names can be given and are remembered by the Workspace

They can be simple or long, and underscores can be used in place of a space

Numbers and capitalization matter and are useful for varying names, but NEVER start a name with a number

Always assign a name then a value. Never the reverse

EGR 102 - Fall 2018

Page 17: EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version

Arrays and Vectors

17

Vectors and Arrays can be manually created with brackets, commas, and semicolons

Always start and end with square brackets

Each comma represents a different column, and each semicolon is a different row

Using only one of these will give either a row vector or column vector

Combining their use creates an array

EGR 102 - Fall 2018

Page 18: EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version

Built-in Functions & Utilities• Many built-in functions come with MATLAB

• These can be accessed by simply typing in their name and providing an input• The general syntax is function_name(input)

• Most names are pretty straight forward. Think of an abbreviation of what operation you want to perform’

Ex: Finding a tangent is tan(…)

• Like all names in MATLAB, spelling and capitalization are important when calling on functions and variables

18EGR 102 - Fall 2018

Page 19: EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version

Built-in Functions & Utilities• Some available built-in functions include:

• sin(x)

• tan(b)

• log10(c)

• log(p)

• acos(y)

19

NOTE:

• Input variable must be defined before using as an input argument

• Explicit input arguments are acceptable• i.e., enter the actual number within the

parentheses• Built-in trigonometric functions require the

input argument in RADIANS• See: >> help sind

NOTE:

• log( ) is used to evaluate the natural log of the input argument

EGR 102 - Fall 2018

Page 20: EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version

MATLAB Help• The HELP feature in MATLAB is very useful

when attempting to troubleshoot or figure out how to code something

• Access it by typing help as a command or clicking on the ‘?’ on the upper corner• Can also type help function_name for a quick link to that

function’s information

• All built in functions as well as many processes and applications have their own page with explanations and examples

20EGR 102 - Fall 2018

Page 21: EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version

Errors

21

Errors will occur when some code is present that MATLAB can’t run

Can be a simple typo, math or syntax error, or attempting something not possible in MATLAB

Usually, MATLAB shows a red error message hinting towards the problem

EGR 102 - Fall 2018

Page 22: EGR 102 Introduction to Engineering Modeling...Opening MATLAB •To open the program, go to start and look for the MATLAB folder. Clicking it should show the currently installed version

Summary

• Introduction to MATLAB (interface, workspace, command window..).

• Executing basic commands

• Creating / Assigning Variables

• Arrays and vectors

• Built-in functions.

• MATLAB help

22EGR 102 - Fall 2018