0% found this document useful (0 votes)
41 views18 pages

Introduction to MATLAB Basics

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

Topics covered

  • error handling,
  • graphics,
  • special variables,
  • variable assignment,
  • character arrays,
  • MATLAB,
  • mathematical functions,
  • built-in functions,
  • user-defined functions,
  • MATLAB desktop
0% found this document useful (0 votes)
41 views18 pages

Introduction to MATLAB Basics

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

Topics covered

  • error handling,
  • graphics,
  • special variables,
  • variable assignment,
  • character arrays,
  • MATLAB,
  • mathematical functions,
  • built-in functions,
  • user-defined functions,
  • MATLAB desktop

Introduction to MATLAB

Dr. Hussein A. AlIedane


College of Oil & Gas Engineering
What Is MATLAB?
• MATLAB is a program for doing numerical
computation. It was originally designed for solving
linear algebra type problems using matrices. It’s
name is derived from MATrix LABoratory.
• high-performance language for technical computing
• computation, visualization, and programming in an
easy-to-use environment
• MATLAB has since been expanded and now has
built-in functions for solving problems requiring
data analysis, signal processing, optimization, and
several other types of scientific computations. It
also contains functions for 2-D and 3-D graphics and
animation.
• Integrated software environment
– Computation
– Visualization
– Easy-to-use environment

• High-level language
– Data types
– Functions
– Control flow statements
– Input/output
– Graphics
– Object-oriented programming capabilities
• The MATLAB environment is command oriented
somewhat like UNIX. A prompt appears on the screen
and a MATLAB statement can be entered. When the
<ENTER> key is pressed, the statement is executed, and
another prompt appears.
• Note - Up to now, we have let MATLAB repeat everything
that we enter at the prompt (>>). Sometimes this is not quite
useful, in particular when the output is pages en length. To
prevent MATLAB from echoing what we type, simply enter a
semicolon (;) at the end of the command. For example,
>> x=-163.6667;
and then ask about the value of x by typing,
>> x
x = -163.6667
Matlab Desktop
Variables
• Variable names:
– Must start with a letter
– May contain only letters, digits, and the underscore “_”
– Matlab is case sensitive, i.e. one & One are different
variables.
– ariable, function, file names
– is case sensitive, e.g., NAME and Name are 2 distinct
names.
– variable begins with a letter, e.g., A2z or a2z
– can be a mix of letters, digits, and underscores (e.g.,
vector_A)
– reserved characters: % = + – ~ ; : ! ' [ ] ( ) , @
# $& ^
– up to 63 characters
A function performs a pre-defined task based on input
to yield certain outcome.
Variables
• MATLAB variables are created with an assignment statement. The
syntax of variable assignment is
variable name = a value (or an expression)
For example,
>> x = expression
where expression is a combination of numerical values, mathematical
operators, variables, and function calls. On other words, expression
can involve:
• manual entry
• built-in functions
• user-defined functions

– Variable = number;
– Variable = expression;
Special variables:
• ans : default variable name for the result
Basic Arithmetic Operator
There are in general two kinds of operators: unary operators, which operate
on
a single value or operand; and binary operators, which operate on two
values
or operands. The symbol “–”, for example, is both the unary operator for
negation
and the binary operator for subtraction.
Here are some of the common operators that can be used with numeric
expressions:
+ addition
– negation, subtraction
* multiplication
/ division (divided by e.g. 10/5 is 2)
\ division (divided into e.g. 5\10 is 2)
^ exponentiation (e.g., 5^2 is 25)
Hierarchy of arithmetic operations
• First The contents of all parentheses are evaluated first, starting from the

innermost parentheses and working outward.

• Second All exponentials are evaluated, working from left to right

• Third All multiplications and divisions are evaluated, working from left to

right

• Fourth All additions and subtractions are evaluated, starting from left to right

() parentheses
^ exponentiation
– negation
*, /, \ all multiplication and division
+, – addition and subtraction
𝑛𝑥 𝑛 𝑛 − 1 𝑥 2
𝑥 =1+ +
3 2

x= 1+(n*x)/3 + n*(n-1)*x^2/2
1 2 3
4 5
6
7
8
Workspace
The workspace is Matlab’s memory Can manipulate variables stored in the workspace
>> b=10;
>> c=a+b
c=
22
Display contents of workspace
>> whos
Name Size Bytes Class
a 1x1 8 double array
b 1x1 8 double array
c 1x1 8 double array
Grand total is 3 elements using 24 bytes
Delete variable(s) from workspace
>> clear a b; % delete a and b from workspace
>> whos
>> clear all; % delete all variables from workspace
>> clc ; cleaning workspace
>> help whos % displays documentation for the function whos
>> lookfor convert % displays functions with convert in the first help line

Start Matlab help documentation


>> helpdesk
• >> isvarname(‘variable’) % Check variables

– Keyword 1 % Yes keyword


>> iskeyword(‘ variable ‘)
0 % No keyword
Data Type
1- Numeric Data ( Integer number , Real number (float))
2- string Data ( ‘ String ‘ )
3- Logical Data ( True or False )
String
• isletter >> s='a3c4f'
s=
a3c4f
>> isletter(s)
ans =
1 0 1 0 1

• length >> length(s)


ans =
5

• Size >> size(s)


ans =
1 5

>> f={'hi' 'hello' 'bye' 'goodbye'}


f=
'hi' 'hello' 'bye' 'goodbye'
• Char >> char(65)
ans = >> x1='one';
A >> x2='two';
>> x3='hussein';
>> x4='Ali';
• Strvcat , strcat >> strvcat(x1,x2,x3,x4)
ans =
one
two
hussein
Ali
>> strcat(x1,x2,x3,x4)
>> a=435.980 ans =
• Int2str a= onetwohusseinAli
435.9800
• num2str >> int2str(a)
ans =
436
>> num2str(a)
ans =
435.98
Converting Numbers
• dec2bin decimal – to – binary
• Bin2dec binary – to – decimal
• Dec2hex decimal – to – hexadecimal
• Hex2dec hexadecimal – to – decimal
• Dec2base(n,m) decimal – to – any base number 2 – 36
• Base2dec(‘n’,m) any base number 2 – 36 –to- decimal
• n the number m base
>> a=234
a=
• Class(var.) variable type 234
>> class(a)
ans =
double
Time & Date
• Clock time and date
>> t=clock
t=
1.0e+003 *
2.0110 0.0030 0.0240 0.0110 0.0550 0.0315
T= { year month day hour minute seconds }
• Date dd-mm-yyyy
>> date
• datestr(t,n) t= now n= number of table ans =
24-Mar-2011
• Datavec string to digital
• ( year ,month ,day ,hour ,minute, seconds )
• Weekday(date) number of day
• Calendar calendar( date)
Some Common Mathematical Function:

You might also like