Contents

% scripts:
%
% Why write them?
%  - keep your code for repetitive/future use
%  - helpful when you are writing more complex code
%
% Topics
% - use comments, semicolon at end of line
% - conditional statements: if, then
% - indentation conventions
% - loops: for, while
% - naming conventions for variables and scripts

Scripting & plotting

% Example script: plot two randomly generated 2-vectors
%
x = rand(2,1);
y = rand(2,1);
% Homework common mistakes: plotting two vectors, and their sum
plot(x,y);

plot(x,y,'o');

origin = [0 0];
figure;
plot([origin(1) x(1)], [origin(2) x(2)],'b-');
hold on;
plot([origin(1) y(1)], [origin(2) y(2)],'b-');

xysum = x + y;
plot([origin(1) xysum(1)], [origin(2) xysum(2)],'r-');

% force axes to be ( [0,3] , [0,3] )
axis([0 3 0 3]);
% label axes and title!
xlabel('x-component');
ylabel('y-component');
title('Two randomly generated vectors (blue), and their sum (red)');


% works in 3d
c = [1 2 1];
figure;
plot3([0 c(1)], [0 c(2)], [0 c(3)], 'b-')
grid on;


% A Circle
figure;
% for i = 1:1000
% 	hold on
% 	x = (rand(1,2)-.5)*2;
% 	x = x/norm(x);
% 	plot([0 x(1)],[0 x(2)])
% end
x = linspace(0,2*pi,100);
plot(cos(x),sin(x))
axis equal

Conditionals review

c == 2
any(c == 2)
disp('Hello!')

% IF
if any(c==2)
disp('c does contain an element equal to 2!');
end

if any(c==2)
disp('c does contain an element equal to 2!');
else
disp('no elements in c equal to 2');
end

c = [5 5 5 7]; % rerun above with this

x = 2;
switch x
	case 0
		disp('x is zero');
	case 1
		disp('x is one');
	case 2
		disp('x is two');
	otherwise
		disp('I dont know what x is!!');
end
ans =

     0     1     0


ans =

     1

Hello!
c does contain an element equal to 2!
c does contain an element equal to 2!
x is two

FOR loops

for a = 1:5 % don't set a within the loop (in MATLAB)
2*a
end

b = 0;
for a = 1:5
b = b + 1
end

% How to compute dot product for 10-element vector, using a for-loop?
x = rand(10,1);
y = rand(10,1);

d_product = 0;
%xlength = length(x);
for a = 1:10%xlength
xa_ya_prod = x(a) * y(a);
d_product = d_product + xa_ya_prod;
end

% How to visualize 10-vector?
figure;
stem(x);

% WHILE LOOP
a = 0;
while a <= 5
b = 2*a
a = a + 1;
end
ans =

     2


ans =

     4


ans =

     6


ans =

     8


ans =

    10


b =

     1


b =

     2


b =

     3


b =

     4


b =

     5


b =

     0


b =

     2


b =

     4


b =

     6


b =

     8


b =

    10

Dot product demo

% Dot product, slow way
x = ones(1,100000);
y = ones(100000,1);

fprintf('\nFor loop\n');
tic % begin timing 
d_product = 0;
v_length = length(x);
for i = 1:v_length
	 d_product = d_product + x(i)*y(i);
end
toc % end timing 
%  Note: use tic / toc around any code segment to time it! 

% Dot product, stupid way
fprintf('\nWhile loop\n');
tic
i = 1;
while i <= length(x)
% while 1
% 	if i > length(x)
% 		break
% 	end
	 d_product = d_product + x(i)*y(i);
	 i = i+1;
end
toc

fprintf('\nx*y\n');
tic
d_product = x*y;
toc

fprintf('\ndot(x,y)\n');
tic
d_product = dot(x,y);
toc

fprintf('\nsum(x.*y'')\n');
tic
d_product = sum(x.*y');
toc
For loop
Elapsed time is 0.002035 seconds.

While loop
Elapsed time is 0.003227 seconds.

x*y
Elapsed time is 0.000325 seconds.

dot(x,y)
Elapsed time is 0.000498 seconds.

sum(x.*y')
Elapsed time is 0.000767 seconds.