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

Programming by MATLAB 2

ماتلاب 2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Programming by MATLAB 2

ماتلاب 2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Programming by MATLAB 2

Petroleum and Gas Engineering


Department
Second Year
1.Data Type

• MATLAB does not require declaring the type of


the variable.

• Regarding numeric variables, By default, MATLAB


stores all numeric variables as double-precision
floating-point values.
1.1. Integer and floating-point data
• Computer number format are different from each other by how they are stored and how
many bytes theyoccupy in computer memory. Table below lists different data types in
MATLAB.
1.1. Integer and floating-point data
• To declare any variable of any type described in above table, use data type name you
desire as a function to assign data to the variable :

The data type you assign must match with the data. For example (uint16) does not accept
negative values, if user does, that MATLAB will assign 0 to the variable instead of the
negative number.
1.2. Characters and Strings
• In MATLAB, char refers to character data type and a row of a
characters is called string.
• Each char type needs 2 bytes to store in computer memory.
• To declare a variable of type character, just use (char) before the
variable name:
1.2. Characters and Strings
• Also user can define char type by holding variable value between
two quotations.
1.2. Characters and Strings

• (whos) command return some information about a variable.


• Notice that x size is 15 which refer to number of characters in x
including spaces and occupied size for x in memory is 30, i.e. 2 byte
for each one and x class is char which is mean data type.
1.2. Characters and Strings
• To convert a number to a string data type, the command
(num2str) is used.

Example:
y=x/2;
y_val=num2str(y);

• To convert a string to a number data type, the command


(str2num)is used.
2. MATLAB Basics
2.1. Variables
• Variables refer to a specific area from the memory which
hold some value. This value may be any data type we
had explained previously.

• Variables also can take the form of an array.

• Users should take care of the letter case because


MATLAB is a case sensitive programming language, i.e X
differ from x.
2.1. Variables
Rules of Naming Variables in MATLAB:
1- The name of the variable must be different from the
name of the script file.
2- The name must start with a letter.
3- It could be more than one word divided by underscore.
4- The name of the variable could be up to 63 characters.
5- Variable name should not be same as names of special
variables in MATLAB (will see them soon).
2.1. Variables
• As soon as variables have been created, its names will appear in workspace
window as shown below:

• MATLAB has many reserved names and user must not use it to name his
variables, some of these reserved names are :
(For, end, if, while, function, return, elseif, case, otherwise, switch, continue, global,
break, try, catch).
2.1. Variables
MATLAB Special Variables
Special variables have some value that are already
reserved by MATLAB. the following table list the mostly
used one:
2.1. Variables
MATLAB Special Variables
• As mentioned that user shouldn’t give names to his
variables similar to MATLAB special variables, this
causes overriding and lost its original value, as an
example to overriding (pi) value:

Note: Restarting MATLAB will reset the special variable to its original values.
3. Some Commands

1. clc command
This command cleans the command window of matlab but does not
delete variable
value from the memory.
2. clear command
Clear command used to delete variables permanently from the
memory and it can be
used to clear a specific variable, to do that just type clear followed by
variable name
in command window.

Or you can delete all variables from workspace window by typing only
clear in
command window.
3. Some Commands
3. who and whos commands
who command return names of the variables in workspace and whos
return a list of
properties for a variable include its size, bytes, class and attributes.
3. Arithmetic Operations.

Precedence of Arithmetic Operations

1. Parentheses
2- Power operator executed firstly.
3- Multiplication and division.
4- Addition and subtraction.
3. Arithmetic Operations.

How to let MATLAB caculate 2+3 before other operations ? Why?


4. Complex Numbers .
• MATLAB can express complex numbers by means of (i) or (j) which
they have the value of and do the mathematical operations
belong to this type of numbers.
• Simply complex numbers can be expressed in MATLAB as follows:
4. Complex Numbers .
• Mathematical operations can be made for these numbers:

• Conjugate complex number easily called as :


4. Complex Numbers .
• MATLAB also support some useful function relevant to this type of
numbers.
5. Trigonometric Functions
5. Trigonometric Functions
6. Exponential Functions
6. Approximation functions
• All the following resulting in integer numbers

You might also like