BASIC (Page 3)
| COMMAND | What it Does |
|---|---|
x = zeros(100,2);
| Makes x a 100 row by 2 column matrix
with all zeros
|
y = ones(20,20);
| Makes y a 20 row by 20 column matrix
with all ones
|
z = 0:.1:10;
| Puts the values of 0,.1,.2,...10
into the variable z
|
a = z.^2;
| The .^2 operator will take each individual element
of z and square it, so a will contain
0,.01,.04,...100
|
b = a.*z;
| The .* operator will take each individual element
of a and multiply it by the same element in
z. The equivalent code in BASIC would befor i=1 to 101 b(i) = a(i)*z(i)next i
|
C = rand(3,3); D = ones(3,3); E = C*D; |
Consider a ball dropped from a window of a tall building and another ball thrown downwards at 2.5 m/s from a window 1 floor (3 meters) higher up. This program will plot the heights of the two balls as a function of time.
t = 0:.01:2;
g = -9.8;
y1 = 0.5*g*t.^2;
y2 = 0.5*g*t.^2 - 2.5*t + 3;
plot (t,y1,'g-');
plot (t,y2,'r.');
xlabel ('Time (Seconds)');
ylabel ('Distance Fallen (Meters)');
title ('Height of Falling Objects');
http://physics.gac.edu/~huber/matlab/mtlbasi3.html