[go: up one dir, main page]

0% found this document useful (0 votes)
8 views9 pages

1. Introduction to MATLAB (1)

The document provides an introduction to MATLAB, a high-level programming language used for numerical computations, including its various windows and functionalities. It explains how to work in the command window, perform arithmetic operations, define scalar variables, and manage variables using specific commands. Additionally, it covers display formats, mathematical computations, and predefined variables within MATLAB.

Uploaded by

nisalinduwara01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

1. Introduction to MATLAB (1)

The document provides an introduction to MATLAB, a high-level programming language used for numerical computations, including its various windows and functionalities. It explains how to work in the command window, perform arithmetic operations, define scalar variables, and manage variables using specific commands. Additionally, it covers display formats, mathematical computations, and predefined variables within MATLAB.

Uploaded by

nisalinduwara01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

IS1301: Engineering Mathematics I

Department of Interdisciplinary Studies


Faculty of Engineering
University of Sri Jayewardenepura

Introduction to MATLAB
2.1.1 What is MATLAB?

MATLAB is a high-level programming language primarily intended for numerical computations. It is


typically used for such problems as solving linear and nonlinear equations, numerical linear algebra,
statistical analysis, and for performing other numerical experiments.
MATLAB’s built in functions provide excellent tools for linear algebra computation, optimization,
and numerical solution of ordinary differential equations (ODEs). Also, there are numerous
functions for 2-D and 3-D graphics.

2.1.2 MATLAB Windows

Once the program starts, the window that opens, is shown in the following figure.

Current folder

Editor Window
Workspace

Command Window
Command History
Figure 1: MATLAB desktop environment

The figure 1 contains five smaller windows which are the command window, the workspace window,
the current folder window, the command history window and the editor window. A list of several
windows and their purpose is given in table 1 below.
Table 1: MATLAB windows

Window Purpose
Command window Main window, enter variables, runs programs.
Figure window Contains output from graphic commands.
Editor window Creates and debugs script and function files.
Help window Provides help information.
Command history window Logs commands entered in the command window.
Workspace window Provides information about the variable that are used
Current folder window Show the files in the current working directory.

Command window: The command window is MATLAB’s main window, and opens when MATLAB is
started. This is the main area where commands can be entered at the command line. An example
of an editor window is shown in following figure 1

Editor window: The editor window is used for writing and editing programs. This window is opened
from the file menu. An example of an editor window is shown in following figure 1

Figure window: The figure window opens automatically when graphic commands are executed, and
contain graphs created by these commands. An example of a figure window is shown in following
figure 2.

Figure 2: Example of a Figure window

Help window: The help window contains help information. This window can be opened from the
help menu in the toolbar of any MATLAB window. Help window is interactive and can be used to
obtain information on any feature of MATLAB. Help window is shown following figure 3 below
Figure 3: Help window

2.1.3 Working in the Command Window


The command window is MATLAB’s main window, and can be used for executing commands,
opening other windows running program written by the user, and managing the software. An
example of the command window with several simple commands are shown in following figure 1.
Notes for working in the Command window:

• To type a command, the cursor must be placed next to the command prompt (>>).
• Once a command is typed and the enter key is pressed, the command is executed. However,
only the last command is executed. Everything executed previously is unchanged.
• Several commands can be typed in the same line. This is done by typing a comma between
the commands. When the enter key is pressed the commands are executed in order from
left to right.
• It is not possible to go back to a previous line in the command window, make a correction,
and then re-execute the command.
• A previously typed command can be recalled to the command prompt with the up arrow
key (↑). When the command is displayed at the command prompt, it can be modified if
needed and executed. The down arrow key (↓) can be used to move down previously
typed command.
• If a command is too long to fit in one line, it can be continued to the next line by leaving
a space and typing three periods … (called an ellipsis) and pressing the enter key. The
continuation of the command is then typed in the new line.

The Semicolon (;):

When command is typed in the command window and the enter key is pressed, the command is
executed. Any output that the command generates is displayed in the command window. If
semicolon (;) is typed at the end of a command. The output of command is not displayed.
Percent symbol (%):

When the symbol % is typed in the beginning of a line, the line is designated as a comment. This
means that when the enter key is pressed the line is not executed.
The clc command:
The “clc” command (type clc and press Enter) clears the command window. After working in the
command Window for a while, the display may be very long. Once the “clc” command is executed
a clear window is displayed. The command does not change anything that was done before. For
example, if some variables were defined previously, they still exist and can be used.

2.1.4 Arithmetic Operations


Table 2: Symbols of Arithmetic operations

Operation Symbol Example


Addition + 5+3
Subtraction - 5-3
Multiplication * 5*3
Right division / 5/3
Left division \ 5\3
Exponentiation ^ 5^3
Note:

• 5\3 is equivalent to 3/5

2.1.5 Order of Precedence


