You are on page 1of 27

COMPUTER PROGRAMMING

GET 231
General introduction and Overview
CVE
Nile University of Nigeria

Ms SALAU OIZA
October, 2021.

28/03/22 07:14 PM 1
General Class Requirement

Attendance: 5%
Quiz: 20%
Midterm: 15%
Finals: 60%

28/03/22 07:14 PM 2
Contents
o Starting MATLAB
References
o Local Environment setup
o Using MATLAB as a calculator
o Quitting MATLAB
o Creating MATLAB variables
o Overwriting variable
o Managing the workshop
o Error messages

28/03/22 07:14 PM 3
MATLAB
o MATLAB stands for MATrix LABoratory
o It is a fourth-generation high-level programming language and interactive
environment for numerical computation, visualization and programming
o MATLAB /Simulink is a powerful software tool for: Performing
mathematical computations and signal processing
o Interpreted Language
o Very good tool for manipulation of matrices
o Great visualization capabilities
o Lots of built-in functions
o Easy to learn and simple to use

28/03/22 07:14 PM 4
Matlab Desktop

Workspace / Current
Directory
The Workspace shows you Command Window
all of your current working
variables and other objects
The Command window is
where you'll give MATLAB its
Command History input and view its output
The history shows you all
commands you used in
command window
Explore the Matlab Desktop
1.The Editor for MATLAB scripts (M-files) . To save & run the m-file type 'F5'. To open the editor with
a new or old m-file use the command Oiza
opensalaufile_name 5
MATLAB
Installation
o MATLAB has since been expanded and now has built-in functions
for solving problems requiring data analysis, signal processing,
optimization, and several other types of scientific computations. It
also contains functions for 2-D and 3-D graphics and animation

28/03/22 07:14 PM 6
Getting started with MATLAB
Run MATLAB from
Start  Programs  MATLAB
oDepending on version used, several windows appear
oFor example in Release 13 (Ver 6), there are several windows – command history, command,
workspace, etc
oCommand window
oMain window – where commands are entered
oThe workspace is Matlab’s memory
oCan manipulate variables stored in the workspace
>> b=10;
>> c=a+b
c=
22
28/03/22 07:14 PM 7
MATLAB as calculator
• Example of a simple interactive calculation
• Type in the expression you want to evaluate.
• Calculate the expression, 1 + 2×3. You type it at the prompt command
(>>) as follows,
• >> 1+2*3
• ans = 7
• Notice that, MATLAB uses a default variable ans, short form for
answer to store the result of the current calculation.

28/03/22 07:14 PM 8
VARIABLES
o Variables in MATLAB are named objects that are assigned using the equals sign =
o MATLAB variables are created with an assignment statement. The syntax of
variable assignment is variablename = a value (or an expression)
o >> x = expression
o An expression is a combination of numerical values, mathematical operators,
variables, and function calls. Expression can be manual entry, built-in functions or
user-defined functions
o They are limited to 31 characters
o Can contain upper and lowercase letters. Does not start with a numeral.
o MATLAB is case sensitive: X and x are different variables.
o MATLAB does not display the result of an expression that ends with a semi-colon ;
28/03/22 07:14 PM 9
VARIABLES
Don’t have to declare type
Don’t even have to initialise
Just assign in command window
>>
>> a=12; % variable a is assigned 12

Matlab
prompt
comment
suppress
assign operator
command
operator output

Try the same line without the


semicolon and comments
10
Variables (continued …)
Variable Cont….
• View variable contents by simply typing the variable name at the
command prompt
>> a
a=
12
>>
>> a*2
a=
24
>>
11
Special Variables

ans Default variable name for results


pi Value of π
eps Smallest incremental number
inf Infinity
NaN Not a number e.g. 0/0
i,j,1i,1j imaginary unit i, i.e. square root of -1
realmin The smallest usable positive realnumber
realmax The largest usable positive real number

28/03/22 07:14 PM 12
Workspace
The workspace is Matlab’s memory
Can manipulate variables stored in the workspace
>> b=10;
>> c=a+b
c=
22
>>

