26

Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Matlab Basics

K. Cooper

2012

Page 2: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

The usual

The equals sign (=) denotes assignment.

There are prede�ned constants pi and i.

All of the usual special functions are available: sin(),exp(), and so on.

All numbers are double precision �oating point. To seeall digits, use format long.

Expressions are terminated by <Enter>. The semicolonends an entry and suppresses output.

Matlab can do some symbolic math, but you should notuse that much. It excels at �oating point computations.

Page 3: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

The usual

The equals sign (=) denotes assignment.

There are prede�ned constants pi and i.

All of the usual special functions are available: sin(),exp(), and so on.

All numbers are double precision �oating point. To seeall digits, use format long.

Expressions are terminated by <Enter>. The semicolonends an entry and suppresses output.

Matlab can do some symbolic math, but you should notuse that much. It excels at �oating point computations.

Page 4: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

The usual

The equals sign (=) denotes assignment.

There are prede�ned constants pi and i.

All of the usual special functions are available: sin(),exp(), and so on.

All numbers are double precision �oating point. To seeall digits, use format long.

Expressions are terminated by <Enter>. The semicolonends an entry and suppresses output.

Matlab can do some symbolic math, but you should notuse that much. It excels at �oating point computations.

Page 5: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

The usual

The equals sign (=) denotes assignment.

There are prede�ned constants pi and i.

All of the usual special functions are available: sin(),exp(), and so on.

All numbers are double precision �oating point. To seeall digits, use format long.

Expressions are terminated by <Enter>. The semicolonends an entry and suppresses output.

Matlab can do some symbolic math, but you should notuse that much. It excels at �oating point computations.

Page 6: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

The usual

The equals sign (=) denotes assignment.

There are prede�ned constants pi and i.

All of the usual special functions are available: sin(),exp(), and so on.

All numbers are double precision �oating point. To seeall digits, use format long.

Expressions are terminated by <Enter>. The semicolonends an entry and suppresses output.

Matlab can do some symbolic math, but you should notuse that much. It excels at �oating point computations.

Page 7: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

Matrices

Matrices are speci�ed using brackets []. E.g. x=[1 2 3]

gives a row vector x = (1, 2, 3).Rows of a matrix can be separated by semicolons. E.g.y=[1;2;3] gives

y =

123

All array objects are treated as matrices.

x' is the transpose of x.

Page 8: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

Arithmetic

All standard operations are understood in a matrixcontext.

Elementwise operations use a period before the binaryoperator.

Functions can operate elementwise on matrices.

Page 9: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

Arithmetic

All standard operations are understood in a matrixcontext.

Elementwise operations use a period before the binaryoperator.

Functions can operate elementwise on matrices.

Page 10: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

Arithmetic

All standard operations are understood in a matrixcontext.

Elementwise operations use a period before the binaryoperator.

Functions can operate elementwise on matrices.

Page 11: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

Arithmetic

All standard operations are understood in a matrixcontext.

Elementwise operations use a period before the binaryoperator.

Functions can operate elementwise on matrices.

x=[1,2,3]; y=[1;2;3];

x*y gives 14.

y*x gives

1 2 32 4 63 6 9

x.*y' gives (1, 4, 9).sin(x) gives (0.8415, 0.9093, 0.1411).

Page 12: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

Matrix Creation

Given the number m of rows and n of columns:

zeros(m,n) makes a matrix of zeros.

ones(m,n) makes a matrix of ones.

rand(m,n) makes a matrix of uniformly distributedpseudorandom numbers in [0, 1].eye(m) makes an m×m identity matrix.

Page 13: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

Ranges

The sequential integers from m to M are denoted m:M

in Matlab.

The integers from m to M stepping by k are obtainedusing m:k:M

Unlike Maple, Matlab actually creates a vectorcontaining the numbers in the range speci�ed, so itmakes sense to set e.g. x = 0:.2:4.

Page 14: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

Matrix elements and submatrices

