Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Experiment 1_introduction to Matlab

This document provides an introduction to MATLAB, focusing on its environment, functionalities, and basic arithmetic operations. It covers defining scalar variables, assignment operators, and the order of precedence in calculations, along with examples of using MATLAB as a calculator. Additionally, it includes rules for variable naming, predefined variables, and a brief overview of built-in functions and exercises for practice.

Uploaded by

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

Experiment 1_introduction to Matlab

This document provides an introduction to MATLAB, focusing on its environment, functionalities, and basic arithmetic operations. It covers defining scalar variables, assignment operators, and the order of precedence in calculations, along with examples of using MATLAB as a calculator. Additionally, it includes rules for variable naming, predefined variables, and a brief overview of built-in functions and exercises for practice.

Uploaded by

Ajay Raagav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Department of Mathematics

Faculty of Engineering & Technology


Jain (Deemed-to-be University)
Matrix Algebra and Calculus with MATLAB

Experiment: 1
Introduction to MATLAB
AIM:

The main objective of this experiment is to


 Understand the MATLAB environment, and its functionalities
 Defining scalar variables, assignment operators
 Explore different mathematical operators

Introduction

MATLAB, which stands for MATrix Laboratory, is a state-of-the-art mathematical software package that is
used extensively in both academia and industry. It is an interactive program for numerical computation and
data visualization, which along with its programming capabilities provides a very useful tool for almost all
areas of science and engineering.

MATLAB is a high-performance language for technical computing. It integrates computation,


visualization, and programming environment. Furthermore, MATLAB is a modern programming language
environment: it has sophisticated data structures, contains built-in editing and debugging tools, and supports
object-oriented programming. These factors make MATLAB an excellent tool for teaching and research.

MATLAB has many advantages compared to conventional computer languages (e.g., C, FORTRAN) for
solving technical problems. MATLAB is an interactive system whose basic data element is an array that does not
require dimensioning. The software package has been commercially available since 1984 and is now considered
as a standard tool at most universities and industries worldwide. It has powerful built-in routines that enable a very
wide variety of computations. It also has easy-to-use graphics commands that make the visualization of results
immediately available. Specific applications are collected in packages referred to as toolbox. There are toolboxes
for signal processing, symbolic computation, control theory, simulation, optimization, and several other fields of
applied science and engineering.

Understanding the MATLAB Environment

The main working window in MATLAB is called the desktop. When MATLAB is started, the desktop appears in
its default layout and it can be seen in the following figure:

Department of Mathematics, Jain (Deemed-to-be University), Kanakapura, Karnataka. Page 1


Editor Window
(To write
script files)

Current Folder
(Access your files)

Workspace Command History


(Lists variables that (Return commands
you have either that are entered at the
entered or Command Window/Prompt command line)
computed) (Enter Commands)

Fig. 1. MATLAB Environment setup

Basic Arithmetic Operations

• MATLAB can work as a calculator.


• A scalar is a 11 matrix.
• A row vector of length (say 4) is a 14 matrix.
• A column vector of length (say 4) is a 41 matrix.
• The below table summarizes the arithmetic operators available in MATLAB.

Department of Mathematics, Jain (Deemed-to-be University), Kanakapura, Karnataka. Page 2


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. For nested parentheses, the
innermost are executed first.
Second Exponentiation.
Third Multiplication, division (equal precedence).
Fourth Addition and subtraction.

In an expression that has several operations, higher-precedence operations are executed before lower-
precedence operations. If two or more operations have the same precedence, the expression is executed from
left to right. As illustrated in the next section, parentheses can be used to change the order of calculations.

MATLAB as a Calculator

The simplest way to use MATLAB is as a calculator. This is done in the Command Window by typing a
mathematical expression and pressing the Enter key. MATLAB calculates the expression and responds by
displaying ans = and the numerical result of the expression in the next line. This is demonstrated as follows.

Examples

>> 7+8/2 Type and press Enter

ans = 8/2 is executed first.

11

>> (7+8)/2 7+8 is executed first.

ans =

7.5000

>> 4+5/3+2 5/3 is executed first

ans =

7.6667

>> 5^3/2 5/3 is executed first, /2 is executed next.

ans =

62.5000

>> 27^(1/3)+32^0.2 1/3 is executed first, 27^(1/3) and 32^0.2 are executed next,
and + is executed last.
ans =

