Matlab: Difference between revisions
Jump to navigation
Jump to search
(No difference)
|
Revision as of 23:22, 31 January 2011
Operators
- 2\1 is equivalent to 1/2
psi = C .* 1./(1+(x./a).^2);
- The dot denotes pair-by-pair operation for vectors
- Technically, you only need dots in front of ^ operators for which the first argument might be a vector, and * and / operators for which the quantities on each side of the operator could both be vectors. The MATLAB function
quadgives a vector argument to the function it is integrating, so vectors may crop up when you don't expect them. The safest thing is to put a dot in front of every *, /, and ^ operator in all your functions. Otherwise, you may get hard-to-understand error messages.
Working at the command line
- semicolon at end of line prevents result from being displayed
clearclears all session variables,clear xclears the variable xwhosshows all session variablessize(x)shows the dimensions of xclcclears the command window- use
diary mynotes.txtto append command window input/output to the file mynotes.txt; usediary offto stop appending disp(x)will display the value of xformatcontrols format of numeric output
Built-in scalar functions
fixrounds to nearest integer toward zero,floor,ceillog, log10, pow2(two to the x)realmin, realmaxsmallest and largest floating point numbers supportedrandin the interval [0,1)rem(x,y)remainder when x is divided by y
Vectors
- create a sequence vector:
x = 1:5(one through five, inclusive)- x = 1: 0.1: 5 (one through five in steps of 0.1)
- create an arbitrary vector:
- x = [5 2 9 4 8]
- x = [5, 2, 9, 4, 8] (may use commas or spaces)
- y = [1 x 1] (has 7 elements)
- multiply:
y = 2*x - add:
x + y - get the first element:
x(1) - get the first three elements:
x(1:3) - column vector using transpose:
y = x' - append an element:
x = [ x 3 ] - remove an element
x(3) = [] - built-in vector functions:
cumsum(cumulative sum),length,max,mean,min,prod,sum
Matrices
x = [1 2 3; 4 5 6]
x(1, 2) (gives 2)
x' % transpose
x(2, :) % gives [4 5 6]
a = [1 2; 3 4], b = a'
a * b % matrix multiplication give [5 11; 11 25]
a .* b % element-by-element multiplication gives [1 6; 6 16]
Control Flow
for i = 1:5, disp(i), end
for i = 1:6
x = (x + a / x) / 2;
disp( x )
end
if x == 0, disp( ’x equals zero’), end
if bal < 5000
rate = 0.09;
elseif bal < 10000
rate = 0.12;
else
rate = 0.15;
end
d = floor(3*rand) + 1
switch d
case 1
disp( ’That’’s a 1!’ );
case 2
disp( ’That’’s a 2!’ );
otherwise
disp( ’Must be 3!’ );
end
Functions
The existence of a file on disk called stat.m containing this code defines a new function called stat that calculates the mean and standard deviation of a vector:
function [mean,stdev] = stat(x)
n = length(x);
mean = sum(x)/n;
stdev = sqrt(sum((x-mean).^2/n));
Call the function, supplying two output variables on the left side of the equation:
[mean stdev] = stat([12.7 45.4 98.9 26.6 53/1])
mean =
47.3200
stdev =
29.4085
Timing
start = clock;
.. do stuff ..
elapsed = etime(clock, start)
or equivalently
tic
..do stuff..
toc % prints out elapsed time
Date and Time
clockgives time and date in a six-element vectordategives a string like 02-Feb-2011
Plotting
x = 1:0.1:5
plot(x, sin(x)), grid
ezplot('sin(x)')
Plot two functions simultaneously:
fplot(@(x) [sin(x),cos(x)],[0,2*pi,-2,2])
Printing
To print some text:
disp( [’The answer is ’, num2str(x)] );
fprintf( ’Interest rate: %6.3f New balance: %8.2f\n’, rate, balance);
File I/O
save mydata.txt myVar -ascii
myVar = load('mydata.txt')
also
save mydata x y z % saves to mydata.mat
load mydata
may also use csvread, csvwrite, dlmread, dlmwrite, textread, xlsread