Matlab Notes 2 (functions and scripts)


see video writing a program video

creating functions in Matlab


Script files -- are just like executing commands at the prompt

(store in .m file)

accessible variables are the same

You can also have function files (e.g. fact.m)

function y = fact(n)
output function name input args

(actually goes by filename if they differ)

(see sigm.m for example)

subfunctions -- helper functions only available from the function file that contains them

(see sigmm.m for example using a subfunction)

function y=fact(n)
y=prod(1:n)


functions can have multiple input and output arguments

function [x,y,z]=cylinder(r,n)


Functions can not be defined in script files

------------------------------

computing primes (another example of efficiency speedups)

prime1

function p=prime1(n);
p=2
for i=3:n
primeflag=1;
for ptest=p
if i/ptest==round(i/ptest)
primeflag=0;
end
end
if primeflag
p=[p i];
end
end



prime2

function p=prime2(n);
p=2;
for i=3:n
if ~any(rem(i,p)==0)
p=[p,i];
end
end


Sieve of Eratosthenes algorithm
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

Notes to help understand the code below


p will hold the set of primes
code starts by setting p = 1 3 5 ...... n
(no even numbers other than 2 are prime)
then it replaces 1 by 2 (1 is not prime and 2 is)
so now p = 2 3 5 7 9 .....
then it runs an efficient version of the sieve algorithm (see web page above)
Note that for k an odd number > 1
p((k+1)/2) gives you k before any zeroing out happens
likewise
p((k*k+1)/2) gives you k*k
Why does the code start zeroing at k*k instead of 2k or 3k?
(because all odd factors n k for n and even factors have already been ruled out from the start)
Why does the loop code stop at sqrt(n)?
(hint -- read and understand the above)


%%% comments added by me in the printout below

>> type primes

function p = primes(n)
%PRIMES Generate list of prime numbers.
% PRIMES(N) is a row vector of the prime numbers less than or
% equal to N. A prime number is one that has no factors other
% than 1 and itself.
%
% See also FACTOR, ISPRIME.

% Copyright 1984-2003 The MathWorks, Inc.
% $Revision: 1.16.4.1 $ $Date: 2003/05/01 20:43:52 $

if length(n)~=1
error('MATLAB:primes:InputNotScalar', 'N must be a scalar');
end
if n < 2, p = zeros(1,0); return, end
%%%p will hold the set of primes
%%%code starts by setting p = 1 3 5 ...... n
p = 1:2:n;
q = length(p);
%%%then it replaces 1 by 2 (1 is not prime and 2 is)
%%% so now p = 2 3 5 7 9 .....
p(1) = 2;
%%% then it runs an efficient version of the sieve algorithm (see web page above)
for k = 3:2:sqrt(n) %%% Why does the loop code stop at sqrt(n)?
%%% Note that for k an odd number > 1
%%% p((k+1)/2) gives you k before any zeroing out happens
%%% likewise
%%% p((k*k+1)/2) gives you k*k
if p((k+1)/2)
%%% Why does the code start zeroing at k*k instead of 2k or 3k?
p(((k*k+1)/2):k:q) = 0;
end
end
p = p(p>0);



-----------------------
pp = primes(100000);

Graphing data

plotting video
plotting commands video

x=[1 2 4 5 6 7 8 9 10]

y=x.^2


plot(y)


plot(x,y)

color and line type can be specified
plot(x,y,'*')

s
x
d
o
h
<
^
v
>
p
+
.
*

plot(x,y,'-*')

stem(x,y)

to plot several on top of each other use hold on

hold on

plot(y,'.')


clf

histograms -- use hist command

pp=primes(10000)
hist(pp)

You can also easily do 3D graphics

example from your book

f(x,y) = (x^2+3y^2)e^(1-x^2-y^2)


x=[1 2 3]
y=[10 20]

[X,Y]=meshgrid(x,y)

x=-2:0.1:2
y=-2.5:.1:2.5

[X,Y]=meshgrid(x,y);
f=(X.^2+3*Y.^2).*exp(1-X.^2-Y.^2);

mesh(X,Y,f)
grid off
grid on
surf(X,Y,f)
surfc(X,Y,f)
contour(f)



print -djpeg90 filename.jpg
----------------