MATLAB executes the calculations according to the order of precedence displayed below. This order
is the same as used in most calculators.
Precedence Mathematical operation
First Parentheses
Second Exponentiation
Third Multiplication and division
Fourth Addition and subtraction.

2.1.6 Mathematical computations in MATLAB


Mathematical computations in MATLAB is done by typing a mathematical expression and pressing
the enter key in the command window. MATLAB calculates the expression and responds by
displaying (ans = ) and the numerical result of the expression in the next line.
Type and press Enter
6/2 is executed first

Type and press enter


5+6 is executed first
8/3 is executed first

2^4 is executed first, /5 is executed next

1/3 is executed first, 64^(1/3) and 32 ^0.2 are executed next, and + is
executed the last

2.1.7 Display formats


Table 3: Display Formats

Command Description Example

Short, fixed-decimal format with 4


format short
digits after the decimal point.

Long, fixed-decimal format with 15


format long
digits after the decimal point

Short scientific notation with 4 digits


format short e
after the decimal point

Long scientific notation with 15 digits


format long e
after the decimal point

format short g Floating point format with 5 digits

format long g Floating point format with 15 digits


Table 4: Trigonometric math functions

Function Description Example

sin(x) Sine of angle x (x in radians)


sind(x) Sine of angle x (x in degrees)

cos(x) Cosine of angle x (x in radians)


cosd(x) Cosine of angle x (x in degrees)

tan(x) Tangent of angle x (x in radians)


tand(x) Tangent of angle x (x in degrees)

Table5: Rounding functions

Function Description Example

round(x) Round to the nearest integer

fix(x) Round towards zero

ceil(x) Round towards infinity

floor(x) Round towards minus infinity

rem(x,y) Return the remainder after x is divided by y

sign(x) Sign function return 1 if x>0, -1 if x<0, and 0 if x=0

2.1.8 Defining Scalar Variables


A variable is a name made of a letter or combination of several letters (and digits) that is assigned
to a numerical value. Once a numerical value is assigned to a variable, it can be used in mathematical
expressions, in functions and in any MATLAB statements and commands.

The Assignment Operator


In MATLAB “=” sign is called the assignment operator. The assignment operator assigns a value to a
variable.
Variable_name = numerical value or a computable express

The left-hand side of the assignment operator can include only one variable name. The right-hand
side can be a number, or a computable expression that can include numbers and/or variables that
were previously assigned numerical values. When the Enter key is pressed the numerical value of
the right-hand side is assigned to the variable, and MATLAB displays the variable and its assigned
value in the next two lines.
The following shows how the assignment operator works.

The number 15 is assigned to the variable x


MATLAB displays the variable and its assigned value

A new value is assigned to x. The new value is 3 times the


previous values of x minus 12

Assign 12 to a

Assign 4 to B

Assign the value of expression on right hand side to the variable


C

• If a semicolon is typed at the end of the command then, when the enter key is pressed,
MATLAB does not display the variable with its assigned value (the variable still exists and
stored in memory).
• If a variable already exists, typing the variable’s name and pressing the enter will display the
variable and its value in the next lines.

The variable B is defined but it is not displayed since


a semicolon is typed at the end of the assignment.

• A variable that already exists can be reassigned a new value.

A value of 72 is assigned to the variable “Time”


A new value of 7 is assigned to the variable “Time”

The current value of the variable is displayed when the name of


the variable is typed and the enter key is pressed

Once a variable is defined it can be used as an argument in function.

The left-hand side of the assignment operator can include only one variable name. The right-hand
side can be a number, or a computable expression that can include numbers and/or variables that
were previously assigned numerical values. When the enter key is pressed the numerical value of
the right-hand side is assigned to the variable.
Rules about variable names

• Variable name can contain letters, digits and the underscore character.
• Variable name must begin with a letter.
• Variable name cannot contain punctuation characters
• MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters. For
example, AA, Aa, aA and aa are the names of four different variables.
• No spaces are allowed between characters (use the underscore where a space is desired).
• Avoid using the name of built in function (avoid using cos, sin, exp, sqrt)

Predefined variables

A number of frequently used variables are already defined when MATLAB is started. Some of the
predefined variables are:

If the user does not assign the value of an expression to variable, MATLAB automatically
ans
stores the result in ans

pi The number π
eps The smallest difference between two numbers.
Inf Used for infinity.
i Defined as √−1 , which is 0 + 1.000𝑖
j Same as i
NaN Stands for not a number.
2.1.9 Useful Commands for Managing Variables

Command Outcome
clear Removes all variables from memory
clear x y z Removes only variable x, y and z from the memory
who Display a list of variables currently in the memory
Display a list of the variables currently in the memory and their size together with
whos
information about their bytes and class.

The help command can be used to find more about a particular command.
help
Ex: help ex

You might also like