0% found this document useful (0 votes)
40 views

Lab 1

Here are the solutions to the practice problems in MATLAB: 1. eng_lab = zeros(3,4); 2. eye(5) 3. x = 5; y = 2; z = x + y; disp(z) This prints: 7

Uploaded by

Enas Qtaifan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Lab 1

Here are the solutions to the practice problems in MATLAB: 1. eng_lab = zeros(3,4); 2. eye(5) 3. x = 5; y = 2; z = x + y; disp(z) This prints: 7

Uploaded by

Enas Qtaifan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Engineering Applications LAB

Applied Science Private University

LAB 1
An introduction to
MATLAB

Dr. Abdelrahman Radwan

Student Name
Student ID
Student Score

1
What is MATLAB?
MATLAB is a software package for high-performance mathematical
computation, visualization, and programming environment. It provides an
interactive environment with hundreds of built-in functions for technical
computing, graphics, and animations.

MATLAB stands for Matrix Laboratory, and it is a modern programming


language environment, that has refined data structures, includes built-in
editing and debugging tools, and supports object-oriented programming.

MATLAB is Multi-paradigm. So, it can work with multiple types of


programming approaches, such as Functional, Object-Oriented, and
Visual.

MATLAB allows several types of tasks, such as manipulations with matrix,


algorithm implementation, data, and functions plotting, and can interact
with programs written in other programming languages.

2
MATLAB Language

This is a high level matrix/array language with control flow statement,


function, data structure, input/output, and object-oriented programming
characteristics. It allows both "programming in the small" to create quick
and dirty throw-away programs rapidly and "programming in the large"
to create large and complex application functions.

Graphics

MATLAB has extensive facilities for displaying vector and matrices as


graphs, as well as annotating and printing these graphs. It contains high-
level structures for two-dimensional and three-dimensional data
visualization, image processing, animation, and presentation graphics. It
also involves low-level structures that allow us to customize the display of
graphics fully as well as to build complete graphical user interfaces on our
MATLAB applications.

MATLAB Desktop

MATLAB provides a Desktop GUI based environment. The Home tab of


the environment contains three panels there:

1. Current folder: It is located on the left side; here, you can access your
files.
2. Command Window: It is located on the right side; it is the command
prompt, and here you can enter commands to operate the functions,
to assign variables, and for calculations.
3. Workspace: It is located on the left side right below the Current
Folder; here, all variables that you create are stored, and data from
other files can also be imported here.

3
Variable and Workspace Information

Commands Purpose

who lists variables currently in the workspace

whos lists variables directly in the workspace with their size

what lists M-, Mat-, and Mex directories on the disk

clear clear the workspace, and all variables are removed

clear x, y, z clear only variables x, y, and z.

clear all Clears all variables and function from workspace

mlock fun locks function fun so that clear cannot delete it

munlock fun unlocks functions fun so that clear cannot remove it

clc clears command window, cursor shift to the top

home scrolls the command window, cursor moves to the top

clf clear figure window

4
load Load variable from file

save Save variable in Mat-file

length Length of a vector

size Size of a matrix

pack Consolidate memory space

disp Display text or matrix

Working with Files and Directory Information

Commands Purpose

pwd shows the current working directory

cd changes the current working directory

dir lists contents of the current directory

ls lists contents of the current files, same as dir

path gets or sets MATLAB search path

editpath modifies MATLAB search path

copyfile copies a file

mkdir creates a new directory

rmdir Remove directory

delete Delete file

diary Save text of MATLAB session

type Show contents of the file

path List accessible directories

what Lists all MATLAB data in the current directory.

5
Working with Variables and Arrays in
MATLAB
The structural unit of data in any MATLAB program is the array. An array is a collection
of record values organized into rows and columns and known by a single name. Arrays
can be allocated as either vectors or matrices. The term "vector" is generally used to
define an array with only one dimension, while the term "matrix" is usually used to
determine an array with two or more dimensions.

The numbers of the row state the size of an array, and the numbers of the column in
the array, with the numbers of the row, mentioned first. The total number of items in
the array will be the product of the number of rows and the number of the column.

For example, the size of the following arrays are:

Individual items in an array are addressed by the array name followed by the row
and column of the particular item. If the array is a row or column vectors, then only
one subscripts are needed. For example, in the preceding array a(2,1) is 3 and c(2) = 2.

A MATLAB variable is an area of memory, including an array, which is called by a


user-specified name. The content of the array may be used or modified at any time by
containing its name in an appropriate MATLAB command.

MATLAB variables name must start with a letter, followed by any sequence of
letters, numbers, and the underscore (_) character. Only the first 63 characters are
essential; if more than 63 are used, the remaining characters will be ignored. If two
variables are stated with names that only differ in the 64th character, MATLAB will treat
them as the same variable.

6
Initializing Variables in Assignment Statement
The simplest method to initialize a variable is to assign it one or more value in an
assignment statement.

An assignment statement has the standard form

var = expression;

where var is the name of the variables and expression is a scalar constant, an array, or a
combination of constants, other variables, and mathematical operations (+, -, etc.). The value
of the expression is computed using the standard rules of mathematics, and the resulting
values are saved in the named variable.

Examples of initializing variables with assignment statements contain

var = 40i;
var2 = var/5;
x = 1; y = 2;
array = [1 2 3 4];

7
Array Initialization

The following statements are all legal arrays that can be used to initialize a variable:

Arrays can also be initialize using built-in MATLAB function. For example:

a = zeros(2);
b = zeros(2,3);
c = [1 2; 3 4];
d = zeros(size(c));

8
Functions Purpose

zeros(n) Creates a n x n matrix of zeros.

zeros(m,n) Creates a m x n matrix of zeros

zeros(size(arr)) Create a matrix of zeros of the same size as arr.

ones(n) Creates a n x n matrix of ones.

ones(m,n) Creates a m x n matrix of ones.

ones(size(arr)) Creates a matrix of ones of the same size as arr.

eye(n) Creates a n x n identity matrix.

eye(m,n) Creates an m x n identity matrix.

length(arr) Return the length of a vector, or the longest dimension of


a 2-D array.

size(arr) Return two values specifying the number of rows and


columns in arr.

9
ELEMENTARY MATH BUILT-IN FUNCTIONS

MATLAB has a very large library of built-in functions. A function has a name and an argument
in parentheses. For example, the function that calculates the square root of a number is sqrt(x).
Its name is sqrt, and the argument is x. When the function is used, the argument can be a
number, a variable that has been assigned a numerical value

10
11
Practice

1- Create an array of zeros, Name “eng_lab”, that has 3


rows and 4 columns.

2- Create an identity matrix of size 5 with single MATLAB


command.

Write the following in Matlab command and print out the


answer:

12
13

You might also like