matlab basics - washington state universitymatlab basics k. cooper 2012. maple syntax the usual the...
TRANSCRIPT
Maple
Matlab Basics
K. Cooper
2012
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.
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.
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.
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.
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.
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.
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.
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.
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.
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).
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.
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.
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.
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.
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.
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.
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.
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.
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.
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);
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>
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.
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.
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.
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.