Comparison: Matlab Scripts & BASIC (Page 3)

There are many matrix functions in Matlab that are not available at all in BASIC. Some of the more useful ones are shown below.
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 be
for i=1 to 101
b(i) = a(i)*z(i)
next i
C = rand(3,3);
D = ones(3,3);
E = C*D;
The first command makes C a 3-by-3 matrix of random numbers between 0 and 1. Matrix D will be all ones, and the matrix E will be the matrix product of these two matrices (so each element of the top row of E will be the sum of the first row of C.

The following code shows some powerful things we can do with Matlab graphics (graphics are non-standard in BASIC).

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');

Previous Page
NOTE: This document uses HTML 3.0 Extensions. It must be viewed with an appropriate browser (such as Netscape Version 2 or higher)
Electronic Copy:
Created: 20-JUL-96 by Tom Huber, Physics Department, Gustavus Adolphus College.