Matlab Tutorial 1: Hello world, plotting, mathematical functions and file types


Each and every element in A is accessible, by using indices:
A(row_index, column_index).
What’s the answer to the following ?

>> A(2,1)+A(1,3)+A(2,3)

Matrices in Matlab does not necessarily only contain real numbers. They can also be complex. We can even have matrices with text as elements. If there is only one row in the matrix => we have a row vector. Only one column in the matrix => a column vector. And finally the special case: a matrix 1×1 is nothing else than a scalar.
What will happen by doing the following?

>> A(1,1)=12

Try to figure out how the commands size and length works!

Exercise 7: Predefined variables in Matlab

Matlab has some own predefined variables aside from the variables that we make ourselves. You do not need to specify what kind of variables you have introduced. There is no need for any declaration. Matlab separates capital and small letters. No variables may begin with a number, underscore or non-english letters like å, ä and ö. Variables which are numbers are stored in matlab like a float with double precision. The class will be double. Since a computer can not store variables with infinetely high representation we are always limited by the precision of the storage. Predefined variables in Matlab:

ans: The value of the last calculated expression.
eps: Gives computer precision, distance between 1 and closest float.
pi: 3.141592653589793
inf: Defines infinity, defined as 1/0.
i,j: Gives the imaginary unit. Defined as squareroot of -1.
nan: An unidentified number ( Not a Number ).
realmax: Gives the highest float which can be represented by the computer.
realmin: Gives the smallest float which can be represented by the computer.

Write clear in matlabs command window to clear all the variables in the Workspace. Do the following assignment:

>> a=2, b=3

What will be the result of a+b? Of course it becomes 5, but please observe where the result is stored, in the temporary variable ans. This is always true if I make a calculation without any assignment to store the result in.

Write 34+2 Now, ans has a new value. Check eps!

Calculate 1/0+3! What is the answer?

Check the largest and smallest value represented in Matlab!

Multiply the answers by 2. What happens?

Divide ans by ans. Matlab can’t represent this with a number instead it answers NaN.

Working with Complex Numbers in Matlab

Finally let us define complex numbers. Calculate the following:

>> sqrt(-1) % squareroot of -1

We can also write a complex variable as:

>> z=2+3*i % complex variable z: real part 2 and an imaginary part 3.

Try some of the built-in functions in matlab operating on complex numbers:

complex(x,y): Gives a complex number with real- and imaginary-part.
conj(z): Gives the conjugate of z.
real(z): Gives real part of z.
imag(z): Gives imaginary part of z.
abs(z): Gives the absolute value of z.
angle(z): Gives the phase angle of the complex number z.

Observe! There is nothing in Matlab that prevents you from making an assignment where i=5 or whatever.

Pages: 1 2 3 4 5