Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Mat 3510 Mathematical Packages

Sattam Bin Abdulaziz University


College of Science and Humanities Studies
Department of mathematics

Mathematical Package

MAT 3510

Level 6

Manasik Mahagoub Mustafa

1
Mat 3510 Mathematical Packages

Lecture (2)

INTRODUCTION TO MATLAB

2
Mat 3510 Mathematical Packages

What is MATLAB

 Matlab is MATrix LABoratory.

 MATLAB is a numerical computing environment and programming language


(initially written in C). MATLAB allows easy matrix manipulation, plotting of
functions and data, implementation of algorithms, creation of user interfaces, and
interfacing with programs in other languages.

 MATLAB makes mathematical operations with vectors y matrices. As a particular


case, it can also work with scalar numbers, both reals and complexes.

Starting MATLAB, MATLAB Windows

Once the program starts, the MATLAB desktop window opens (Figure 1-1). The
window contains four smaller windows: the Command Window, the Current Folder
Window, the Workspace Window, and the Command History Window.

Workspace
Command windows
Current Directory

Figure 1-1: The default view of MATLAB desktop. Command History

3
Mat 3510 Mathematical Packages
Table 1-1: MATLAB windows

Window Purpose
Command Window Main window, enters 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
variables that are used.
Current Folder Window Shows the files in the current folder.

Figure Window: The Figure Window opens automatically when graphics com- mands are
executed, and contains graphs created by these commands. An example of a Figure
Window is shown in Figure 1-2.

Figure 1-2: Example of a Figure Window.

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 Figure 1-3.

4
Mat 3510 Mathematical Packages

Figure 1-3: Example of an Editor 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. Figure 1-4 shows an open
Help Window.

Figure 1-4: The Help Window.

The Command History Window lists the commands that have been entered in the
Command Window. A command in the Command History Window can be used again in the
Command Window. By double-clicking on the command, the command is reentered in the
Command Window and executed.

5
Mat 3510 Mathematical Packages

Working in the command Windows

 When a command is typed in the command window and the Enter Key is pressed, the
command is executed. shown in Figure 1-5.
 If a semicolon “;” is typed at the end of a command, the output of the command is
not displayed.
 Write “%” at the beginning of a line to write a comment in the program.
 Write “…” at the end of a line if you are writing a very long statement and you want
to continue in the next line.

To type a command the cursor is placed


next to the command prompt (>>)

Figure 1-5: The Command Window.

Note:-
- If several commands are typed in the same line, the output from any of the
commands will not be displayed if a semicolon is typed between the commands
instead of a comma.
- The clc command (type clc and press Enter) clears the Command Window.

Athematic Operations
Operation Symbol Example
Addition + 5+3
Subtraction – 5–3
Multiplication * 5*3
Right division / 5/3
Left division \ 5\3=3/5
Exponentiation ^ 5 ^ 3 (means 53 = 125)

6
Mat 3510 Mathematical Packages

MATLAB as a Calculation

Tutorial 1-1: Using MATLAB as a calculator.

>> 7+8/2 Type and press Enter


ans = 8/2 is executed first
11
>> (7+8)/2
Type and press Enter
ans =
7 + 8 is executed first
7.5000

>> 4+5/3+2
5/3 is executed first
ans =
7.6667

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

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

>> 0.7854-(0.7854)^3/(1*2*3)+0.785^5/(1*2*3*4*5)…
-(0.785)^7/(1*2*3*4*5*6*7) Type three periods … (and press Enter)
ans = to continue the expression on the next line
0.7071
The last expression is the first four terms
𝜋
>> of the Taylor series for sin ( 4 )

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 sqrt(x). Its name is sqrt, and the argument is x.

Tutorial 1-2: Using the sqrt built-in function.

>> sqrt(64)
Argument is a number
ans =
8

>> sqrt(50+14*5)
Argument is a an expression
ans =
9.5917

7
Mat 3510 Mathematical Packages

>> 54+9*sqrt(100)
Argument includes a function
ans =
12

>> (15+600/4)sqrt(121) Function is included in an expression


ans =
15

>>

Table 1-3: Elementary math functions

Function Description Example


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

>> exp(5)
exp(x) Exponential e x  . ans =
148.4132

>> abs(-24)
abs(x) Absolute value. ans =
24

>> log(1000)
Natural logarithm. Base e
log(x) ans =
logarithm (ln). 6.9078

>> log10(1000)
log10(x) Base 10 logarithm. ans =
3.0000

The factorial function x! >> factorial(5)


factorial(x) (x must be a positive ans =
integer.) 120

