LAB1
LAB1
LAB1
Lab ( 1 )
Starting with MATLAB
Name:
Reg No:
Signature:
Objective:
Step2: In Matlab, go to current folder panel and select the current folder as the
folder you have already created. Now, every function name or script will be
referenced to the current folder so it preferable to do it before any further work
begins.
a=1
b=2
c=a+b
d = cos(a)
Page 1
Numerical Computation
If you end a statement with a semicolon, MATLAB performs the computation, but suppresses the display
of output in the Command Window.
E = a * b;
You can recall previous commands by pressing the up- and down-arrow keys, ↑ and ↓.
Step1: To create an array with four elements in a single row, separate the elements with either a
comma (,) or a space. This type of array is a row vector.
a = [1 2 3 4]
Write down the output.
Step2: Create a matrix that has multiple rows, separate the rows with semicolons.
a = [1 2 3; 4 5 6; 7 8 10]
Write down the output:
Step3: Process all of the values in a matrix using a single arithmetic operator or function.
a + 10
sin(a)
Write down the output:
a'
Step4: You can perform standard matrix multiplication, which computes the inner products
between rows and columns, using the * operator. For example, confirm that a matrix times its
inverse returns the identity matrix:
p = a*inv(a)
write down the output:
Step5: To perform element-wise multiplication rather than matrix multiplication, use the .*
operator:
p = a.*a
write down the output:
Page 2
Numerical Computation
Step6 : Concatenation is the process of joining arrays to make larger ones. In fact, you made your
first array by concatenating its individual elements. The pair of square brackets [] is the
concatenation operator.
A = [a,a]
write down output.
Concatenating arrays next to one another using commas is called horizontal concatenation. Each
array must have the same number of rows. Similarly, when the arrays have the same number of
columns, you can concatenate vertically using semicolons.
A = [a ; a]
Step7: Every variable in MATLAB® is an array that can hold many numbers. When you want to
access selected elements of an array, use indexing.
A = magic(4)
A(4,2)
write down output.
Step8 : To refer to multiple elements of an array, use the colon operator, which allows you to specify
a range of the form start:end For example, list the elements in the first three rows and the second
column of A:
A(1:3,2)
The colon alone, without start or end values, specifies all of the elements in that dimension. For
example, select all the columns in the third row of A:
A(3,:)
The colon operator also allows you to create an equally spaced vector of values using the more
general form start:step:end
B = 0:10:100
Note: If you omit the middle step, as in start:end, MATLAB uses the default step value of 1.
Step9: A character string is a sequence of any number of characters enclosed in single quotes.
You can assign a string to a variable.
Page 3
Numerical Computation
Step10: You can concatenate strings with square brackets, just as you concatenate numeric arrays.
Step1: To call a function, such as max, enclose its input arguments in parentheses:
A = [1 3 5];
max(A)
Step2: Return output from a function by assigning it to a variable, When there are multiple output
arguments, enclose them in square brackets:
maxA = max(A)
[maxA,location] = max(A)
Step3: To create two-dimensional line plots, use the plot function. For example, plot the value of the
sine function from 0 to pi:
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
Page 4
Numerical Computation
ylabel('sin(x)')
hold on
y2 = cos(x);
plot(x,y2,':')
legend('sin','cos')
How many plots do you see? What colors are they showing?
doc mean
help mean
lookfor 'modulation'
Try the above commands. What is the difference between doc and lookfor commands?
Page 5