introduction to matlab -...

15
EGR115 Introduction to Computing for Engineers Chapter 1 Introduction to MATLAB from: S.J. Chapman, MATLAB Programming for Engineers, 5 th Ed. © 2016 Cengage Learning Chapter 1 Topics Introduction: Computing for Engineers 1.1 The Advantages of MATLAB 1.2 Disadvantages of MATLAB 1.3 The MATLAB Environment 1.4 Using MATLAB as a Calculator

Upload: ledien

Post on 17-Dec-2018

244 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Introduction to MATLAB - mercury.pr.erau.edumercury.pr.erau.edu/~hayasd87/EGR115/EGR115_Notes_Chapter_1.pdf · MATLAB is an INTERPRETER (no need to compile: means, ease of use), platform

EGR115 Introduction to Computing for Engineers

Chapter 1

Introduction to MATLAB

from: S.J. Chapman, MATLAB Programming for Engineers, 5th Ed.© 2016 Cengage Learning

Chapter 1 Topics

Introduction: Computing for Engineers

1.1 The Advantages of MATLAB

1.2 Disadvantages of MATLAB

1.3 The MATLAB Environment

1.4 Using MATLAB as a Calculator

Page 2: Introduction to MATLAB - mercury.pr.erau.edumercury.pr.erau.edu/~hayasd87/EGR115/EGR115_Notes_Chapter_1.pdf · MATLAB is an INTERPRETER (no need to compile: means, ease of use), platform

Father of Computing: Charles Babbage (1791 – 1871) Charles Babbage (1791 – 1871), an English mathematician, is best remembered for originating the first concept of a programmable computation machinery* (original concept of today’s computer). *Note on the Application of Machinery to the Computation of Astronomical and Mathematical Tables,” Royal Astronomical Society (1822).

The Babbage Analytical Engine (1833 – 1842) The “mill” – executed the operations on values retrieved from the “store” (CPU). The “store” – used punched cards adapted from the Jacquard to specify input (MEMORY). The machine “eats its own tail” – the output redirects to the input for further instructions. ALU (Arithmetic Logic Unit) Computer’s Central Processing Unit (CPU) that performs operations on binary numbers (bitwise math). Multiple ALUs can be found in a single CPU. In some processors, the ALU is divided into AU (Arithmetic Unit) and LU (Logical Unit). Are today’s most advanced computers POWERFUL ENOUGH to do anything?

