Professional Documents
Culture Documents
MATLAB CODE
a = 20;
b = 10;
c = 30;
data = [a,b,c];
largest = data(1);
for i = 1:length(data) I
largest = data(i);
end
end
2) Change the Current directory to mynewdir. Then open an Edit Window and add the
following lines
t=-2*pi:pi/10:2*pi
% Calculate |sin(t)|
x=abs(sin(t));
% Plot result
plot(t,x)
3) Think about what the results would be for the following expressions, and then type
them to verify your answers.
• 25 / 4 * 4
• 3+4^2
• 4 \ 12 + 4
• 3 ^ 2 (5 – 2) * 3
• 3456
• 1.0000 1.5000 2.0000 2.5000 3.0000
• 5432
Vectors and matrices are used to store sets of values, all of which are the same type. A vector
can be either a row vector or a column vector. A matrix can be visualized as a table of
values. The dimensions of a matrix are r × c, where r is the number of rows and c is the
number of columns. This is pronounced “r by c.” If a vector has n elements, a row vector
would have the dimensions 1 × n, and a column vector would have the dimensions n × 1.
A scalar (one value) has the dimensions 1 × 1. Therefore, vectors and scalars are actually
just subsets of matrices. Here are some diagrams showing, from left to right, a scalar, a
column vector, a row vector, and a matrix:
A particular element in a vector is accessed using the name of the vector variable and the
element number (or index, or subscript) in parentheses. In MATLAB, the indices start
at 1. Normally, diagrams of vectors and matrices show the indices; for example, for
the variable newvec created earlier the indices 1–10
Use : to indicate all rows for example a(:,1) will display all rows of first column
Use : to indicate all columns for example a(1,:) will display all columns of first column
Display Options
Matlab Command ratio Comments
format short 50.8333 4 decimal digits
format long 50.83333333333334 14 decimal digits
format short e 5.0833e+001 4 decimal digits plus exponent
format long e 5.083333333333334e+001 14 decimal digits plus exponent
format short g 50.8333 better of format short or
format short e (default), switching for ans > 1000
format long g 5.083333333333334e+001 better of format long or format
long e
format bank 50.83 2 decimal digits
format + positive, negative, or zero