8
Mat 3510 Mathematical Packages

Table 1-4: Trigonometric math functions

Function Description Example

sin(x) Sine of angle x (x in radians). >> sin(pi/6)


ans =
sind(x) Sine of angle x (x in degrees). 0.5000

cos(x) Cosine of angle x (x in radians). >> cosd(30)


ans =
cosd(x) Cosine of angle x (x in degrees). 0.8660

tan(x) Tangent of angle x (x in radians). >> tan(pi/6)


ans =
tand(x) Tangent of angle x (x in degrees). 0.5774
>> cotd(30)
cot(x) Cotangent of angle x (x in radians).
ans =
cotd(x) Cotangent of angle x (x in degrees). 1.7321

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). Table 1-4
uses pi, which is equal to

Defining Scalar Variables


1. The Assignment Operator

Variable_name = A numerical value, or a computable expression


The following shows how the assignment operator works.

>> x = 15 The number 15 is assigned to the variable x


x =
MATLAB displays the variable and
15
its assigned the value

>> x=3*x-12 A new value is assigned to x. A new value


x = is 3 times the previous value of x minus 12
33

>> a=12 Assigned 12 to a


a =
12

>> B=4 Assigned 4 to B


B =
4
>> C=(a-B)+40-a/B*10 Assigned value of the expression on
x = right hand side to the variable C.
18

9
Mat 3510 Mathematical Packages

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.

>> a=12; The variables a, B and C are defined but are


>> B=4; not displayed since a semicolon is typed at
the end of each statements.
>> C=(a-B)+40-a/B*10;

>> C
The value of the variable C is displayed by
C =
typed the name of the variable.
18

Several assignments can be typed in the same line. See the following example:

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

a =
12 The variable B is not displayed because a semicolon is
C = typed at the end of the assignment.
18

2. Rules about Variable Names:


A variable can be named according to the following rules:
• Must begin with a letter.
• Can contain letters, digits, and the underscore character.
• Cannot contain punctuation characters (e.g., comma, semicolon).
• 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).

3. 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

 A number of frequently used variables are already defined when


MATLAB is started. Some of the predefined variables are:
ans MATLAB automatically stores the result in ans, If the user does not
assign the value of an expression to a variable.

11
Mat 3510 Mathematical Packages

Pi The number .
eps The smallest difference between two numbers. Equal to 2^(–52).
inf Used for infinity.
i Defined as √ , which is: 0 + 1.0000i.

j Same as i.

NaN Stands for Not-a-Number. Used when MATLAB cannot determine a valid numeric value.
Example: 0/0.

Useful Commands For Managing Variables

Command Outcome

clear Removes all variables from the memory.


clear x y z Removes only variables x, y, and z from the memory.
who Displays a list of the variables currently in the memory.
whos Displays a list of the variables currently in the memory and their
sizes together with information about their byits.

Example:-
A trigonometric identity is given by:

Verify that the identity is correct by calculating each side of the equation,
substituting

Solution:-
The problem is solved by typing the following commands in the Command Window.

>> x=pi/5; Define x


>> LHS=cos(x/2)^2 Calculate the left-hand side.
LHS=
0.9045

>> RHS=(tan(x)+sin(x))/(2*tan(x))
Calculate the right-hand side.
RHS =
0.9045

11
Mat 3510 Mathematical Packages

Tutorial (1)

The following problems solve by writing commands in the command window.

1. Calculate:
) 4 √ ) ) 4)
(a) (b) )
4 ) )
)
(c) (d) )
√ ) √ )

2. Define the variable , then evaluate:


4
(a) (b)
√ 4
3. Define the variable , then evaluate:

(a) (b) √ √ ( ) √

4. In the triangle shown a = 9 cm, b = 18 cm, and c = 25 cm.


Define a, b, and c as variables, and then:
(a) Calculate the angle (in degrees) by substituting the 
variables in the Law of Cosines.
(Low of Cosines: = 

(b) Calculate the angles (in degrees) using the
Low of Sines.
(c) Check that the sum of the angles is

5. For the triangle shown a = 200 mm, b = 250 mm, and


c = 300 mm. Define a, b, and c as variables, and then: 

(a) Calculate the angle (in degrees) by substituting the c

variables in the Law of Cosines.


r
b
(Low of Cosines: = 

(b) Calculate the radius of the circle inscribed in the  a


triangle using the formula ) )

(c) Calculate the radius of the circle inscribed in the


√ ) ) )
triangle using the formula
)

12
Mat 3510 Mathematical Packages

13

You might also like