Department of Mathematics, Jain (Deemed-to-be University), Kanakapura, Karnataka. Page 3


>> 27^1/3+32^0.2 27^1 and 32^0.2 are executed first, /3 is executed
next, and + is executed last.
ans =

11

>> 100+15-14.0567+0.5678-225/2+... Type three periods ... (and press Enter) to


continue the expression on the next line.
45+224-0.456

ans =

257.5551

It is seen that if we do not specify an output variable, MATLAB uses a default variable ans, short for answer,
to store the results of the current calculation. Note that the variable ans is created (or overwritten, if it is already
existed). To avoid this, we may assign a value to a variable or output argument name.

Defining Scalar Variables

A variable is a name made of a letter or a combination of several letters (and digits) that is assigned a numerical
value. Once a variable is assigned a numerical value, it can be used in mathematical expressions, in functions,
and in any MATLAB statements and commands. A variable is actually a name of a memory location. When
a new variable is defined, MATLAB allocates an appropriate memory space where the variable’s assignment
is stored.

Rules about Variable Names

A variable can be named according to the following rules:


 Must begin with a letter.
 Can be up to 63 characters long.
 Can contain letters, digits, and the underscore character.
 Cannot contain punctuation characters (e.g., period, comma, and semicolon).
 No spaces are allowed between characters (use the underscore where a space is desired).
 Avoid using the name of a built-in function for a variable (i.e., cos, sin, exp, sqrt, etc.).
 Once a function name is used to define a variable, the function cannot be used.

Predefined Variables and Keywords

There are 20 words, called keywords, that are reserved by MATLAB for various purposes and cannot be used
as variable names. These words are:

break case catch classdef continue else elseif


end for function global if otherwise parfor
persistent return spmd switch try while

When typed, these words appear in blue. An error message is displayed if the user tries to use a keyword as a
variable name. Some of the predefined variables are given as follows:

Department of Mathematics, Jain (Deemed-to-be University), Kanakapura, Karnataka. Page 4


Variable Description
ans If the user does not assign the value of an
expression to a variable, MATLAB automatically
stores the result in ans.
pi The number π.
inf Used for infinity

Stands for Not-a-Number. Used when MATLAB


NaN cannot determine a valid numeric value.
Example: 0/0.

Assignment Operator

In MATLAB the = sign is called the assignment operator. The assignment operator assigns a value to a
variable.
Variable_name = A numerical value, or a computable expression

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.

Example

>> x=7+8/2

x =

11

>> y=27^(1/3)+32^0.2

y =
5
Thus, the result is stored in y. This variable name can always be used to refer to the results of the previous
computations. Therefore, computing 4y will result in

Example

>> 4*x
ans =
44
Multiple Assignments

Multiple assignments can be done in the same line. The assignments must be separated with a comma
(spaces can be added after the comma). When the Enter key is pressed, the assignments are executed from
left to right and the variables and their assignments are displayed. For example, the assignments of the
variables a, B, and c above can all be done in the same line.

Example

>> a=12; B=4; c=(a-B)+40-a/B*10

Department of Mathematics, Jain (Deemed-to-be University), Kanakapura, Karnataka. Page 5


c =
18

An alternate command

An alternative command for assigning multiple variables is described in the following example.

Example

>> a=12; B=4; c=(a-B)+40-a/B*10;


>> [x,y,z]=deal(a,B,c)
x =
12
Assigns a, B and c to x, y and z
y =
respectively at a time.
4
z =
18

In the above command, the values a, B, and c are assigned to the variables x, y, and z respectively at a time.

Overwriting the variable and semicolon (;)

Once a variable has been created, it can be reassigned. In addition, if you do not wish to see the intermediate
results, you can suppress the numerical output by putting a semicolon (;) at the end of the line. Then the
sequence of commands looks like this:

Example

>> t = 5;
>> t = t+1
t =
6

Quitting MATLAB

To end your MATLAB session, type quit/exit in the Command Window, or select File → Exit MATLAB in
the desktop main menu.

Example

>> quit

Comments (%)

The percentage sign is used to denote a comment. Comments are text in the program that has no effect on the
program itself. In MATLAB, everything on a line that appears after the % sign is ignored. Comments are very
useful for making code easier to understand

Department of Mathematics, Jain (Deemed-to-be University), Kanakapura, Karnataka. Page 6


Example

>> x=1+3 % addition of 1 & 3

