MATLAB(motivation)an array ‘a’ with 1 element, which turns out to have a value of 10 (integer...

13
MATLAB(motivation) Why use MATLAB? Mathematcal computations Used a lot for problem solving Statistical Analysis (e.g., mean, min) Visualisation (1D-3D) Signal processing (Fourier transform, etc.) Image processing (e.g., animations) Pattern recognition

Transcript of MATLAB(motivation)an array ‘a’ with 1 element, which turns out to have a value of 10 (integer...

Page 1: MATLAB(motivation)an array ‘a’ with 1 element, which turns out to have a value of 10 (integer type) What about i= 3+5i, or i=5+6j? Scalar: array with 1 element Vector:anarraywith1dimension

MATLAB(motivation)

Why use MATLAB?

Mathematcal computations

Used a lot for problem solving

Statistical Analysis (e.g., mean, min)

Visualisation (1D-3D)

Signal processing (Fourier transform, etc.)

Image processing (e.g., animations)

Pattern recognition…

Page 2: MATLAB(motivation)an array ‘a’ with 1 element, which turns out to have a value of 10 (integer type) What about i= 3+5i, or i=5+6j? Scalar: array with 1 element Vector:anarraywith1dimension

P velocity isosurface (-1% and +1%)

2. Lithosphere integrity and growth

Page 3: MATLAB(motivation)an array ‘a’ with 1 element, which turns out to have a value of 10 (integer type) What about i= 3+5i, or i=5+6j? Scalar: array with 1 element Vector:anarraywith1dimension

Starting Matlab

Free Access: Where to download

• https://ualberta.onthehub.com/

• among the products, find ’mathematics’

• Find the appropriate version and download using student CCID

. When installing, only one of the installing options (e.g., not using a license) works. I also suggest that all the items are installed (by default only a few are selected among many different packages).

Page 4: MATLAB(motivation)an array ‘a’ with 1 element, which turns out to have a value of 10 (integer type) What about i= 3+5i, or i=5+6j? Scalar: array with 1 element Vector:anarraywith1dimension

Starting Matlab

Double click on the Matlab icon, or on unixsystems type “matlab” at the command line.

• After startup Matlab displays a command window that is used to enter commands and display results.

• Enter Commands at the command prompt:

>> 3+4

for full version version

• Matlab responds to commands by printing text in the command window, or by opening a figure window for graphical output.

• Toggle between windows by clicking on them with the mouse.

Page 5: MATLAB(motivation)an array ‘a’ with 1 element, which turns out to have a value of 10 (integer type) What about i= 3+5i, or i=5+6j? Scalar: array with 1 element Vector:anarraywith1dimension

Matlab screen of R2018bLaunch Pad/Workspace: Used to browse documentation, or view values of variables in the workspace

Command History window: keeps tracks of earlier commands (why? Ex: allows you to put them into a script file if you want later, or re-execute some or a block of them).

Details window: shows the detail of the files you select under the ‘Current Folder’ above. A keypurpose is to see the first line of a M-file (which is why you should have comments).

File browserFor the currentfolder

Command window: where your commands are inputand excuted.

Page 6: MATLAB(motivation)an array ‘a’ with 1 element, which turns out to have a value of 10 (integer type) What about i= 3+5i, or i=5+6j? Scalar: array with 1 element Vector:anarraywith1dimension

Simple usages (e.g., Calculator)

•Enter formula at the commandprompt

• mathematical symbols:

Add: + subtract: -Multiply: *Divide: /Exponent: ^

>> 10+2*3

ans =

16

>> (105-32)*5/9

ans =

40.5556 (yes, med time!)

•Built-in functions(provided by MATLAB)

sin() cosh() log log10. sqrt() exp() …

Page 7: MATLAB(motivation)an array ‘a’ with 1 element, which turns out to have a value of 10 (integer type) What about i= 3+5i, or i=5+6j? Scalar: array with 1 element Vector:anarraywith1dimension

Getting helpMany ways to get help

help sinlog Natural logarithm.

log(X) is the natural logarithm of the elements of X.Complex results are produced if X is not positive.

See also log1p, log2, log10, exp, logm, reallog.

Reference page for logOther functions named log

lookfor sinFind all functions that contain ‘sin’

whichTells you where function sin() is defined

edit log10Provides details of log10 above the command window

Press the fx icon to the left of >>Function browser by categories

Page 8: MATLAB(motivation)an array ‘a’ with 1 element, which turns out to have a value of 10 (integer type) What about i= 3+5i, or i=5+6j? Scalar: array with 1 element Vector:anarraywith1dimension

Define variables

>> a =3;

Prevents printouts

•Workspace

•User defined variables (case sensitive)

>> a = 10; % Overwritten>> b = sin(a); %. Both are doubles without type cast!

Page 9: MATLAB(motivation)an array ‘a’ with 1 element, which turns out to have a value of 10 (integer type) What about i= 3+5i, or i=5+6j? Scalar: array with 1 element Vector:anarraywith1dimension

•Built-in variables

Define variables

e.g. pi i j

• Intrinsically, variables in MATLAB arevectors and matrices!

Every variable is an array!

By defining a=10, we are essentially defining an array ‘a’ with 1 element, which turns out to have a value of 10 (integer type)

What about i = 3+5i, or i=5+6j?

Scalar: array with 1 element

Vector: an array with 1 dimension ---à a sequence of elements, i.e., a matrix with 1 row or 1 column

>> a = [1, 2, 3]>> a = [1 2 3]

>> a = [1; 2; 3]

Row vector

column vector

Or a= 3+5i; b=3; a+b? a+b*i ?

Page 10: MATLAB(motivation)an array ‘a’ with 1 element, which turns out to have a value of 10 (integer type) What about i= 3+5i, or i=5+6j? Scalar: array with 1 element Vector:anarraywith1dimension

Saving your work

• As scripts (M-files, with .m extension such as mytest.m), can be done with GUI or by highlighting commands from ‘command history’ window using the right mouse click.• Remove clutter on the screen by>> clc % does not affect Workspace and variablesOr>>clear %actually removes everything from Workspace and all variable definitions and answers

• As function scripts (M-files, more self contained and variables inside functions, which are not visible to the other codes or workspace unless defined as global). Use GUI by clicking on the right triangular icon and ‘save’ or save(‘mytest.mat’) with options like save(‘mytest.mat’, ‘a’)

• MAT files (saved workspace and all the global variables only, including arrays and answers)

• Up arrow cycles through your earlier commands

• Executing by loading via MATLAB GUI

Page 11: MATLAB(motivation)an array ‘a’ with 1 element, which turns out to have a value of 10 (integer type) What about i= 3+5i, or i=5+6j? Scalar: array with 1 element Vector:anarraywith1dimension

The working directory is controlled by>> dir

>> cd catalogue

>> pwd

The path variable defines where matlab searches for m-files

>> path

>> addpath

>> pathtool

>> which function

Page 12: MATLAB(motivation)an array ‘a’ with 1 element, which turns out to have a value of 10 (integer type) What about i= 3+5i, or i=5+6j? Scalar: array with 1 element Vector:anarraywith1dimension

Flow control - selection

• Syntax:if <logicalexpression>

<commands>elseif <logicalexpression>

<commands>else

<commands>end

height =185;if height > 180

disp(’tall, for meanyway’)elseif height<150

disp(’small’)else

disp(’average’)end

Example:

Page 13: MATLAB(motivation)an array ‘a’ with 1 element, which turns out to have a value of 10 (integer type) What about i= 3+5i, or i=5+6j? Scalar: array with 1 element Vector:anarraywith1dimension

Logical expressions• Relational operators• == (equal to) ~= (not equal)

< (less than) <= (less than or equal to)> (greater than) >= (greater than or equal to)

• Logical operators & (and)| (or)~ (not)

• Logical functionsxorisemptyanyall

X=20;

if (x>=0) & (x<=10)disp(‘x is in range from 0

to 10’)elsedisp(‘x is greater than 10’)

end

x iS greater than 10

Example: