65
November 2, 2022 1 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

Embed Size (px)

Citation preview

Page 1: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 1

Laura Broussard, Ph.D.

Professor

COS 131: Computing for Engineers

Ch. 2: Getting Started with MATLAB

Page 2: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

Ch. 2: Getting Started with MATLAB

I. Introduction

II. Programming Language Background

III. Basic Data Manipulation

IV.The MATLAB User Interface

V. Scripts

VI.Engineering Example

April 19, 2023 2

Page 3: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 3

I. Introduction

• The name MATLAB is a contraction of Matrix Laboratory.

• MATLAB was developed for engineers to create, manipulate, and visualize matrices (matrix ≡ lots of numbers arranged in a

rectangular array).

Page 4: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 4

I. Introduction• Fundamental components of MATLAB:

– Accepts one instruction at a time in text form and implements the logic of that instruction (similar to a calculator)

– Large library of modules that provide high-level capabilities for processing data.

– Large collection of toolboxes(Toolbox ≡ separate application program that provides

graphical capabilities

– A Graphical User Interface (GUI) that lets users assemble and implement programs that solve specific problems.

Page 5: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 5

I. Introduction• Advantages over compiled languages:

– MATLAB is an interpreted language. Problem-solving can be much faster than for compiledlanguages.

– Excels in numerical and especially matrix calculations

– A wide set of toolboxes for graphical problem-solving

– Build your own GUIs with MATLAB’s GUIDE (Graphical User Interface Development

Environment)

– Graphic output is high quality Can use in professional reports

Page 6: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 6

I. Introduction

• Compiled programs are better for:

– Large computing projects, especially where there are multiple programmers

– Major GUI and graphics-based programs

Page 7: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

Ch. 2: Getting Started with MATLAB

I. Introduction

II. Programming Language Background

III. Basic Data Manipulation

IV.The MATLAB User Interface

V. Scripts

VI.Engineering Example

April 19, 2023 7

Page 8: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 8

II. Programming Language Background

A. Abstraction

B. Algorithms

C. Programming Paradigms

Page 9: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 9

II. Programming Language Background

A. Abstraction

Abstraction ≡ expressing a “quality” apart from a “particular implementation.”

Example: “Exacto, maroon, Standup stapler”

or “stapler”

Page 10: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 10

Programming Language Background

A. Abstraction

• Two types for computer programming:

1. data abstraction• To convert from degrees Celsius to Kelvin, you add

273 to the temperature.

• Specific is 33°C, abstract is “temperature”

2. procedural abstraction • He drove home from the office.

• Specific is: turn left, drive 3 blocks, turn right,… Abstract is: drive home

Page 11: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 11

II. Programming Language Background

B. Algorithms

• Algorithm ≡ A sequence of instructions for solving a problem.

• Algorithms vary in level of abstraction.

• Baking cookies instructions• For your grandmother – high level of abstraction• For a beginner – low level

• Computer program problems – Start with abstract algorithms,

Page 12: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 12

II. Programming Language Background

B. Algorithms• Algorithms & abstraction in problem-solving

1. Method: Divide entire problem into subproblems. • Solutions to subproblems are algorithms. • Process of solving and assembling subproblems intosolution for entire problem is also an alogorithm of higherabstraction.

2. Process: Start with big picture and go down• Initial attempt at entire problem - high level of abstraction.• Successive attempts for subprograms at lower and lower

levels of abstraction until they work.

Page 13: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 13

II. Programming Language Background

C. Programming Paradigms• paradeigma ≡ to show alongside (Greek)

• Everyday meaning:a set of assumptions, concepts, values, and practices that constitutes a way of viewing reality for the community that shares them, especially in an intellectual discipline

• Computer Programming:a codified set of practices allowing the community of computer professionals to frame their ideas.

Page 14: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 14

II. Programming Language BackgroundC. Programming Paradigms

– Three different types of paradigms:1. Functional Programming

2. Procedural Programming

3. Object Oriented Programming or OOP

Side effect - when a function returns a result and also changes other objects, it has “side effects”

Useful, but problematic

Computer paradigms allow to different degrees

Page 15: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 15

II. Programming Language Background

C. Programming Paradigms1. Functional Programming

• Every programming operation is actually implemented as a function call without side effects (program surroundings do not change by the function call)

• Can prove solution to be mathematically correct

• Of some value in theoretical computer science

• Languages: Lisp and Forth

Page 16: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 16

II. Programming Language Background

C. Programming Paradigms2. Procedural Programming

• Programs are sequences of operations on data items that are generally accessible to all programs.

• Side effects are possible but considered bad programming practice

• Languages: FORTRAN, C, and MATLAB

Page 17: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 17

II. Programming Language Background

C. Programming Paradigms3. Object-Oriented Programming or OOP

• Objects (data, variables) packaged together with the methods or functions that manipulate those data items “everything is an object”⇒

• Side effects explicitly managed

• MATLAB exhibits OOP-like traits, but you won’t need to use them!

• Languages: C++, Ada, and Java

Page 18: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

Ch. 2: Getting Started with MATLAB

I. Programming Language Background

II. Basic Data Manipulation

III. The MATLAB User Interface

IV.Scripts

V. Engineering Example

April 19, 2023 18

Page 19: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

III. Basic Data Manipulation

A. Starting and Stopping MATLAB

B. Assigning Values to Variables

C. Data Typing

D. Classes and Objects

April 19, 2023 19

Page 20: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 20

Basic Data ManipulationA. Starting and Stopping MATLAB

• Practice Exercise 2.1 (p. 21b in text): Starting and stopping MATLAB

• Macintosh:•Start – Click icon on Dock

•End • Menu: MATLAB/Quit

• Click red circle Upper-Left

• Type “quit” or “exit” after prompt in Command Window

(next slide)

• PC’s?

Page 21: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

III. Basic Data Manipulation

April 19, 2023 21

MATLAB Screen

Note the double cursor to the left sideof the command window

Page 22: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

III. Basic Data Manipulation

• Definition of Variable– A representative for a number (algebra) – A symbolic name associated with a value and

whose value may be changed (programming)– Significantly different meanings

⇒ be careful!

April 19, 2023 22

Page 23: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 23

III. Basic Data ManipulationB. Assigning values to Variables

– Point of confusion: MATLAB’s syntax for assigning values to variables looks like an algebra equation. It’s not!

– Example: z = x + y– In programming this means take the values stored

in x and in y, sum them, and place them in the memory location defined as z.

• If x or y has not been assigned a value, get an error.

– In algebra, if z and y are known, can solve for x.

Page 24: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 24

III. Basic Data Manipulation

B. Assigning values to Variables– Example: z = x+ y

– Note: True only for this statement.

– Programmer might change this in the next instruction: z = 4*x – y

– Some programs (Pascal and Ada) use the := to denote the difference

Page 25: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023

Basic Data ManipulationB. Assigning values to Variables

• Practice Exercise 2.2 (p. 23t in text): Assigning Variables

• Set radius equal to 49

• Retrieve radius. Default variable is “ans.”

• Terminate lines by pressing: Enter

Page 26: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 26

III. Basic Data Manipulation

B. Assigning Values to Variables

Variable names may contain any combination of: uppercase and lowercase letters numbers special characters: _ (underscore) and $ (dollar sign) Underscore represents a space (not allowed).

Be consistent (“_” or “$,” not a mix)

May be really long (100’s of characters!)

First 64 characters must be unique

Hyphens not allowed: file-size (no) file_size (yes)

Page 27: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

III. Basic Data ManipulationB. Assigning Values to Variables

“Style Points”Choose names that describe variable’s content

Example: for the velocity of an object, not just “v:” velocity_in_feet_per_second or VelocityInFeetPerSecond

April 19, 2023 27

Page 28: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 28

III. Basic Data ManipulationC. Data Typing

– How does MATLAB treat the data stored in a variable?

– Computer languages may be categorized as:• Untyped

• Typed

– Interpreted or untyped languages determine type of data in a variable by the data that is there.

– Typed languages require user to declare the data type.

Page 29: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 29

III. Basic Data ManipulationC. Data Typing

• Data types:

char (assign by single quotes around characters)

numeric (numbers)

• MATLAB may handle different data types the same way…

Page 30: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 30

III. Basic Data Manipulation

>> radius = 49radius = 49>> radius + 1ans = 50>> radius = 'radius of a circle’radius =radius of a circle>> radius + 1ans = Columns 1 through 12 115 98 101 106 …

C. Data Typing Exer 2.3: Performing basic mathematical operations

MATLAB allows radius to be both types: Initial radius is a numeric type. Second radius is a char type.

“radius + 1” depends on the type of dataradius is!

Good or bad?Good – Can assign a variable type without advance preparation.Bad – A typo can make a new variable Other program runtime errors

Page 31: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 31

III. Basic Data Manipulation• Data Typing

– Typed languages require the programmer to declare the name and data type of a variable

– Compiler controls the appropriate use of each variable

– Weak typing – programmer uses only the normal data types

– Strong typing – programmer defines specific data types with a limited set of permissible interactions

Page 32: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 32

III. Basic Data ManipulationD. Classes and Objects

• Variables have two attributes Value - determined by what is assigned Data type (class) - the type of data stored

Example: myShoeSize = 9.5MATLAB considers:the value contained in myShoeSize to be 9.5 its class to be double (the default numeric type).A value of data is also called an object.

Page 33: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

Ch. 2: Getting Started with MATLAB

I. Introduction

II. Programming Language Background

III. Basic Data Manipulation

IV.The MATLAB User Interface

V. Scripts

VI.Engineering Example

April 19, 2023 33

Page 34: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 34

IV. The MATLAB User Interface• MATLAB Interface Basics

• Several display windows are visible

• Default view:• Left side – Current Folder

• Middle – Command Window

• Right – Workspace and History Windows (stacked)

• Other windows (Editor, Graphing) open as needed

Page 35: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 35

IV. The MATLAB User Interface

Close icon Current directory

Workspace Window

Command window

Command historyMacintosh Display

Page 36: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 36

V. The MATLAB User InterfaceMATLAB can be used in two modes:

• Command mode – when you need immediate responses to specific MATLAB commands

Commands not saved permanently

• Edit mode – where you develop practical solutions to real problems

Create and execute a text file of commands

Page 37: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 37

V. The MATLAB User Interface• Command Window

– Exercise 2.4 Using the Command window– Smith text, page 29, top

Page 38: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 38

V. The MATLAB User Interface• Command History Window

– Records the commands you issued in the command window in chronological order

– Retains list of commands when Command Window is cleared (clc)

– Window retains a list of commands from previous MATLAB sessions

– Clear by menu: Edit / Clear Command History

Page 39: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 39

V. The MATLAB User Interface• Command History Window

– Exercise 2.5: Using the Command History Window

– Smith text, p. 28t

Page 40: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 40

V. The MATLAB User Interface• Workspace Window

– Keeps track of the variables you use– Columns display information about variables:

• the name of the variable

• the current value

• class (data type) with an icon and entry

• others information can be selected

Page 41: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 41

V. The MATLAB User Interface

• Workspace Window and VariablesExercise 2.6: Showing more details in the

workspace window, p. 28b

Exercise 2.7: Defining other variables, p. 29m

Exercise 2.8: Creating a vector, p. 29b

Page 42: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 42

V. The MATLAB User Interface• Workspace Window and Variables

– Notes on vectors and matrices• Semicolons are used to separate rows

• Can recall the values for any variable by just typing in the variable name

• If you suppress the workspace window, you can still find out what variables have been defined by using the who and whos commands.

• who – lists the variable names

• whos – lists the variable names together with their size and class

Page 43: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 43

V. The MATLAB User Interface

• Workspace Window and Variables

– Exercise 2.9: Creating a 3 x 4 matrix, p. 30b

– Exercise 2.10: Recalling values for variables,

p31t

– Exercise 2.11: Using the who and whos

command, p. 33b

Page 44: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 44

V. The MATLAB User Interface• Current Directory Window

– This window gives the current directory path where MATLAB is retrieving and storing your files

Note how unintuitive theCurrent directory windowis. Your eye skips right over it.

Page 45: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 45

V. The MATLAB User Interface• Variable Editor

– Double-clicking on any variable in the Workspace window automatically launches a document window containing the Array editor.

– Array editor allows you to enter new data or change existing data

– A semicolon at the end of data entry to a variable suppresses the display of those values in the command window.

Page 46: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 46

V. The MATLAB User Interface

• Variable Editor– Exercise 2.12: Creating a two-dimensional matrix,

p. 33t

Page 47: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 47

V. The MATLAB User Interface• Graphics Window

– Created automatically when a MATLAB command requests a graph.

– Additional graphics requests will overwrite the contents of the current graphic window unless you request MATLAB to open a new Graphics window

Page 48: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 48

V. The MATLAB User Interface• Graphics Window

– Created automatically when a MATLAB command requests a graph.

– Additional graphics requests will overwrite the contents of the current graphic window unless you request MATLAB to open a new Graphics window

– Exercise 2.13 Creating a graph, p. 33b

Page 49: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 49

The MATLAB User Interface• Graphics Window

Page 50: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 50

V. The MATLAB User Interface• Editor Window

– The text editor for MATLAB– Create or modify text files here– Opened by choosing File > New > M-File– Lets you type and save a series of commands without

executing them– Also open the editor window by double-clicking a file

name in the current directory window or by typing– >> edit <file_name> in the command window

Page 51: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

Ch. 2: Getting Started with MATLAB

I. Introduction

II. Programming Language Background

III. Basic Data Manipulation

IV.The MATLAB User Interface

V. Scripts

VI.Engineering Example

April 19, 2023 51

Page 52: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 52

VI. Scripts• Text files

– Section describes the basic mechanism for creating, saving, and executing scripts (programs) as m-files

– Use text files as a permanent means of saving scripts

– A script is like writing an email message – lines of text written in a ‘smart’ editor

Page 53: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 53

VI. Scripts• Creating Scripts

– A combination of executable instructions interpreted by MATLAB and comment statements that document the script – help readers understand what the program is doing

– Comments are created by placing a percent symbol, “%”, at the beginning of any text you desire to be a comment; good through the end of the line in which it was created; comment lines are green in editor window

– Uses the extension .m for script files

– Exercise 2.14: Creating a script, p. 37b

Page 54: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 54

VI. Scripts• Creating Scripts

See line by line descriptionof this simple MATLABprogram on page 36 of theSmith text.

Page 55: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 55

VI. Scripts• The Current Directory

– Must name and save the script in a directory where MATLAB can find it

– MATLAB expects a path like c:\MATLABxxx\work

– You can provide MATLAB with a different directory to use in the current directory window; you will then need to use the browse button pointed out earlier to retrieve the m-file

– Exercise 2.15: Saving a script, p. 39t

Page 56: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 56

VI. Scripts• Running Scripts

– Can run a script using any of the following methods:• Type the name of the script in the command window

• Choose the Debug > Run menu item in the MATLAB editor window

• Press the F5 key when the script is visible in the editor. This automatically saves the script before executing it.

– Exercise 2.16: Running a script, p39b

Page 57: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 57

VI. Scripts• Debugging Scripts

– Uses breakpoints – places in your program where you want to verify your code is doing what you want it to

– Click the small dash between the line number and the start of the text line in the Editor window

– Can examine the content of variables by passing the mouse slowly over the variable in the Workspace window or variable in the Editor window

– Watch for inadvertently typed semicolons; does weird things to your programs!

– Exercise 2.17 Debugging a script, p. 40b

Page 58: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

Ch. 2: Getting Started with MATLAB

I. Introduction

II. Programming Language Background

III. Basic Data Manipulation

IV.The MATLAB User Interface

V. Scripts

VI.Engineering Example

April 19, 2023 58

Page 59: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 59

VI. Engineering Example: Spacecraft Launch• Problem: Assuming that the spacecraft uses all its

fuel to achieve a vertical velocity u at 25,000 feet, what is the value of u for the spacecraft to reach outer space?

Page 60: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 60

VI. Engineering Example: Spacecraft Launch• Solution:• Two parts to this problem:

– Converting units to the metric system

– Choosing and solving an equation for motion under constant acceleration

Page 61: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 61

VI. Engineering Example: Spacecraft Launch• Part 1 – converting units to the metric system• 1 inch = 2.54 cm• We have: meters meters cm inch

* *foot cm inch foot

meters0.01*2.54*12

foot

Page 62: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 62

VI. Engineering Example: Spacecraft Launch• MATLAB code for converting units to the metric

system

Note line by linedescription of codeon pages 41-42 oftext.

Page 63: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 63

VI. Engineering Example: Spacecraft Launch• MATLAB code for solving for the equation• Need the following:

– Initial and final altitudes from which you can compute the distance traveled: s

– Motion is under constant acceleration, the force of gravity, g

– Just to reach outer space, the final velocity, v, is 0

– Initial velocity, u, is needed

Page 64: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 64

VI. Engineering Example: Spacecraft Launch• MATLAB code for solving for the equation• Equation for motion under constant acceleration

connecting u, v, s, and a is:

2 2

2

2

2

2

0 2

0 2

2

2 final equation

v u as

u as a g

u gs

gs u

u gs

Page 65: Covenant College October 7, 20151 Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Ch. 2: Getting Started with MATLAB

April 19, 2023 65

VI. Engineering Example: Spacecraft Launch• Complete MATLAB code for solving for the

equation

Note line by linedescription of codeon pages 41-43 oftext.