Docsity

MATLAB Tutorial 1-Machine Learning-Lecture Handout, Exercises of Machine Learning

Partial preview of the text

Download MATLAB Tutorial 1-Machine Learning-Lecture Handout and more Exercises Machine Learning in PDF only on Docsity!

Dr. Hanif Durad

Lecture Outline (1/2)

^ Basic MATLAB Operations^ 

Starting MATLAB  Using MATLAB as a calculator  Introduction to variables and functions

^ Matrices and Vectors: All variables are matrices.^ 

Creating matrices and vectors  Subscript notation  Colon notation

Dr. Hanif Durad

Lecture Outline (2/2)

^ Additional Types of Variables^ ^

Complex numbers  Strings  Polynomials

^ Working with Matrices and Vectors^ ^

Some basic linear algebra  Vectorized operations  Array operators

^ Managing the Interactive Environment ^ Plotting

MATLAB Desktop (1/2)

MATLAB Desktop (2/2)  The desktop provides different ways ofinteracting with MATLAB ^ Entering commands in the command window ^ Viewing values stored in variables ^ Editing statements in MATLAB functions andscripts ^ Creating and annotating plots

Dr. Hanif Durad

MATLAB as a Calculator (2/2)

^ Define and use variables>> a = 5a =^5 >> b = 6b =^6 >> c = b/ac =1.

Dr. Hanif Durad

Built-in Variables (1/2)

pi (=

π) and ans are a built-in variables

>> pians =3.1416>> sin(ans/4)ans =0.7071Note: There is no “degrees” mode. All angles are measuredin radians.

Dr. Hanif Durad

Built-in Functions

^ Many standard mathematical functions, such as sin, cos, log, andlog10, are built-in>> log(256)

log(x) computes the natural logarithm of x

ans =5.5452>> log10(256)

log10(x) is the base 10 logarithm

ans =2.4082>> log2(256)

log2(x) is the base 2 logarithm

ans = 8

Ways to Get Help

Dr. Hanif Durad

^ Use on-line help to request info on a specific function>> help sqrt ^ In MATLAB version 6 and later the doc functionopens the on-line version of the manual. This is veryhelpful for more complex commands.>> doc plot ^ Use lookfor to find functions by keywords>> lookfor functionName

^ The help browser opens when you type a doc command:>> doc plot

On-line Help (2/2)

Looking for Functions

Dr. Hanif Durad

^ Syntax:lookfor stringsearches first line of function descriptions for “string”.Example:>> lookfor cosineproducesACOS

Inverse cosine. ACOSH

Inverse hyperbolic cosine. COS

Cosine. COSH

Hyperbolic cosine.

Suppress Output with Semicolon(1/2)

Dr. Hanif Durad

^ Results of intermediate steps can be suppressed withsemicolons.Example: Assign values to x, y, and z, but only displaythe value of z in the command window:>> x = 5;>> y = sqrt(59);>> z = log(y) + x^0.

z =3.

Dr. Hanif Durad

^ Type variable name and omit the semicolon to print the value ofa variable (that is already defined)>> x = 5;>> y = sqrt(59);>> z = log(y) + x^0.25z =3.5341>> yy =7.

( = log(sqrt(59)) + 5^0.25 ) Suppress Output with Semicolon(2/2)

MATLAB Variables Names

Legal variable names:^ ^

Begin with one of a–z or A–Z  Have remaining characters chosen from a–z, A–Z, 0–9, or  Have a maximum length of 31 characters  Should not be the name of a built-in variable, built-in function, or  user-defined function

^ Examples:^ ^

xxxxxxxxx  pipeRadius  widgets_per_box  mySum  mysum

^ Note: mySum and mysum are differentvariables. MATLAB is case sensitive.

Built-in MATLAB Variables(1/2)

Dr. Hanif Durad

Name

Meaning ans^

value of an expression when that expression is not assignedto a variable eps^

floating point precision pi^

π, (3.141492.. .) realmax

largest positive floating point number realmin

smallest positive floating point number Inf^

∞, a number larger than realmax, the result of evaluating 1/0. NaN^

not a number, the result of evaluating 0/