Important points:
• MATLAB is case sensitive
• On-line HELP is available as follows:
• Array operations take place on an entry-by-entry basis. In the example below, X .* Y is an array operation:
>> X = [2 3 4]; Y = [1 2 3];
>> X .* Y
ans =
2 6 12
• The expression X * Y’ is a matrix operation; it is defined according to the rules of linear algebra. The ' symbol is the transpose operator.
>> X * Y'
ans =
20
• The semicolon separator has two purposes.
You can put multiple MATLAB commands on a single line. This makes M-files
especially compact. At the end of a line, the semicolon supresses the normal
display of the answer you have requested. Examine the difference
between:
| expanded form | abbreviated form |
| >> a=2 | >> a=2; b=3; |
| >> b=3 |
• M-files contain general MATLAB statements. This way, a particular sequence of operations can be pre-programmed and stored. This is very similar to programming in BASIC or other mathematical languages.
• Functions are specific mathematical functions stored in separate files.
• Executing commands can be stopped by entering
control-C
EXAMPLES
1) Simple computations, operators, and functions
| >> 2+2 | >> sin(2*pi*3) |
| >> 2*3-7/4 | >> log (42) * exp(4.4) |
| >> a=3; b = 9 | |
| >> log(a^sqrt(b/2)) | >> 3*a^2+4*b^3 |
2) Working with arrays
Determine which operations below will compute, and why.
>> t
= [ 1 2 3]; u = [ 4 5 6] ; w = [ -2; 1; 4];
>> t
+ u
>> 4
* t - 2 * u
>> t
* u
>> t
.* u
>> v = t'
>> t * v
>> u * w
>> w + 2 * v
>> m = [t; y]
3) Arrays used for function plotting
Define an array of independent variable values: t(1) = 0, t(2) = .1, . . . t(21) = 2
>> t = [0 : .1 : 2 ]
Define an array of dependent values: y(1) = 3*(0)^2 - 4*(0) + 7 = 7, y(2) = etc., ..., y(21) = 3*(2)^2 +4*(2) +7
>> y = 3*t .^ 2 - 4 * t + 7 (Notice the .^ operator)
Plot y as a function of t:
>> plot (t,y); xlabel(‘t’); ylabel(‘y’); title(‘A polynomial plot’)
An alternate way to define the t array
>> t =
linspace(0, 2, 21)
4) Working with complex numbers
>> sqrt(-3)
>> x
= 3+2i
>> real(x);
imag(x)
Determine the roots of the polynomial 1x2 - 2x + 3 = 0
>> roots([1 -2 3])