You are on page 1of 4

MATLAB FUNDAMENTALS

>> help command lists all your previously worked files. But if you need a real help, for example
you want to find the meaning of a command and/or you need a command like zeros then you
go the upper right box and type what you want.
For example type zeros and look what you can find.

1.2. Arrays, Vectors and Matrices

If you need a matrix consists of zeros as (1×3) then type

>> A = zeros(1,3)

A=

0 0 0

If you need a matrix consists of ones for example

>> B = ones(4,7)

B=

1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1 1 1
Dimensions can be as you need. These types of zeros or ones matrices are useful to form the
dimensions of a desired matrix first then fill it with other calculations.

1.3. The Colon Operator

The colon operator “:” works totally different from brackets as we saw previously.

>> a = 1:3:34

a=

1 4 7 10 13 16 19 22 25 28 31 34

This means if you want to create a matrix which starts from 1 and end in 34 and numbers step
by 3. Something like 1, 1+3=4, 4+3=7, … etc.

You can change increment (number steps) as you wish like increasing as 1:3:34, decreasing as
75:-1:68 and/or decimals as 73:-0.5:15.

1.4. The linspace and logspace Functions

linspace(x1, x2, n) and logspace(x1, x2, n) both generate equally spaced vectors. For example

>>A = linspace(4,700, 300)

A=

Columns 1 through 12

4.0000 6.3278 8.6555 10.9833 13.3110 15.6388 17.9666 20.2943 22.6221 24.9498
27.2776 29.6054

Columns 13 through 24

31.9331 34.2609 36.5886 38.9164 41.2441 43.5719 45.8997 48.2274 50.5552


52.8829 55.2107 57.5385

Columns 25 through 36 …

Apart from this, logspace uses decade increments as 10x1 and 10x2. For example

>> format long


>> B = logspace(-7, 10, 24)

B=

1.0e+10 *

Columns 1 through 6
0.000000000000000 0.000000000000000 0.000000000000000 0.000000000000002
0.000000000000009 0.000000000000050

Columns 7 through 12

0.000000000000272 0.000000000001492 0.000000000008185 0.000000000044893


0.000000000246209 0.000000001350314

Columns 13 through 18

0.000000007405685 0.000000040615860 0.000000222754295 0.000001221677349


0.000006700187504 0.000036746619407

Columns 19 through 24

0.000201533768594 0.001105295141126 0.006061898993498 0.033245979322709


0.182334800086844 1.000000000000000

You can easily understand the importance of format short and long functions along with
linspace and logspace functions.

1.4. Character Strings

Characters namely chars written with ‘’ quotations. For example

>> V = 'Yeser';
>> Z = 'Aslanoglu';

Also, we can concatenate them as in one sentence as

>> X = [V, Z]; or


>> X = [V Z];

2. Mathematical Operations

^ Exponentiation
- Negation
* Multiplication
/ Division
\ Left Division (applies to matrix algebra)
+ Addition
- Subtraction

As it is obvious MATLAB can work as a calculator. You can play with tis option with your
previous notes. Hands on training! Dig deeper on matrix algebra rules!

2. Built-in Functions

Built-in mentions which the functions MATLAB already has in its info. You can use these built-
in functions to make your life easier. At beginning of this doc, you saw >>help function. Only
typing help shows your previous works on MATLAB. But if you want to learn about a function
more, you have to type for example as >>help zeros.
>> help zeros
zeros Zeros array.
zeros(N) is an N-by-N matrix of zeros.

zeros(M,N) or zeros([M,N]) is an M-by-N matrix of zeros.

zeros(M,N,P,...) or zeros([M N P ...]) is an M-by-N-by-P-by-... array of


zeros.

zeros(SIZE(A)) is the same size as A and all zeros.

zeros with no arguments is the scalar 0.

zeros(..., CLASSNAME) is an array of zeros of class specified by the


string CLASSNAME.

zeros(..., 'like', Y) is an array of zeros with the same data type, sparsity,
and complexity (real or complex) as the numeric variable Y.

Note: The size inputs M, N, and P... should be nonnegative integers.


Negative integers are treated as 0.

Example:
x = zeros(2,3,'int8');

See also eye, ones.

Reference page for zeros


Other functions named zeros

MATLAB can give you all about the function which you asked to learn. Give a try the functions
you are wondering with typing help.

Example_1. Create two matrices and try to find min, max, mean and square root of each matrix.
Then try basic math algebra with them (addition, subtraction, multiplication, division, etc.)

Assignment_2. (1). You have a bungee-jumping company and you need to check the safety
plans are working or not. First of all, you need to find a bungee jumper’s free-falling velocity.
Please calculate the velocity with below equation and the givens. USE MATLAB!

mass is m = 72 kg,
acceleration due to gravity is g = 9,81 m/s2,
drag coefficient is cd = 0.23 kg/m
time is t = 20s by 2s steps*
* you have to create a column vector for t(you learned to create a vector which is default a row,
you learned also transpose and make it a column vector, tanh is a built-in function also)
𝑔𝑚 𝑔𝑐𝑑
𝑣= √ 𝑡𝑎𝑛ℎ (√ 𝑡)
𝑐𝑑 𝑚

You might also like