Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

LAB1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Numerical Computation

Lab ( 1 )
Starting with MATLAB

Name:

Reg No:

Signature:

Objective:

 Get information about Matlab Desktop Environment.


 Working with matrices, arrays, array indexing, character strings, and
workspace variables.
 Calling functions, plotting functions, getting help, and documentation.

Section1: Desktop basics:


The desktop includes these panels:
Current Folder — Access your files.
Command Window — Enter commands at the command line, indicated by the
prompt (>>).
Workspace — Explore data that you create or import from files.

Step1: In windows, create a folder called <Numerical_Computing>, make sure


that no spaces included, and no Arabic or Unicode characters are included in the
path.

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.

Step3: Write the following commands and check workspace panel

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 ↓.

SECTION2: Matrices and Arrays:

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:

To transpose a matrix, use a single quote ('):

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]

write down output:

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

write down the output:

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.

myText = 'Hello, world';

Page 3
Numerical Computation

Step10: You can concatenate strings with square brackets, just as you concatenate numeric arrays.

longText = [myText,' - ',otherText]

write down the output:

Section3: Calling functions, Plotting functions, getting help


and documentation:

Step1: To call a function, such as max, enclose its input arguments in parentheses:

A = [1 3 5];

max(A)

Write down the output

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)

Write down the output

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)

What do you see in the figure? Keep the figure open.

Page 4
Numerical Computation

Step4: You can label the axes and add a title.


xlabel('x')

ylabel('sin(x)')

title('Plot of the Sine Function')

Check the figure?

Step5: To add plots to an existing figure, use hold.

hold on

y2 = cos(x);

plot(x,y2,':')

legend('sin','cos')

How many plots do you see? What colors are they showing?

Step6: Getting help and documentation:

doc mean

help mean

lookfor 'modulation'

Try the above commands. What is the difference between doc and lookfor commands?

Page 5

You might also like