Matlab

From Wiki
Revision as of 22:49, 31 January 2013 by Scott (talk | contribs) (File I/O)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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 quad gives 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
  • clear clears all session variables, clear x clears the variable x
  • whos shows all session variables
  • size(x) shows the dimensions of x
  • clc clears the command window
  • use diary mynotes.txt to append command window input/output to the file mynotes.txt; use diary off to stop appending
  • disp(x) will display the value of x
  • format controls format of numeric output

Built-in scalar functions

  • fix rounds to nearest integer toward zero, floor, ceil
  • log, log10, pow2 (two to the x)
  • realmin, realmax smallest and largest floating point numbers supported
  • rand in the interval [0,1)
  • rem(x,y) remainder when x is divided by y

Vectors

  • create a sequence vector:
  1. x = 1:5 (one through five, inclusive)
  2. x = 1: 0.1: 5 (one through five in steps of 0.1)
  • create an arbitrary vector:
  1. x = [5 2 9 4 8]
  2. x = [5, 2, 9, 4, 8] (may use commas or spaces)
  3. 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

  • clock gives time and date in a six-element vector
  • date gives 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

Troubleshooting

When starting matlab 2012a, I get this error at the command line:

/nfs/pkg64/matlab_2012a/bin/matlab: 1: /nfs/pkg64/matlab_2012a/bin/util/oscheck.sh: /lib64/libc.so.6: not found

Fix it like this:

sudo ln -s /lib/x86_64-linux-gnu/libc-2.15.so /lib64/libc.so.6