NASA Columbia (https://www.nas.nasa.gov/hecc/resources/columbia.html) ERAU Prescott: Advanced Computing & Simulations Laboratory (ACSL)

Chapter 1Slide 1 of 14

Introduction: Computing for Engineers

Page 3: Introduction to MATLAB - mercury.pr.erau.edumercury.pr.erau.edu/~hayasd87/EGR115/EGR115_Notes_Chapter_1.pdf · MATLAB is an INTERPRETER (no need to compile: means, ease of use), platform

The Purpose of EGR115 (Introduction to Computing for Engineers) It is NOT the purpose of this course to make you learn “how to use MATLAB.” MATLAB is simply the choice of computer language (or software) for you to learn “how to understand and apply computing (design/develop/debug/deploy computer programs) for an effective engineering problem solutions.” The MATLAB (MATrix LABoratory) A special-purpose computer program originally developed to perform engineering and scientific calculations. As the name implies, the software was originally developed for matrix calculations (Algebra/Calculus applications). Today, MATLAB has been grown to be one of the most used engineering/scientific software tools. The origins of MATLAB: https://www.mathworks.com/company/newsletters/articles/the-origins-of-matlab.html

Advantages & Disadvantages of MATLAB MATLAB is an INTERPRETER (no need to compile: means, ease of use), platform independent,

predefined functions, device-independent plotting, and capable for developing a Graphical User Interface (GUI).

MATLAB is an INTERPRETER (no need to compile: means, slower than compiled language) and the software is prohibitively expensive for personal (and academic research) use.

Any solution(s)?: MATLAB Just-In-Time (JIT) Compiler / GNU Octave

Chapter 1Slide 2 of 14

1.1/1.2 The Advantages/Disadvantages of MATLAB

Page 4: Introduction to MATLAB - mercury.pr.erau.edumercury.pr.erau.edu/~hayasd87/EGR115/EGR115_Notes_Chapter_1.pdf · MATLAB is an INTERPRETER (no need to compile: means, ease of use), platform

The MATLAB Environment The fundamental unit of data in MATLAB is the ARRAY: an array is a collection of data values organized into rows & columns (MATRIX) and given a single name. As an interpreter, you can just quickly write up your computer code and run it: no need to compile your code (means: “decode language instructions into command” so that you can execute your developed code in a computer). The MATLAB Desktop The MATLAB desktop is the collection of 4 windows: COMMAND WINDOW, COMMAND HISTORY, CURRENT FOLDER, and WORKSPACE. When you “create” (new) or “open” (import and load an existing file) a MATLAB script (knows as “M-FILE”), a MATLAB EDITOR opens. As you simply “type, save, and run” MATLAB script, you can execute your program (getting a result). MATLAB is an interpreter: no need to compile your program! Just run the program: if the program is good, you’ll get a result. If the program is bad, you’ll get an error. The MATLAB Command Window A user can enter interactive MATLAB commands at the command prompt (>>). The MATLAB accepts commands, interpret/execute it, and then output result(s). For example,

Chapter 1Slide 3 of 14

1.3 The MATLAB Environment

Workspace Browser

shows variables defined in workspace

MATLAB Command Window

Details Windowdisplays properties

of file selected above

MATLAB Editor

Current Folder Browser

shows a list of the files in the current

folder

NOTE: the textbook is written for MATLAB 2014b

The MATLAB Desktop (this is where you begin . . .)

>> diameter = 10

diameter = 10

>> area = (pi/4)*(diameter^2)

area = 78.5398

Command Window

Page 5: Introduction to MATLAB - mercury.pr.erau.edumercury.pr.erau.edu/~hayasd87/EGR115/EGR115_Notes_Chapter_1.pdf · MATLAB is an INTERPRETER (no need to compile: means, ease of use), platform

Dock/Undock Each Individual Window The MATLAB windows can be either docked (to form a desktop) or undocked (to be used as an individual window). Just click the top-right-corner of each window

The PANELS icon (in Quick Access Toolbar) also controls the MATLAB Windows The MATLAB Toolstrip The MATLAB toolstrip conveniently provides you quick access tabs to some key control functions, based on the categorized groups: HOME, PLOTS, APPS, EDITOR, PUBLISH, and VIEW. The most commonly used tab will (most likely) be the EDITOR tab: this is going to be used for developing your own MATLAB script (m-file) by typing the code, open/save the m-file, test run/time/debug the code. The MATLAB Command History The Command History Window appears when you click the PANELS icon (in Quick Access Toolbar: above) or just simply hit “up arrow key” (▲) while you type in the Command Window. Commands remain in the list until they are deleted (it is kept even after you close the program).

Chapter 1Slide 4 of 14

1.3 The MATLAB Environment

MATLAB Toolstrip (selection of tools & commands)

Page 6: Introduction to MATLAB - mercury.pr.erau.edumercury.pr.erau.edu/~hayasd87/EGR115/EGR115_Notes_Chapter_1.pdf · MATLAB is an INTERPRETER (no need to compile: means, ease of use), platform

The MATLAB Script File (“M-File”) Instead of typing commands directly in the Command Window, a series of commands can be placed into a file, and the entire file can be executed by typing its name in the Command Window.

Open a new script and save as “calc_area.m” Type the same commands into the MATLAB Editor (save the file after you’re done typing) Type “calc_area” into MATLAB Command Window

The MATLAB script file (“m-file”) can be created (new) by choosing: (a) new script under “HOME” tab (then, the MATLAB Editor will automatically open) or (if a script is already open in MATLAB Editor):

(b) new => script under “EDITOR” tab (then, a new m-file will be added into the MATLAB Editor).

The MATLAB Editor can be used in “docked” mode (c) or “undocked” (individual window) mode (d). The MATLAB m-file is a simple ASCII text file (.txt): thus, one can even create a MATLAB m-file by using any text editor (i.e., notepad) and can import into MATLAB (file extension must be changed from “.txt” into “.m”). ASCII = “American Standard Code for Information Interchange” (ASCII code is a numerical representation of a character set: computers can only understand binary numbers . . .)

Chapter 1Slide 5 of 14

1.3 The MATLAB Environment

Script (the “M-File”: the program code you’ll develop/run)

a b

c d

>> calc_area

diameter = 10

area = 78.5398

Command Window

Page 7: Introduction to MATLAB - mercury.pr.erau.edumercury.pr.erau.edu/~hayasd87/EGR115/EGR115_Notes_Chapter_1.pdf · MATLAB is an INTERPRETER (no need to compile: means, ease of use), platform

The MATLAB Workspace Using the MATLAB Editor, create a simple script:

Then, run the script in Command Window:

Executing the script will create variables in Workspace, namely: area, radius, and string. A list of variables (ARRAYS) in MATLAB Workspace can be generated with the “whos” command:

Chapter 1Slide 6 of 14

1.3 The MATLAB Environment

MATLAB Workspace (this is where your variables are . . .)

% calc_area2.m

% Simple MATLAB M-File Demo

radius = 2.5;

area = pi * radius^2;

string = [’The area of the circle is ’ num2str(area)];

disp(string);

>> calc_area2

The area of the circle is 19.635

>> whos

Name Size Bytes Class Attributes

Area 1x1 8 double

Radius 1x1 8 double

String 1x32 64 char

Command Window

Editor

Command Window

Page 8: Introduction to MATLAB - mercury.pr.erau.edumercury.pr.erau.edu/~hayasd87/EGR115/EGR115_Notes_Chapter_1.pdf · MATLAB is an INTERPRETER (no need to compile: means, ease of use), platform

The MATLAB Figure Window When a plotting command is executed, MATLAB automatically opens a new window (the FIGURE WINDOW) and completes the plot within the figure window. The strong plotting capability is one of the unique advantages of MATLAB, making this software extremely effective for engineering and scientific problem solutions. (Network\prfsvs01.erau.edu\Courses (T):\EGR115\Hayashibara\EXAMPLES\) example_1_1_sin_x.m

Chapter 1Slide 7 of 14

1.3 The MATLAB Environment

Calculate and plot sin(x) versus x for the range of 0 ≤ x ≤ 6 by using MATLAB script (M-file).

EXAMPLE 1-1

% example_1_1_sin_x.m

%

% This is the very first MATLAB M-File

% Calculation & Plot the function sin(x)

% Range 0 <= x <=6 (increment of 0.1)

%

x = 0:0.1:6

y = sin(x)

plot(x,y)

Page 9: Introduction to MATLAB - mercury.pr.erau.edumercury.pr.erau.edu/~hayasd87/EGR115/EGR115_Notes_Chapter_1.pdf · MATLAB is an INTERPRETER (no need to compile: means, ease of use), platform

(Network\prfsvs01.erau.edu\Courses (T):\EGR115\Hayashibara\EXAMPLES\) example_1_2_cos_x.m

Chapter 1Slide 8 of 14

1.3 The MATLAB Environment

Calculate and plot cos(x) versus x for the range of 0 ≤ x ≤ 6 by using MATLAB script (M-file). Then, can we plot BOTH sin(x) (from example 1-1) and cos(x) on the same figure window?

EXAMPLE 1-2

% example_1_2_cos_x.m

%

% This is the 2nd MATLAB M-File

% Plot the function sin(x) & cos(x)

% Range 0 <= x <=6 (increment of 0.1)

% Then plot BOTH sin(x) & cos(x)

%

clc

clear all

close all

x = 0:0.1:6

y1 = sin(x)

y2 = cos(x)

plot(x,y1,x,y2)

Page 10: Introduction to MATLAB - mercury.pr.erau.edumercury.pr.erau.edu/~hayasd87/EGR115/EGR115_Notes_Chapter_1.pdf · MATLAB is an INTERPRETER (no need to compile: means, ease of use), platform

exercise_1_1_exp_x.m

Chapter 1Slide 9 of 14

1.3 The MATLAB Environment

Do-It-Yourself (DIY) EXERCISE 1-1Develop a MATLAB script (M-File) to plot the function:

0.22 xy x e

for the range of 0 x 10.

Use the MATLAB Editor to create a new empty M-File, type statements, save the file with the name exercise_1_1_exp_x.m. Then, execute the program by typing the M-File name (exercise_1_1_exp_x) in the Command Window.

What results can you get?

% exercise_1_1_exp_x.m

%

. . .

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

Page 11: Introduction to MATLAB - mercury.pr.erau.edumercury.pr.erau.edu/~hayasd87/EGR115/EGR115_Notes_Chapter_1.pdf · MATLAB is an INTERPRETER (no need to compile: means, ease of use), platform

exercise_1_2_log_x.m

Chapter 1Slide 10 of 14

1.3 The MATLAB Environment

Do-It-Yourself (DIY) EXERCISE 1-2Develop a MATLAB script (M-File) to plot the function:

2log 10y x x

for the range of 0 x 10.Then, plot BOTH exp(x) (from exercise 1-1) and log(x) on the same figure window.

% exercise_1_2_log_x.m

%

clc

clear all

close all

. . .

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

Page 12: Introduction to MATLAB - mercury.pr.erau.edumercury.pr.erau.edu/~hayasd87/EGR115/EGR115_Notes_Chapter_1.pdf · MATLAB is an INTERPRETER (no need to compile: means, ease of use), platform

The MATLAB Workspace Browser / Array Editor The MATLAB workspace variables (arrays with m × n size) can be seen in Workspace Browser. After executing example_1_2_cos_x.m, you’ll see:

If you “double click” each variable, you can view/edit actual numerical values allocated within the array (stored in the memory of a computer you are running).

The MATLAB Current Folder Browser and Documentation You can set the “working directory” of your MATLAB session by choosing the current folder. A comprehensive MATLAB documentation can be accessed easily at search box.

Chapter 1Slide 11 of 14

1.3 The MATLAB Environment

MATLAB Current Folder Browser and Documentation

Page 13: Introduction to MATLAB - mercury.pr.erau.edumercury.pr.erau.edu/~hayasd87/EGR115/EGR115_Notes_Chapter_1.pdf · MATLAB is an INTERPRETER (no need to compile: means, ease of use), platform

The MATLAB Set Path As you start working MATLAB project, you’ll find it would be useful to set a particular working directory for quick access. You can do that by HOME => Set Path.

(a) The MATLAB command window takes interactive user command inputs. A user can enter a command at the

command prompt (>>). Then the MATLAB interprets/executes the command and outputs result(s). Instead of typing commands manually, a user can develop a script file (the MATLAB M-File) and execute the script by type the name of the script in the command window. The MATLAB command window is the core component of the MATLAB environment.

The MATLAB Editor allows the user develop (new) and edit (existing) MATLAB scripts (M-Files). The Editor also can let the user test run/time/debug/deploy the script.

The MATLAB figure window allows the plot (2-D/3-D) within the window, upon execution of plot commands.

(b) Use MATLAB help browser (built-in MATLAB documentation)

At the command window, type help command/function name

At the command window, type lookfor command/function name (c) The MATLAB workspace is a part of computer memory used to store variables defined by a user. A list of

variables in the current workspace can be generated by whos command.

You can view/edit actual values stored within each variable by using MATLAB workspace browser and array editor.

(d) clear = clear variables in workspace

clear all = clear all local/global user-defined variables and all functions from the symbol table clc = clear contents of current command window

close all = close all currently open figure window

Chapter 1Slide 12 of 14

1.3 The MATLAB Environment

Do-It-Yourself (DIY) EXERCISE 1-3

Answer the followings (use your own words to explain):

(a) What is the purpose of the MATLAB Command Window? The Edit Window? The Figure Window?

(b) List the different ways that you get help in MATLAB.

(c) What is a workspace? How can you determine what is stored in a MATLAB workplace?

(d) How can you clear the contents of a workspace? How can you clear the current command window? How can you close figure window(s)?

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

Page 14: Introduction to MATLAB - mercury.pr.erau.edumercury.pr.erau.edu/~hayasd87/EGR115/EGR115_Notes_Chapter_1.pdf · MATLAB is an INTERPRETER (no need to compile: means, ease of use), platform

Using the MATLAB Editor, develop simple scripts (m-files):

Then, run the script in Command Window:

Getting Help in MATLAB 3 Ways to get help in MATLAB: (i) Help browser (built-in MATLAB documentation):

The help browser (in Quick Access Toolbar) opens MATLAB documentation (with search options) In command window, type either (ii) help or (iii) lookfor (followed by a specific command/function name) A Few Important MATLAB Commands clc = clear contents of current command window clf = clear contents of current figure window clear = clear variables in workspace clear all = clear all local/global user-defined variables and all functions from the symbol table close all = close all currently open figure window abort (or “CTRL+c”) = terminate the current execution of commands diary = record everything done during a MATLAB session (diary filename)

Chapter 1Slide 13 of 14

1.4 Using MATLAB as a Calculator

EXAMPLE 1-3

(a) Suppose that x = 3 and y = 4. Use MATLAB to evaluate the following expression:

2 2

2

x y

x y

(b) Calculate the volume of a cylinder of radius r (0.1) and length l (0.5) by using MATLAB command window. Note that: Area is A = r2 and Volume is V = Al

% example_1_3a.m

x = 3

y = 4

((x^2)*(y^2))/(x-y)^2

% example_1_3b.m

r = 0.1;

l = 0.5;

area = pi*r^2

volume = area*l

>> example_1_3a.m

x = 3

y = 4

ans = 144

>> example_1_3b.m

area = 0.031416

volume = 0.015708

Editor

Command Window

Editor

Command Window

Page 15: Introduction to MATLAB - mercury.pr.erau.edumercury.pr.erau.edu/~hayasd87/EGR115/EGR115_Notes_Chapter_1.pdf · MATLAB is an INTERPRETER (no need to compile: means, ease of use), platform

exercise_1_4.m

Chapter 1Slide 14 of 14

1.4 Using MATLAB as a Calculator

Do-It-Yourself (DIY) EXERCISE 1-4(a) Suppose that x = 2 and y = 1, evaluate the following expressions using MATLAB: 4 32x 34 2 y

(b) Suppose that u = 2 and v = 3, evaluate the following expressions using MATLAB: 4

3

u

v

2

2

2v

u v

3

3 3

v

v u 24

3v

(c) The distance traveled by a ball falling in the air is given by the equation:

Use MATLAB (save your program code in M-File script) to calculate the position of the ball x (m) at:

Time t = 5 (s)x0 = 10 (m)vo = 15 (m/s)a = 9.81 (m/s2)

20 0.5ox x v t at

% exercise_1_4.m

% (a)

x = 2;

y = -1;

ans_a1 = (2*x^3)^(1/4)

ans_a2 = (2*y^3)^(1/4)

% (b)

u = 2;

v = 3;

ans_b1 = (4*u)/(3*v)

ans_b2 = (2*v^(-2))/((u+v)^2)

ans_b3 = (v^3)/(v^3-u^3)

ans_b4 = (4/3)*pi*v^2

% (c)

t = 5;

x0 = 10;

v0 = 15;

a = -9.81;

ans_c_x = x0+v0*t+0.5*a*t^2

>> exercise_1_4

ans_a1 = 2

ans_a2 = 0.84090 + 0.84090i

ans_b1 = 0.88889

ans_b2 = 0.0088889

ans_b3 = 1.4211

ans_b4 = 37.699

ans_c_x = -37.625

Editor Command Window _________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________

_________________________________________________________