# MATH 442: Mathematical Modeling Getting started with Matlab ```MATH 442: Mathematical Modeling
Lecturer:
Dr. Jean Marie Linhart
http://www.math.tamu.edu/~jmlinhart/m442
Getting started with Matlab
Matlab is short for MATrix LABoratory. Matlab makes it easy to work with
matrices. Matlab assumes if you add (+) or multiply (*), you mean matrix
This is practice and explanation; it is not handed in.
1. Enter the following into Matlab to create a row vector
a = [1 2 3]
2. Enter the following into Matlab to create a column vector
b = [1; 2; 3]
3. You can also create a column vector using the matrix transpose operation,
which you get with a single quote. Try it:
b = [1 2 3]’]
or
b = a’
4. Generalize from the rules above to enter the following matrix into matlab:
1 −2
c=
1 1
5. What happens if you end a line in Matlab with a semi-colon? What is
the difference between
x = [1 0 1]
and
x = [1 0 1];
6. With the variables as defined above, try the two multiplications
a*b
and
b*a
Why do these give different answers? What is happening here?
1
Now try
a*a
a.*a
Now, answer the question: what is the difference between * and .*? The
description we use for .* is elementwise multiplication. Likewise ./
is elementwise division. Addition and subtraction are always elementwise!
7. Functions work elementwise. exp(c) is the exponential function ex and it
works elementwise on c. log(c) takes the natural log or the entries of c.
If you want the base-10 logarithm use log10(c).
8. Ways of creating a vector of evenly spaced values:
t = 0:0.1:10;
t = linspace(0,10,100)
t = linspace(0,10,101)
Which of these are equivalent and can you figure out why?
Use the semicolon to end the line if you don’t want to see all those values
echoed out!
Making this work for trigonometry:
t = 0:4*pi/100:4*pi
t = linspace(0, 4*pi,100
9. Try
x = exp(t);
x = sin(t);
x = cos(t);
Notice that the following does not work. Why not? How do you fix it?
(Hint: elementwise!)
x = sin(t)*cos(t);
10. To make a simple plot, we can simply use the plot(t, x) command.
11. Generally, for class, you need to make multiple plots with titles, axis labels
and legends. Try:
x = exp(t);
figure(2)
plot(t, x, ’k--’, ’LineWidth’, 2)
title(’x= exp(t)’)
xlabel(’t’)
ylabel(’x’)
2
Here is a more complicated figure which plots cosine, sine, and a dotted
line at zero. This figure includes labels, titles and a legend, with the font
increased on the labels and title.
t = linspace(0,3*pi, 100);
x = cos(t);
y = sin(t);
z = zeros(size(t));
figure(3)
plot(t, x, ’k--’, ’LineWidth’, 2)
hold on;
plot(t, y, ’k’, ’LineWidth’, 2)
plot(t, z, ’:k’)
title(’Sine and Cosine’,’FontSize’, 24)
xlabel(’t’, ’FontSize’, 20)
ylabel(’x’, ’FontSize’, 20))
legend(’cos(t)’, ’sin(t)’)
12. Matlab has for loops and if statements like any other programming
language. If I wanted to collect the first 4 odd numbers into a column
called odd:
odd = zeros(4,1)
for i = 1:4
2*i - 1;
end
odd
If I want Matlab to count from 1 to 10, printing out even numbers and
replacing odd numbers with *, I might try the following, which uses the
floor() function, which takes the largest integer less than the one you
put in. There are many other ways to do this.
for i = 1:10
if (i/2 - floor(i/2) &gt; 0)
fprintf(’*, ’)
else fprintf(’%d, ’, i)
end
fprintf(’\n’)
3
```