We can get the i, jth element of a matrix A usingA(i,j).

We can get a submatrix of A using ranges: e.g.B=A(1:4,2:3)

To get a whole column, use a colon without limits:v=A(:,3) gives the third column of A.

Page 15: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

Plots

Plots are made by creating two vectors: one of abscissae, sayx, and the other of ordinates, say y. Then useplot(x,y).

You can specify colors and dots/dashes using an optionalcharacter string third argument: e.g. 'g�' to make a greendashed curve.

You can put as many of these pairs/triplets in the plotcommand as you like.

Page 16: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

Plots

Plots are made by creating two vectors: one of abscissae, sayx, and the other of ordinates, say y. Then useplot(x,y).

You can specify colors and dots/dashes using an optionalcharacter string third argument: e.g. 'g�' to make a greendashed curve.

You can put as many of these pairs/triplets in the plotcommand as you like.

Page 17: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

Plots

Plots are made by creating two vectors: one of abscissae, sayx, and the other of ordinates, say y. Then useplot(x,y).

You can specify colors and dots/dashes using an optionalcharacter string third argument: e.g. 'g�' to make a greendashed curve.

You can put as many of these pairs/triplets in the plotcommand as you like.

Page 18: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

Character Strings

Character strings are delimited using single quotes ( ' ).

Character strings are stored as row matrices ofcharacters: 'Hello' == ['H' 'e' 'l' 'l' 'o'].

There is a sprintf function to insert numeric variablesinto character strings.

Page 19: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

Character Strings

Character strings are delimited using single quotes ( ' ).

Character strings are stored as row matrices ofcharacters: 'Hello' == ['H' 'e' 'l' 'l' 'o'].

There is a sprintf function to insert numeric variablesinto character strings.

Page 20: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

Character Strings

Character strings are delimited using single quotes ( ' ).

Character strings are stored as row matrices ofcharacters: 'Hello' == ['H' 'e' 'l' 'l' 'o'].

There is a sprintf function to insert numeric variablesinto character strings.

Page 21: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

Character Strings

Character strings are delimited using single quotes ( ' ).

Character strings are stored as row matrices ofcharacters: 'Hello' == ['H' 'e' 'l' 'l' 'o'].

There is a sprintf function to insert numeric variablesinto character strings.

str=sprintf('Soln for %d points, h=%f',np,1/np);

Page 22: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

I/O

Use csvread('filename') to read a comma-separatevalue �le

Use dlmread('filename','delimiter') to read text�le using a delimiter other than a comma.

There are csvwrite() and dlmwrite() functions

You can save all variables in a session at any time usingsave <name>

You can load those variables back using load <name>

Page 23: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

Conditional constructs

if cond1 expr1; elseif cond2 expr2; else expr3;

end;

if a==15 a=1; elseif a>0 a=-2; else a=0; end;

Of course, one can always leave o� the elseif or else, and canput as many statements as one likes in place of theexpressions.

Page 24: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

Conditional constructs

if cond1 expr1; elseif cond2 expr2; else expr3;

end;

if a==15 a=1; elseif a>0 a=-2; else a=0; end;

Of course, one can always leave o� the elseif or else, and canput as many statements as one likes in place of theexpressions.

Page 25: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

Conditional constructs

if cond1 expr1; elseif cond2 expr2; else expr3;

end;

if a==15 a=1; elseif a>0 a=-2; else a=0; end;

Of course, one can always leave o� the elseif or else, and canput as many statements as one likes in place of theexpressions.

Page 26: Matlab Basics - Washington State UniversityMatlab Basics K. Cooper 2012. Maple Syntax The usual The equals sign (=) denotes assignment . There are prede ned constants pi and i . All

Maple

Syntax

Conditionals

< is �less than�

<= is �less than or equal to�

Analogously for greater than

== is �test for equality�

� = is �test for inequality�

& denotes �and�; | denotes �or�.

�False� is represented by 0; any nonzero can beconsidered �true�, but the default value of �true� is 1.