x =
4

Case Sensitivity

MATLAB is case sensitive so it distinguishes between upper and lower-case letters, which means that A + B
is not the same as a + b. For example, AA, Aa, aA, and aa are the names of four different variables. Variable
and function names are case sensitive.

Getting Help

To view the online documentation, select MATLAB Help from Help menu or MATLAB Help directly in the
Command Window. The preferred method is to use the Help Browser. The Help Browser can be started by
selecting the ? icon from the desktop toolbar. On the other hand, information about any command is available
by typing

>> help Command

Examples:

>> help who

who List current variables.


who lists the variables in the current workspace.

>> help clc

clc Clear command window.


clc clears the command window and homes the cursor.

Elementary MATLAB Built-in Functions

In addition to basic arithmetic operations, expressions in MATLAB can include 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, or a
computable expression that can be made up of numbers and/or variables. Functions can also be included in
arguments, as well as in expressions. Some commonly used elementary MATLAB mathematical built-in
functions are given as follows.

Elementary math functions


Function Description Example
sqrt(x) Square root. >> sqrt(81)
ans =
9
nthroot(x,n) Real nth root of a real number x. >> nthroot(80,5)
(If x is negative n must be an ans =
odd integer.) 2.4022

Department of Mathematics, Jain (Deemed-to-be University), Kanakapura, Karnataka. Page 7


exp(x) Exponential e x . >> exp(5)
ans =
148.4132
abs(x) Absolute value. >> abs(-24)
ans =
24
log(x) Natural logarithm. >> log(1000)
Base e logarithm (ln). ans =
6.9078
log10(x) Base 10 logarithm >> log10(1000)
ans =
3.0000
factorial(x) The factorial function x! >> factorial(5)
(x must be a positive integer.) ans =
120

Trigonometric math functions


Function Description Example
sin(x) Sine of angle x (x in radians). >> sin(pi/6)
sind(x) Sine of angle x (x in degrees). ans =
0.5000
cos(x) Cosine of angle x (x in radians).>> cosd(30)
cosd(x) Cosine of angle x (x in degrees).ans =
0.8660
tan(x) Tangent of angle x (x in radians). >> tan(pi/6)
tand(x) Tangent of angle x (x in degrees). ans =
0.5774
cot(x) Cotangent of angle x (x in radians). >> cotd(30)
cotd(x) Cotangent of angle x (x in degrees). ans =
1.7321

Inverse Trigonometric & Hyperbolic Functions

The inverse trigonometric functions are asin(x), acos(x), atan(x), acot(x) for the angle in radians;
and asind(x), acosd(x), atand(x), acotd(x) for the angle in degrees. The hyperbolic trigonometric
functions are sinh(x), cosh(x), tanh(x), and coth(x).

Rounding functions
Function Description Example
round(x) Round to the nearest integer. >> round(17/5)
ans =
3
fix(x) Round toward zero. >> fix(13/5)
ans =
2
ceil(x) Round toward infinity. >> ceil(11/5)
ans =
3
floor(x) Round toward minus infinity. >> floor(-9/4)
ans =
-3
rem(x,y) Returns the remainder after x is >> rem(13,5)
divided by y. ans =
3

Department of Mathematics, Jain (Deemed-to-be University), Kanakapura, Karnataka. Page 8


Exercise Problems

1. Calculate the following quantities

a.
2 2
 9 
49

b.
14.82  6.52  
55
2  14
2
3.8

c.
 144  e10.5   5!
log(1000) 2

2. Calculate the following quantities



a. sin 2   cos2  when  
4

b. cosh 2   sinh 2  when  
2
 
tan 64 cos55
c. 
cos 14 sin11
2 

3. Define the variable x as x  2.34 , then evaluate:


a. 2 x4  6 x3  14.8x2  x  4
e2 x
b.
14  x 2  x
4. Define the variable x and y as x  8.3 & y  2.4 , then evaluate:
a. x3  14.8 x 2  y 2  2 x 2 y  y  41
xy  x 2  y 2
b.
x2  y 2  2x

5. A cube has a side of 18 cm. Determine the radius of the sphere that has the same volume as the cube.
4
(Hint: Volume of the cube = s3 , Volume of the sphere = V   r 3 )
3

Department of Mathematics, Jain (Deemed-to-be University), Kanakapura, Karnataka. Page 9

You might also like