13
Workspace cont…..
• Display contents of workspace
>> whos
Name Size Bytes Class
a 1x1 8 double array
b 1x1 8 double array
c 1x1 8 double array
Grand total is 3 elements using 24 bytes
>>

• Delete variable(s) from workspace


>> clear a b; % delete a and b from workspace
>> whos
>> clear all; % delete all variables from workspace
>> whos

14
Matlab Help commands
 help
>> help whos % displays documentation for the function whos
>> lookfor convert % displays functions with convert in the first help line

Start Matlab help documentation


>> helpdesk

15
Vectors
A vector is a one dimensional array of numbers MATLAB allows creating two types of vectors
Row vectors are created by enclosing the set of elements in square brackets using space or comma to
delimit the elements
Example
r = [ 4 5 7 8 6]
Matlab will execute this as
r=
4 5 7 8 6
another example
r = [ 4 5 7 8 6];
t = [ 2, 3, 1, 2, 0];
res = r + t
res= 6 8 8 10 6

16
Vectors contd…
column vectors are created by enclosing the set of elements in square brackets,
using semicolon (;) to delimit the elements
c = [ 4; 5; 7; 8; 6]
Matlab will execute this as
c=
4
5
7
8
6

17
Matrices

• A matrix is a two- dimensional array of numbers


• Don’t need to initialise type, or dimensions
>>A = [3 2 1; 5 1 0; 2 1 7]
A=
3 2 1 square brackets to define matrices

5 1 0
semicolon for next row in matrix
2 1 7
>>

18
The colon (:) operator
• VERY important operator in Matlab
• It is use to create vectors, subscript arrays and specify for iterations
• To create a row vectors of integers 1 to 10 we do
>> 1:10 Try the following
ans = >> x=0:pi/12:2*pi;
1 2 3 4 5 6 7 8 9 10 >> y=sin(x)
If you want to increment the value other than one
>> 1:2:10
ans =
1 3 5 7 9
19
The : operator and matrices
>>A(3,2) A=
ans = 3 2 1
5 1 0
A = [3 2 1;5 1 0;2 1 7]
1
2 1 7
>>A(:,2)
ans =
2 What’ll happen if you type
1 A(:,:) ?
1

20
Manipulating Matrices

• Access elements of a matrix A=


>>A(1,2) 3 2 1
ans= 5 1 0
indices of matrix element(s)
2 2 1 7
• Remember Matrix(row,column)
• Naming convention Matrix variables start with a capital letter while
vectors or scalar variables start with a simple letter

21
Manipulating Matrices
A=
>> A ' % transpose B=
1 3 1
3 2 1
5 1 0
>> B*A % matrix multiplication 4 9 5
2 1 7
2 7 2
>> B.*A % element by element multiplication
>> B/A % matrix division
Enter matrix B into the
>> B./A % element by element division Matlab workspace

>> [B A] % Join matrices (horizontally)


>> [B; A] % Join matrices (vertically)

Create matrices A and B and try out the the matrix operators in this slide

22
Useful commands

 To clear the Command Window, type clc


 To abort a MATLAB computation, type ctrl-c
 cd/chdir Change directory
 pwd Show current directory
 who List known variables
 whos List known variables plus their size
 clear Clear variables from workspace
 clc Clear the command window
28/03/22 07:14 PM 23
Overwriting variables

When a variable has been created, it can be reassigned


Example:
 >> x = 7;
 >> x = x*2 Task: 1
Perform the multiple operations (4.5 + 15)/2.
 Perform the multiple operations (15 – 4 + 12)/5 –
2*(7^4)/100.
 Perform the multiple operations (15 – 4) + 12/5 –
(2*7)^4/100
28/03/22 07:14 PM 24
Error messages

 incorrectly typed expression, MATLAB returns an error message


 For example, in the following, we left out the multiplication sign, *,
in the following expression
>> r = 3;
>> ; (pi)
Error: Unexpected MATLAB expression.

28/03/22 07:14 PM 25
ASSIGNMENT
1. Compare MATLAB with Mathematica, Rlab,
Scilab and GNU octave.
2. As an Engineer, why should you use
MATLAB?

28/03/22 07:14 PM 26
Questions

28/03/22 07:14 PM 27

You might also like