SlideShare ist ein Scribd-Unternehmen logo
1 von 100
MATLAB
inf infinity
pi Ratio of circle
exp ( degree ) Exponential
sin (radian) cos (radian) tan (radian)
sind (degree) cosd(degree) tand (degree)
X = 3 + 4i
real ( X ) = 3
imag ( X ) = 4
abd ( X ) = 5
angle ( X ) = 0.9273 Angle in radian
conj ( X ) = 3.0000 - 4.0000i
asin (Value) Inverse sin
acos (Value) Inverse cos
atan (Value) Inverse tan
sec cosec cot sinh cosh tanh sech cosech coth
Can add ( d ) to use in degree or add ( a ) in first to get inverse
sqrt ( Value ) Root
log ( Value ) Ln
log10 ( Value )
factorial ( Value )
round ( Value ) Closest number for value
Format long increase accuracy of number
Format short decrease accuracy of number
round ( Value ) Round nearest integer  .5
fix ( Value ) Round toward zero
ceil ( Value ) Round toward infinity
floor ( Value ) Round toward minus infinity
rem ( X , Y ) Returns the remainder
sign ( X ) sign function. [ X>0 -> 1 , X<0 -> -1 , X=0 -> 0 ]
clc Clean command window
clear Delete all variable
clear all Delete all variable
clear A Delete variable A only
who Show all variables
& = and ( x , y ) And
~ = not( x ) Not
| = or( x , y ) Or
xor ( x, y ) Xor
Define Vector (horizontal matrix)
Vector = [ a(1) a(2) a(3) a(4) a(5) ] Define vector by elements
X = [ 12 8 2 34 0 ]
X = [ A : B : C ] Define vector by steps
A : first element B : steps of counter C : last element
X = [ 1 : 2 : 5 ]
X = 1 3 5
X = [ 1 : 5 ] Define with one step by default
X = 1 2 3 4 5
If you write X = [ 12 8 2 34 0 ] on command window without ; (semi Colom)
Matlab will print it .
X = [ 12 8 2 34 0 ]
X =
12 8 2 34 0
But if write X = [ 12 8 2 34 0 ] ; ( with semi Colom ) the matlab will not print
it.
X = [ 12 8 2 34 0 ] ;
Print vector
X = [ 0 1 2 3 4 ]
answer = X ( 1 ) print element a( 1 ) = 0
answer = X ( 1 : 4 ) print from elements from 0 to 3
answer = X ( 1 : end ) print from elements from 0 to 4
answer = X ( 1 : end - 1 ) print from elements from 0 to 3
OPERATIONS ON VECTOR
X = [ 1 2 3 ] Y = [ 4 5 6 ]
prod ( X ) Product all elements of vector
Answer = prod ( X ) = 1 * 2 * 3 * 4 = 24
dot ( X , Y ) Dot product of two vectors
Answer = 1*4 + 2*5 + 3*6 = 32
cross ( X , Y ) Cross product of two vectors
Answer = -3 6 -3
Sum (X) To sum all elements in matrix
Answer = 6
linspace ( A , B , C ) generates C points between A and B
A : first element B : last element C : number of elements
X = linspace ( 0 , 1 , 5 )
= 0 0.2500 0.5000 0.7500 1.0000
X = linspace ( 0 , 1 , 6 )
= 0 0.2000 0.4000 0.6000 0.8000 1.0000
Define matrix
X = [ A B C ; D E F ] or X = [ A , B , C ; D , E , F ]
X = A B C Semi Colom to make new row
D E F
X = [ 1 : 0.5 : 2 ; 2 : -0.5 : 1 ] Different way to define
X = 1 1.5 2
2 1.5 1
Print matrix
X = [ 1 2 5 ; 0 1 7 ; 2 3 4 ]
X (1 , 3) = X ( A ( 1 ) , B ( 3 ) )
= 5
X (end - 1 , end - 2 ) = X ( A ( 2 ) , B ( 1 ) )
= 0
X (end , end) = X ( A ( 3 ) , B ( 3 ) )
= 4
X = [ 1 2 5 ; 0 1 7 ; 2 3 4 ]
X (1 , 1 : 3 ) = X (1 , : )
= 1 2 5
X (1 , [ 1 3 ] ) = X ( A( 1 ) , B( 1 ) X ( A( 1 ) , B( 3 ) )
= 1 5
X ([ 1 3 ] , [ 1 3 ] ) = 1 5
2 4
Delete Elements
Define by index number for row or column then equal with []
X = [ 1 2 5 8 ; 0 1 7 2 ; 6 2 3 4 ]
X = X (1 , : ) =[] Delete first row
= 0 1 7 2
6 2 3 4
X ( : , [ 1 3 ] ) = [] Delete first and third column
= 2 8
1 2
2 4
Add Elements
Define by index number for row or column then equal with elements
X = [ 1 2 5 ; 0 1 7 ; 6 2 3 ]
X = X ( 4 , : ) = [ 0 0 0 ] Adding row in last
= 1 2 5
0 1 7
6 2 3
0 0 0
Operations on matrix
X = [ 1 2 : 3 4 ] Addition on matrix
Answer = X + 5
Answer = 6 7
8 9
rand ( A , B ) Make matrix with random values
A : Number of rows B : Number of columns
X = prod ( 2 , 3 )
0.8147 0.1270 0.6324
0.9058 0.9134 0.0975
E–notation
E – 1 = 0.1
E – 2 = 0.01
9E - 1 = .9
4E - 2 = .04
X = [ 5E-1 7E-2 9e-4]
= 0.5000 0.0700 0.0009
It can be e or E
Complex Numbers
•Both i and j are allowed
•Don’t write I nor J
•Write 3i and don’t write i3
•You can write 3*i or i*3
X = [ 1 + 3i ; 5 - 1i ; 4 + 3j ]
1.0000 + 3.0000i
5.0000 - 1.0000i
4.0000 + 3.0000i
Special matrix
zeros ( Raw , Column ) Elements of matrix are zeros only
X= zeros ( 2 , 3 )
= 0 0 0
0 0 0
ones ( Raw , Column ) Elements of matrix are ones only
X= ones ( 1 , 4 )
= 1 1 1 1
eye( N ) Square matrix ( Identity matrix )
X= eye ( 3 )
= 1 0 0
0 1 0
0 0 1
inv ( X ) = X^-1 Inverse the matrix X=[ 1 2 ; 1 6 ]
answer= inv ( X )
= 1.5000 -0.5000
-0.2500 0.2500
magic ( N ) Make square matrix with random values
X= magic ( 2 ) N : number of raw or column
= 1 3
4 2
reshape ( A , B , C ) Reshape the size of matrix
A : Name of matrix B : Row of new matrix C : Column of new matrix
answer= reshape ( X , 2 ,3 ) // X = [ 1 2 ; 3 4 ; 5 6 ]
= 1 5 4
3 2 6
X = [ 1 2 3 ; 4 5 6 ]
max ( X ) Maximum element in matrix
= 6
min ( X ) Minimum element in matrix
= 1
size ( X ) Size of matrix
= 2 3 2 is raw , 3 is column
length ( X ) Number of columns in matrix
= 3
X = [ 1 2 3 ; 4 5 6 ]
fliplr ( X ) Replace columns from left to right
= 3 2 1
6 5 4
flipud ( X ) Replace rows from up to down
= 4 5 6
1 2 3
X = [ 1 2 3 ; 4 5 6 ]
transpose ( X ) = X ' Make rows to columns
= 1 4
2 5
3 6
rot90 ( X ) Rotate matrix 90 degree anti-clockwise
= 3 6
2 5
1 4
X = [ 1 2 ; 4 5 ] Y = [ 6 7 ; 8 9 ]
Product matrix element by element (square element)
Answer = X . * Y
= 6 1 4
3 2 4 5
Division in matrix element by element
Answer = X . / Y
= 0.1667 0.2857
0.5000 0.5556
X = [ 1 2 ; 4 5 ]
Product and matrix ( cross product )
X*X = X^2
= 7 1 0
1 5 2 2
Product matrix by number
Answer = X * 2
= 2 4
6 8
X = [ 1 2 ; 4 5 ]
Division in matrix by number (using / )
Answer = X / 2
= 0.5000 1.0000
1.5000 2.0000
Division in matrix to solve equations (using  )
By invers matrix ( power -1 )
x + y = 2
x - y = 0
A ^ - 1 * B = inv( A ) * B = A  B  is up of enter key
B * A ^ - 1 = B * inv( A ) = B  A
A X B
A = [ 1 1 ; 1 -1 ] B = [ 2 ; 0 ]
X = A  B = inv (A) * B
answer = 1 x=1 y=1
1
X = [ 1 2 3 ; 4 5 6 ]
Power in matrix power elements
Answer = X.^2
= 1 4 9
16 25 36
Power to product in matrix ( cross product )
Answer = X^2 = X*X Must dimension allow
= Error
A = [ 1 2 ; 3 4 ] B = [ 1 -1 ; 2 -2 ]
Power in matrix by matrix
Answer = A.^B
= 1.0000 0.5000
9.0000 0.0625
The determinant in matrix ( | ‫المحددة‬ | )
det (A) = -2
det (B) = 0
A = [ 1 2 ; 3 4 ] Matrix
B = [ 10 -1 2 -2 ] Vector
sum ( Matrix ) Sum elements in each column
sum ( A ) = 4 6
But in vector the sum for row
sum ( B ) = 9
A = [ 1 2 5 ; 0 1 7 ; 2 3 4 ]
rank ( Matrix ) provides an estimate of the number of linearly independent
rows or columns of matrix.
rank ( A ) = 3
trace ( Matrix ) Sum of the diagonal elements
trace ( A ) = 6
pascal ( N ) Make random square matrix like magic (N)
N : number of row or column
pascal (3) = 1 1 1
1 2 3
1 3 6
diag ( A ) It is main diagonal elements of matrix
A = [ 1 2 5 ; 0 1 7 ; 6 2 3 ]
diag ( A ) = 1
1
3
Remember trace ( A ) get the sum of diag ( A ) = 5
GRAPHICS FUNDAMENTALS
(2D PLOTTING)
X = [ 0 pi/4 pi/2 pi ] Y = sin ( X )
plot ( A , B ) Drawing F(A) horizontal axis and F (B) Vertical axis
plot ( X , Y )
The problem is low
number of data points
no figure .
X vector is 4 points only.
So on Y function.
The solution by increase input data points in X vector by :
X = [ 0 : 0.01 : pi ] ; Y = sin ( X ) ;
plot ( X , Y )
In X vector increase point .Start form 1
to pi using steps 0.01 that data points
( increase accuracy ).
You can put ; after X and Y to don't
print the vector again
X = [ 0 : 1 : 360 ] ; Y = cosd ( X ) ;
plot ( X , Y )
Print sin function start from 0 degree
To 360 degree using 1 degree step.
plot ( X , Y ) = plot ( X , Y ) ;
If you put ; after plot ( X ,Y ) or
don't put the same.
X = [ 0 : 1 : 360 ] ; Y = cosd ( X ) ;
To change color of figure
plot ( X , Y , 'r')
Black ‘k'
Yellow ‘y'
Magenta ‘m'
Cyan ‘c'
Red 'r'
Green ‘g'
Blue ‘b'
X = [ 0 : 1 : 360 ] ; Y = cosd ( X ) ;
To draw grid on figure:
grid on
To turn of the grid:
grid off
Line marking:
Change line to be . + * o >
Put after color in the ‘ '
plot ( X , Y , 'r+')
+ plus sign
o circle
* asterisk
. Point
x cross
s square
d diamond
^ upward pointing triangle
v downward pointing triangle
> right pointing triangle
< left pointing triangle
p five-pointed star (pentagram)
h six-pointed star (hexagram)
Line Styles:
- solid line (default)
-- dashed line
: dotted line
-. dash-dot line
plot ( X , Y , '- - ' )
You can use line style or line mark
without color and can use them together
plot ( X , Y , '* - -' )
Multiple plot:
To plot multiple plots on the same
figure use the command:
hold on
X = [ 0 : . 01 : 10 ] ;
Y = X . ^ 2;
A = 3 * X ;
plot ( X , Y , 'r - -' )
hold on
plot ( X , A , 'c ' )
Multiple plot:
To plot multiple plots on the same
figure use the command:
plot (X,Y,A ,B)
grid on
X = [ 0 : . 01 : 10 ] ;
Y = X . ^ 2 ;
A = 3 * X ;
plot ( X , Y, 'b', X ,A , 'k ')
Adding Titles and Axes Labels :
title xlabel ylabel
grid on
A = [ 0 : . 01 : 10 ] ;
B = A . ^ 3;
plot ( A , B, 'c ')
title (' Two functions ')
xlabel (' A axis ' )
ylabel (' B axis ' )
Don't forget ' ' around text
Legend:
Legend is used when dealing
with multiple plots.
A = [ 0 : . 01 : 10 ] ;
B = A . ^ 2;
C = sqrt ( A ) ;
plot(A ,B, 'r ',A , C , 'g --') ;
legend ( 'square', 'root') ;
Square : first plot < A , B >
Root : second plot < A , C >
legend
subplot ( A , B , C ):
Divide the MATLAB plot window into sub-plot windows
A : number of rows.
B : number of columns.
C : wanted plot to drawing.
X = [ 0:0.01: 2*pi];
Y = sin (X );
s u bplot(1 , 2,1)
p l ot( X ,Y, 'r')
Z = X .^ 3;
s u bplot(1 , 2,2)
p l ot( X ,Z, 'k')
linewidth:
Change line style or line mark line size by :
plot(x,y,'linewidth',2)
plot(X,Y,'g-','linewidth',3)
fontsize:
Change title , xlabel or ylabel font size by :
title ( ' Sample Plot ' ,'fontsize', 14 ) ;
xlabel ( ' X values ' , ' fontsize ' , 14 ) ;
ylabel ( ' Y values ' , ' fontsize ' , 14 ) ;
FUNCTION PLOT
Function plot:
fplot ( @X , [ A Z ] , 'r--+')
X : standard function to drawing
( sin exp sind acos sqrt log )
A : start substitution
Z : end substitution
fplot ( @sin , [ 0 2*pi ] , 'm--')
Function plot:
You can use:
fplot ( 'X' , [ A Z ] , 'r--+')
But will get warning
fplot ( 'sin' , [ 0 2*pi ] , 'm--')
Function plot:
Draw without period
fplot ( @X ,'r--+')
fplot ( 'X' ,'r--+')
fplot ( @exp , 'm')
fplot ( 'exp' , 'm')
Multi-Function plot:
fplot ( @(X)f(X)+g(X) ,[A Z],'r --+')
fplot ( 'f(X)+g(X)' ,[A Z],'r--+')
fplot (@(x)exp(x)-x-2,[-3 3])
fplot ('exp(x)-x-2',[-3 3])
Multi-Function plot:
fplot ( @(X)f(X).*g(X) ,[A Z],'r --+')
fplot ( '(X)f(X)*g(X)' ,[A Z],'r--+')
fplot (@(x)exp(-x).*sin(x),[0 pi])
fplot ('exp(-x)*sin(x)',[0 pi])
Multi-Function plot:
fplot ( @(X)f(X).*g(X) ,[A Z],'r --+')
fplot ( '(X)f(X)*g(X)' ,[A Z],'r--+')
fplot ( @(X)X^+2*X+1 ,[0 20],'r--')
figure : This command make new figure to draw new plot.
X=[ 0 : 1 : 360 ];
Y = sind (X);
Z = cosd (X);
plot(X,Y)
grid on
figure
plot(X,Z)
grid on
Figure 1 Figure 2
GRAPHICS FUNDAMENTALS
(3D PLOTTING)
plot3 (A,B,C,'r--')
t = 0 : .01 : 1;
x = t + 2 ;
y = t - 2 ;
z = t ;
plot3 (x,y,z,'r--')
Plot 3D :
t = -pi:0.01:pi;
X = ones ( length (t) );
Y =5*cos(t);
Z =5*sin(t);
plot3 (X,Y,Z, 'r.')
grid on
Plot 3D :
t= 0 : 0.01 : 10*pi ;
X = cos (t) ;
Y = sin (t) ;
Z = t ;
plot3 (X,Y,Z, 'g','linewidth',3)
grid on
Plot 3D :
X = 0:0.01:3*pi ;
Y1 = zeros (size(X));
Y2 = ones (size(X));
Z1 = sin(X) ;
Z2 = cos(X) ;
plot3 ( X, Y1 , Z1 , 'r' , X ,Y2 , Z2 , 'g' )
grid on
GRAPHICS FUNDAMENTALS
(MESH - SURFACE)
meshgrid ( A , B )
A = -10 : .01 : 10;
B = -10 : .01 : 10;
[ X , Y ] =meshgrid (A ,B);
C = X.^2 + Y.^2;
mesh ( X , Y , C )
grid on
PROGRAMMING
M - FILE
To make m-file press N + ctrl or select New then script .
First rule in programming end every command with ;
X = input (' Text ') ; To receive value form user
X = input (' Text ' , 's') ; To receive text form user
X : The variable that the input will be saved in it.
Text : Told user what to enter.
X = input (' Enter the temperature ') ; Enter 2023
X = input (' Enter your name ' , 's') ; Enter Ahmed
Print text or print variable:
disp ( X ); Print the saved value in variable X
disp ( 'X' ); Print X letter
Area = 2 * pi * 10;
disp ( area ); Print 62.831853
disp ( 'area' ); Print area
You can not print variable and text together
Commands:
%
Any thing will be writhen after % called comment and will not read from matlab
% Matlab is big program
n Get new line (should put it every display command)
disp ( 'area n of circle' ); Print area
of circle
Area = 62.831853 length= 20
If you print integer number use %d else if float number use %f
fprintf ('the area of circle = %f - %d n' , Area, length);
the area of circle = 62.831853 , 20
fprintf ('% 12.3 f ' , Area);
12 Present on space in printing
.3 Present number of fractions after . In float numbers
fprintf ('the area of circle = %0.2f n' , Area);
the area of circle = 62.83
If statement:
if condition
Statement;
elseif condition
Statement;
else
Statement;
end
Result
Price=input('Enter the price=n');
if Price == 2000
Discount = .5;
elseif Price >1000
Discount = .3;
else
Discount = .1;
end
Price = Price – Price * Discount;
price == 2000 used to check equal
for statement:
for variable = condition
Statement;
end
condition : initial value : increment : end value
Result = 1; Factorial command
for X = 1 : 5
Result = Result * X;
end
i =input('Enter number of rows=n');
j =input('Enter number of columns=n');
for A = 1 : i
for B = 1 : j
X( A , B )=input('Enter the value =n');
end
end
disp(X)
while statement:
while condition
Statement;
end
N=input('Enter the number =n'); Result = 1; Factorial command
while N > 1
Result = Result *N;
N=N-1;
end
disp ( Result ) -> 120 if N = 5
switch statement:
switch condition
case 1
Statement;
case 2
Statement;
otherwise
Statement;
end
X=input(‘Enter the case number =n’);
switch X
case 1
disp (' one ');
case 2
disp (' two ');
otherwise
disp (' error ');
end
X=input('Enter the case letter =n','s');
switch X
case 'A'
disp (' a letter ');
case 'B'
disp (' B letter ');
otherwise
disp (‘ error ');
end
Break:
Use break to exit the loop ( for , while ).
for i = 1 : 360
X = sind(i);
if(X==1)
break;
end
end
fprintf (' X = %d n Angle = %d degree n', X , i );
To make function-file select New then function.
function [outputArg1,outputArg2] = untitled (inputArg1,inputArg2)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
outputArg1 = inputArg1;
outputArg2 = inputArg2;
end
First look when open function file
untitled title of function
outputArg1,outputArg2 Outputs
inputArg1,inputArg2 Inputs
function [Area_of_circle] = Area(Diameter)
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
format long
Area_of_circle = (Diameter^2)*.25 * pi;
end
Function called Area calculate area of circle by one input Diameter and one output
Area_of_circle
Area (10) Calling in command
ans = 78.539816339744831
function [Area , Distance] = Circle(Diameter)
% Area of circle Function calculate Area and Circumference
% Distance : Circumference of circle
Area = (Diameter ^ 2 )*.25*pi;
Distance = Diameter*pi;
end
Use next expression when have two or more outputs
[ Area Distance ] = Circle (10)
Area =
78.5398
Distance =
31.4159
Symbols in matlab:
syms X Y Z T Define symbols
A = X^2 – 2*X +1
B = X^2 - 16
C = X^3 + X^2 + 2*X +1
solve ( Equation == 0 ) Solving equations for all degrees
But must define symbols at first syms X
solve ( X - 1 == 0) >>> X = 1
solve (X^2 - 10*X + 16 == 0) >>> X= 2 8
Solving equations together:
Define symbols then put the unknown symbols in [ ] then equal it to
solve (equations in one side equal to zero )
Solve A = X - Y = 6 , B = X + Y = -2
syms X Y
[ X Y ] = solve ( X - Y - 6 , X + Y + 2 )
X = 2 Y = - 4
Differentiation:
Define symbols then diff ( Equation )
syms x
Y = x^3 + x^2 + 2*x +1
diff ( Y )
Answer = 3*x^2 + 2*x + 2
By default matlab differentiate for x ( small letter )
Differentiation for any symbol and more than one:
syms X
Y = X^3 + X^2 + 2*X +1
diff ( Y , X ) Differentiate for X symbol
Answer = 3*X^2 + 2*X + 2
diff ( Y , 2 ) or diff ( diff ( Y ) ) Differentiate twice
Answer = 6*X + 2
Substitution in Differentiation:
subs ( Answer , [ A B X ] , [ 1 2 3] )
syms X A B
Y = A*X^3 + B*X^2 + 2*X +1
Answer = diff ( Y , X )
Answer = 3*A*X^2 + 2*B*X + 2
subs ( Answer , [ A B X ] , [ 1 2 3] )
ans = 41
Simple in Differentiation:
simplify ( Answer ) Simple the answer of differentiation
syms X
Y = X^3 * exp ( - X^2) * sin ( X )
Answer = diff ( Y , X )
Answer = X^3*exp (-X^2)*cos(X) + 3*X^2*exp (-X^2)*sin(X) - 2*X^4*exp(-
X^2)*sin(X)
simplify ( Answer )
ans = X^2*exp(-X^2)*(3*sin(X) + X*cos (X) - 2*X^2*sin(X))
Expand in Differentiation:
expand ( Answer ) Expand the answer of differentiation
Answer = X^2*exp(-X^2)*(3*sin(X) + X*cos (X) - 2*X^2*sin(X))
expand ( Answer )
Answer = X^3*exp (-X^2)*cos(X) + 3*X^2*exp (-X^2)*sin(X) - 2*X^4*exp(-
X^2)*sin(X)
Integration:
Define symbols then int ( Equation )
syms x
Y = x^3 + x^2 + 2*x +1
int ( Y )
Answer = x^4/4 + x^3/3 + x^2 + x
By default matlab integrate for x ( small letter )
Integration for any variable :
int ( Equation , Integration for )
syms X Y
F = Y*X^3 + + Y^2+X^2 + 2*Y^3*X +1
int ( F , Y )
Answer = (X*Y^4)/2 + Y^3/3 + Y*(X^2 + 1) + (X^3*Y^2)/2
For integration more than one time:
int ( int ( F ) )
Answer = (X^5*Y)/20 + X^2*(Y^2/2 + 1/2) + X^4/12 + (X^3*Y^3)/3
Substitution in integration :
int ( Equation , Integration for , Star , End )
syms X
Y = 3*X^2 + 2*X + 1
int ( Y , X , 0 , 1 )
Answer = 3
You can use command subs ( ) :
subs ( Answer , [ X ] , [ 1 ] ) - subs ( Answer , [ X ] , [ 0 ] )
Integration for X then for Y and substitution:
syms X Y
F = Y*X^3 + Y^2 + X^2 + 2*Y^3*X +1
int ( int ( F , X , 0 , 1 ) , Y , 0 , 2 )
Answer = 59/6
Wil integrate F for X and substitution form 0 to 1 then integrate answer for Y
and substitution form 0 to 2
Limits:
Define symbols then limit ( Equation , limit by , limit to )
syms X
Y = sin ( X ) / X
limit ( Y , X , 0 )
Answer = 1
By default matlab do limits without define ( limit by ) if there are one symbol
in equation only ( Equation has X or Y not X and Y )
limit ( Y , 0 )
Partial fraction for numerical:
[ C , D , E ] = residue ( A , B )
A = [ - 4 8 ];
B = [ 1 6 8 ];
[ C D E ] = residue ( A , B )
C = - 12 8 D = - 4 - 2 E = []
You can change A,B,C,D and E with any symbols but the arrangement
required the first is numerator and the second is maqam.
[ r p k ] = residue ( b , a ) == [ C D E ] = residue ( A , B )
When define A and B must arrange power and if power missed equal zero
Y = (x^2+5)/(x^3+2*x)
A = [ 1 0 1 ] ;
B = [ 1 0 2 0 ] ;
[C D E] = residue ( A , B )
C = 0.2500 0.2500 0.5000
D = 0.0000 + 1.4142i 0.0000 - 1.4142i 0.0000 + 0.0000i
E = [ ]
The unification of the stations:
[ A , B ] = residue ( C , D , E )
Must C and D vertical matrix (one column))
C = [ - 12 ; 8 ];
D = [ - 4 ; - 2 ];
E = [];
[ A B ] = residue (C , D , E )
A = - 4 8 B = 1 6 8
Partial fraction for symbols:
syms X
A = - 4*X+8 ;
B = X^2+6*X+8 ;
diff ( int ( A / B ) )
Answer = 8/(X + 2) - 12/(X + 4)
More better diff ( int ( A / B , X ) , X )
Differential equation:
dsolve ('equation')
y’’ + 5* y’ + 8* y =10
D2y+6*Dy+8*y=10
dsolve ('D2y+6*Dy+8*y=10')
Answer = C2*exp(-2*t) + C1*exp (-4*t) + 5/4
Substitution in differential equation:
dsolve (' Equation ','y(0)=0')
y’’ + 5* y’ + 8* y =10
D2y+6*Dy+8*y=10
dsolve ('D2y+6*Dy+8*y=10',‘y(0)=0',‘y(1)=1')
Answer = 5/4 - (exp(-4*t)*(5*exp(2) - exp(4)))/(4*(exp(2) - 1)) - (exp(-
2*t)*(exp(4) - 5))/(4*(exp(2) - 1))

Weitere ähnliche Inhalte

Ähnlich wie Matlab level 1.pptx

2010 assessment review_homework_1-4-done
2010 assessment review_homework_1-4-done2010 assessment review_homework_1-4-done
2010 assessment review_homework_1-4-done
jendailey
 

Ähnlich wie Matlab level 1.pptx (20)

Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
Intro to Matlab programming
Intro to Matlab programmingIntro to Matlab programming
Intro to Matlab programming
 
Matlab cheatsheet
Matlab cheatsheetMatlab cheatsheet
Matlab cheatsheet
 
2010 assessment review_homework_1-4-done
2010 assessment review_homework_1-4-done2010 assessment review_homework_1-4-done
2010 assessment review_homework_1-4-done
 
MATLAB-Introd.ppt
MATLAB-Introd.pptMATLAB-Introd.ppt
MATLAB-Introd.ppt
 
Solution of matlab chapter 3
Solution of matlab chapter 3Solution of matlab chapter 3
Solution of matlab chapter 3
 
Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4Introduction to matlab lecture 2 of 4
Introduction to matlab lecture 2 of 4
 
bobok
bobokbobok
bobok
 
MATHS SYMBOLS.pdf
MATHS SYMBOLS.pdfMATHS SYMBOLS.pdf
MATHS SYMBOLS.pdf
 
Array
ArrayArray
Array
 
BUilt in Functions and Simple programs in R.pdf
BUilt in Functions and Simple programs in R.pdfBUilt in Functions and Simple programs in R.pdf
BUilt in Functions and Simple programs in R.pdf
 
Mat lab day 1
Mat lab day 1Mat lab day 1
Mat lab day 1
 
Matrices & Determinants.pdf
Matrices & Determinants.pdfMatrices & Determinants.pdf
Matrices & Determinants.pdf
 
Basics of Matlab for students and faculty
Basics of Matlab for students and facultyBasics of Matlab for students and faculty
Basics of Matlab for students and faculty
 
mat lab introduction and basics to learn
mat lab introduction and basics to learnmat lab introduction and basics to learn
mat lab introduction and basics to learn
 
Matlab ploting
Matlab plotingMatlab ploting
Matlab ploting
 
Linear Algebra Assignment help
Linear Algebra Assignment helpLinear Algebra Assignment help
Linear Algebra Assignment help
 
Assignments for class XII
Assignments for class XIIAssignments for class XII
Assignments for class XII
 
Add math may june 2016 p1
Add math may june 2016 p1Add math may june 2016 p1
Add math may june 2016 p1
 
H 2008 2011
H 2008   2011H 2008   2011
H 2008 2011
 

Kürzlich hochgeladen

Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 

Kürzlich hochgeladen (20)

UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 

Matlab level 1.pptx

  • 2. inf infinity pi Ratio of circle exp ( degree ) Exponential sin (radian) cos (radian) tan (radian) sind (degree) cosd(degree) tand (degree) X = 3 + 4i real ( X ) = 3 imag ( X ) = 4 abd ( X ) = 5 angle ( X ) = 0.9273 Angle in radian conj ( X ) = 3.0000 - 4.0000i
  • 3. asin (Value) Inverse sin acos (Value) Inverse cos atan (Value) Inverse tan sec cosec cot sinh cosh tanh sech cosech coth Can add ( d ) to use in degree or add ( a ) in first to get inverse sqrt ( Value ) Root log ( Value ) Ln log10 ( Value ) factorial ( Value ) round ( Value ) Closest number for value
  • 4. Format long increase accuracy of number Format short decrease accuracy of number round ( Value ) Round nearest integer  .5 fix ( Value ) Round toward zero ceil ( Value ) Round toward infinity floor ( Value ) Round toward minus infinity rem ( X , Y ) Returns the remainder sign ( X ) sign function. [ X>0 -> 1 , X<0 -> -1 , X=0 -> 0 ]
  • 5. clc Clean command window clear Delete all variable clear all Delete all variable clear A Delete variable A only who Show all variables & = and ( x , y ) And ~ = not( x ) Not | = or( x , y ) Or xor ( x, y ) Xor
  • 7. Vector = [ a(1) a(2) a(3) a(4) a(5) ] Define vector by elements X = [ 12 8 2 34 0 ] X = [ A : B : C ] Define vector by steps A : first element B : steps of counter C : last element X = [ 1 : 2 : 5 ] X = 1 3 5 X = [ 1 : 5 ] Define with one step by default X = 1 2 3 4 5
  • 8. If you write X = [ 12 8 2 34 0 ] on command window without ; (semi Colom) Matlab will print it . X = [ 12 8 2 34 0 ] X = 12 8 2 34 0 But if write X = [ 12 8 2 34 0 ] ; ( with semi Colom ) the matlab will not print it. X = [ 12 8 2 34 0 ] ;
  • 9. Print vector X = [ 0 1 2 3 4 ] answer = X ( 1 ) print element a( 1 ) = 0 answer = X ( 1 : 4 ) print from elements from 0 to 3 answer = X ( 1 : end ) print from elements from 0 to 4 answer = X ( 1 : end - 1 ) print from elements from 0 to 3
  • 11. X = [ 1 2 3 ] Y = [ 4 5 6 ] prod ( X ) Product all elements of vector Answer = prod ( X ) = 1 * 2 * 3 * 4 = 24 dot ( X , Y ) Dot product of two vectors Answer = 1*4 + 2*5 + 3*6 = 32 cross ( X , Y ) Cross product of two vectors Answer = -3 6 -3 Sum (X) To sum all elements in matrix Answer = 6
  • 12. linspace ( A , B , C ) generates C points between A and B A : first element B : last element C : number of elements X = linspace ( 0 , 1 , 5 ) = 0 0.2500 0.5000 0.7500 1.0000 X = linspace ( 0 , 1 , 6 ) = 0 0.2000 0.4000 0.6000 0.8000 1.0000
  • 13. Define matrix X = [ A B C ; D E F ] or X = [ A , B , C ; D , E , F ] X = A B C Semi Colom to make new row D E F X = [ 1 : 0.5 : 2 ; 2 : -0.5 : 1 ] Different way to define X = 1 1.5 2 2 1.5 1
  • 14. Print matrix X = [ 1 2 5 ; 0 1 7 ; 2 3 4 ] X (1 , 3) = X ( A ( 1 ) , B ( 3 ) ) = 5 X (end - 1 , end - 2 ) = X ( A ( 2 ) , B ( 1 ) ) = 0 X (end , end) = X ( A ( 3 ) , B ( 3 ) ) = 4
  • 15. X = [ 1 2 5 ; 0 1 7 ; 2 3 4 ] X (1 , 1 : 3 ) = X (1 , : ) = 1 2 5 X (1 , [ 1 3 ] ) = X ( A( 1 ) , B( 1 ) X ( A( 1 ) , B( 3 ) ) = 1 5 X ([ 1 3 ] , [ 1 3 ] ) = 1 5 2 4
  • 16. Delete Elements Define by index number for row or column then equal with [] X = [ 1 2 5 8 ; 0 1 7 2 ; 6 2 3 4 ] X = X (1 , : ) =[] Delete first row = 0 1 7 2 6 2 3 4 X ( : , [ 1 3 ] ) = [] Delete first and third column = 2 8 1 2 2 4
  • 17. Add Elements Define by index number for row or column then equal with elements X = [ 1 2 5 ; 0 1 7 ; 6 2 3 ] X = X ( 4 , : ) = [ 0 0 0 ] Adding row in last = 1 2 5 0 1 7 6 2 3 0 0 0
  • 18. Operations on matrix X = [ 1 2 : 3 4 ] Addition on matrix Answer = X + 5 Answer = 6 7 8 9 rand ( A , B ) Make matrix with random values A : Number of rows B : Number of columns X = prod ( 2 , 3 ) 0.8147 0.1270 0.6324 0.9058 0.9134 0.0975
  • 19. E–notation E – 1 = 0.1 E – 2 = 0.01 9E - 1 = .9 4E - 2 = .04 X = [ 5E-1 7E-2 9e-4] = 0.5000 0.0700 0.0009 It can be e or E
  • 20. Complex Numbers •Both i and j are allowed •Don’t write I nor J •Write 3i and don’t write i3 •You can write 3*i or i*3 X = [ 1 + 3i ; 5 - 1i ; 4 + 3j ] 1.0000 + 3.0000i 5.0000 - 1.0000i 4.0000 + 3.0000i
  • 22. zeros ( Raw , Column ) Elements of matrix are zeros only X= zeros ( 2 , 3 ) = 0 0 0 0 0 0 ones ( Raw , Column ) Elements of matrix are ones only X= ones ( 1 , 4 ) = 1 1 1 1
  • 23. eye( N ) Square matrix ( Identity matrix ) X= eye ( 3 ) = 1 0 0 0 1 0 0 0 1 inv ( X ) = X^-1 Inverse the matrix X=[ 1 2 ; 1 6 ] answer= inv ( X ) = 1.5000 -0.5000 -0.2500 0.2500
  • 24. magic ( N ) Make square matrix with random values X= magic ( 2 ) N : number of raw or column = 1 3 4 2 reshape ( A , B , C ) Reshape the size of matrix A : Name of matrix B : Row of new matrix C : Column of new matrix answer= reshape ( X , 2 ,3 ) // X = [ 1 2 ; 3 4 ; 5 6 ] = 1 5 4 3 2 6
  • 25. X = [ 1 2 3 ; 4 5 6 ] max ( X ) Maximum element in matrix = 6 min ( X ) Minimum element in matrix = 1 size ( X ) Size of matrix = 2 3 2 is raw , 3 is column length ( X ) Number of columns in matrix = 3
  • 26. X = [ 1 2 3 ; 4 5 6 ] fliplr ( X ) Replace columns from left to right = 3 2 1 6 5 4 flipud ( X ) Replace rows from up to down = 4 5 6 1 2 3
  • 27. X = [ 1 2 3 ; 4 5 6 ] transpose ( X ) = X ' Make rows to columns = 1 4 2 5 3 6 rot90 ( X ) Rotate matrix 90 degree anti-clockwise = 3 6 2 5 1 4
  • 28. X = [ 1 2 ; 4 5 ] Y = [ 6 7 ; 8 9 ] Product matrix element by element (square element) Answer = X . * Y = 6 1 4 3 2 4 5 Division in matrix element by element Answer = X . / Y = 0.1667 0.2857 0.5000 0.5556
  • 29. X = [ 1 2 ; 4 5 ] Product and matrix ( cross product ) X*X = X^2 = 7 1 0 1 5 2 2 Product matrix by number Answer = X * 2 = 2 4 6 8
  • 30. X = [ 1 2 ; 4 5 ] Division in matrix by number (using / ) Answer = X / 2 = 0.5000 1.0000 1.5000 2.0000 Division in matrix to solve equations (using ) By invers matrix ( power -1 ) x + y = 2 x - y = 0
  • 31. A ^ - 1 * B = inv( A ) * B = A B is up of enter key B * A ^ - 1 = B * inv( A ) = B A A X B A = [ 1 1 ; 1 -1 ] B = [ 2 ; 0 ] X = A B = inv (A) * B answer = 1 x=1 y=1 1
  • 32. X = [ 1 2 3 ; 4 5 6 ] Power in matrix power elements Answer = X.^2 = 1 4 9 16 25 36 Power to product in matrix ( cross product ) Answer = X^2 = X*X Must dimension allow = Error
  • 33. A = [ 1 2 ; 3 4 ] B = [ 1 -1 ; 2 -2 ] Power in matrix by matrix Answer = A.^B = 1.0000 0.5000 9.0000 0.0625 The determinant in matrix ( | ‫المحددة‬ | ) det (A) = -2 det (B) = 0
  • 34. A = [ 1 2 ; 3 4 ] Matrix B = [ 10 -1 2 -2 ] Vector sum ( Matrix ) Sum elements in each column sum ( A ) = 4 6 But in vector the sum for row sum ( B ) = 9
  • 35. A = [ 1 2 5 ; 0 1 7 ; 2 3 4 ] rank ( Matrix ) provides an estimate of the number of linearly independent rows or columns of matrix. rank ( A ) = 3 trace ( Matrix ) Sum of the diagonal elements trace ( A ) = 6
  • 36. pascal ( N ) Make random square matrix like magic (N) N : number of row or column pascal (3) = 1 1 1 1 2 3 1 3 6 diag ( A ) It is main diagonal elements of matrix A = [ 1 2 5 ; 0 1 7 ; 6 2 3 ] diag ( A ) = 1 1 3 Remember trace ( A ) get the sum of diag ( A ) = 5
  • 38. X = [ 0 pi/4 pi/2 pi ] Y = sin ( X ) plot ( A , B ) Drawing F(A) horizontal axis and F (B) Vertical axis plot ( X , Y ) The problem is low number of data points no figure . X vector is 4 points only. So on Y function.
  • 39. The solution by increase input data points in X vector by : X = [ 0 : 0.01 : pi ] ; Y = sin ( X ) ; plot ( X , Y ) In X vector increase point .Start form 1 to pi using steps 0.01 that data points ( increase accuracy ). You can put ; after X and Y to don't print the vector again
  • 40. X = [ 0 : 1 : 360 ] ; Y = cosd ( X ) ; plot ( X , Y ) Print sin function start from 0 degree To 360 degree using 1 degree step. plot ( X , Y ) = plot ( X , Y ) ; If you put ; after plot ( X ,Y ) or don't put the same.
  • 41. X = [ 0 : 1 : 360 ] ; Y = cosd ( X ) ; To change color of figure plot ( X , Y , 'r') Black ‘k' Yellow ‘y' Magenta ‘m' Cyan ‘c' Red 'r' Green ‘g' Blue ‘b'
  • 42. X = [ 0 : 1 : 360 ] ; Y = cosd ( X ) ; To draw grid on figure: grid on To turn of the grid: grid off Line marking: Change line to be . + * o > Put after color in the ‘ ' plot ( X , Y , 'r+')
  • 43. + plus sign o circle * asterisk . Point x cross s square d diamond ^ upward pointing triangle v downward pointing triangle > right pointing triangle < left pointing triangle p five-pointed star (pentagram) h six-pointed star (hexagram)
  • 44. Line Styles: - solid line (default) -- dashed line : dotted line -. dash-dot line plot ( X , Y , '- - ' ) You can use line style or line mark without color and can use them together plot ( X , Y , '* - -' )
  • 45. Multiple plot: To plot multiple plots on the same figure use the command: hold on X = [ 0 : . 01 : 10 ] ; Y = X . ^ 2; A = 3 * X ; plot ( X , Y , 'r - -' ) hold on plot ( X , A , 'c ' )
  • 46. Multiple plot: To plot multiple plots on the same figure use the command: plot (X,Y,A ,B) grid on X = [ 0 : . 01 : 10 ] ; Y = X . ^ 2 ; A = 3 * X ; plot ( X , Y, 'b', X ,A , 'k ')
  • 47. Adding Titles and Axes Labels : title xlabel ylabel grid on A = [ 0 : . 01 : 10 ] ; B = A . ^ 3; plot ( A , B, 'c ') title (' Two functions ') xlabel (' A axis ' ) ylabel (' B axis ' ) Don't forget ' ' around text
  • 48. Legend: Legend is used when dealing with multiple plots. A = [ 0 : . 01 : 10 ] ; B = A . ^ 2; C = sqrt ( A ) ; plot(A ,B, 'r ',A , C , 'g --') ; legend ( 'square', 'root') ; Square : first plot < A , B > Root : second plot < A , C > legend
  • 49. subplot ( A , B , C ): Divide the MATLAB plot window into sub-plot windows A : number of rows. B : number of columns. C : wanted plot to drawing. X = [ 0:0.01: 2*pi]; Y = sin (X ); s u bplot(1 , 2,1) p l ot( X ,Y, 'r') Z = X .^ 3; s u bplot(1 , 2,2) p l ot( X ,Z, 'k')
  • 50. linewidth: Change line style or line mark line size by : plot(x,y,'linewidth',2) plot(X,Y,'g-','linewidth',3) fontsize: Change title , xlabel or ylabel font size by : title ( ' Sample Plot ' ,'fontsize', 14 ) ; xlabel ( ' X values ' , ' fontsize ' , 14 ) ; ylabel ( ' Y values ' , ' fontsize ' , 14 ) ;
  • 52. Function plot: fplot ( @X , [ A Z ] , 'r--+') X : standard function to drawing ( sin exp sind acos sqrt log ) A : start substitution Z : end substitution fplot ( @sin , [ 0 2*pi ] , 'm--')
  • 53. Function plot: You can use: fplot ( 'X' , [ A Z ] , 'r--+') But will get warning fplot ( 'sin' , [ 0 2*pi ] , 'm--')
  • 54. Function plot: Draw without period fplot ( @X ,'r--+') fplot ( 'X' ,'r--+') fplot ( @exp , 'm') fplot ( 'exp' , 'm')
  • 55. Multi-Function plot: fplot ( @(X)f(X)+g(X) ,[A Z],'r --+') fplot ( 'f(X)+g(X)' ,[A Z],'r--+') fplot (@(x)exp(x)-x-2,[-3 3]) fplot ('exp(x)-x-2',[-3 3])
  • 56. Multi-Function plot: fplot ( @(X)f(X).*g(X) ,[A Z],'r --+') fplot ( '(X)f(X)*g(X)' ,[A Z],'r--+') fplot (@(x)exp(-x).*sin(x),[0 pi]) fplot ('exp(-x)*sin(x)',[0 pi])
  • 57. Multi-Function plot: fplot ( @(X)f(X).*g(X) ,[A Z],'r --+') fplot ( '(X)f(X)*g(X)' ,[A Z],'r--+') fplot ( @(X)X^+2*X+1 ,[0 20],'r--')
  • 58. figure : This command make new figure to draw new plot. X=[ 0 : 1 : 360 ]; Y = sind (X); Z = cosd (X); plot(X,Y) grid on figure plot(X,Z) grid on Figure 1 Figure 2
  • 60. plot3 (A,B,C,'r--') t = 0 : .01 : 1; x = t + 2 ; y = t - 2 ; z = t ; plot3 (x,y,z,'r--')
  • 61. Plot 3D : t = -pi:0.01:pi; X = ones ( length (t) ); Y =5*cos(t); Z =5*sin(t); plot3 (X,Y,Z, 'r.') grid on
  • 62. Plot 3D : t= 0 : 0.01 : 10*pi ; X = cos (t) ; Y = sin (t) ; Z = t ; plot3 (X,Y,Z, 'g','linewidth',3) grid on
  • 63. Plot 3D : X = 0:0.01:3*pi ; Y1 = zeros (size(X)); Y2 = ones (size(X)); Z1 = sin(X) ; Z2 = cos(X) ; plot3 ( X, Y1 , Z1 , 'r' , X ,Y2 , Z2 , 'g' ) grid on
  • 65. meshgrid ( A , B ) A = -10 : .01 : 10; B = -10 : .01 : 10; [ X , Y ] =meshgrid (A ,B); C = X.^2 + Y.^2; mesh ( X , Y , C ) grid on
  • 67. To make m-file press N + ctrl or select New then script . First rule in programming end every command with ; X = input (' Text ') ; To receive value form user X = input (' Text ' , 's') ; To receive text form user X : The variable that the input will be saved in it. Text : Told user what to enter. X = input (' Enter the temperature ') ; Enter 2023 X = input (' Enter your name ' , 's') ; Enter Ahmed
  • 68. Print text or print variable: disp ( X ); Print the saved value in variable X disp ( 'X' ); Print X letter Area = 2 * pi * 10; disp ( area ); Print 62.831853 disp ( 'area' ); Print area You can not print variable and text together
  • 69. Commands: % Any thing will be writhen after % called comment and will not read from matlab % Matlab is big program n Get new line (should put it every display command) disp ( 'area n of circle' ); Print area of circle
  • 70. Area = 62.831853 length= 20 If you print integer number use %d else if float number use %f fprintf ('the area of circle = %f - %d n' , Area, length); the area of circle = 62.831853 , 20 fprintf ('% 12.3 f ' , Area); 12 Present on space in printing .3 Present number of fractions after . In float numbers fprintf ('the area of circle = %0.2f n' , Area); the area of circle = 62.83
  • 71. If statement: if condition Statement; elseif condition Statement; else Statement; end Result
  • 72. Price=input('Enter the price=n'); if Price == 2000 Discount = .5; elseif Price >1000 Discount = .3; else Discount = .1; end Price = Price – Price * Discount; price == 2000 used to check equal
  • 73. for statement: for variable = condition Statement; end condition : initial value : increment : end value Result = 1; Factorial command for X = 1 : 5 Result = Result * X; end
  • 74. i =input('Enter number of rows=n'); j =input('Enter number of columns=n'); for A = 1 : i for B = 1 : j X( A , B )=input('Enter the value =n'); end end disp(X)
  • 75. while statement: while condition Statement; end N=input('Enter the number =n'); Result = 1; Factorial command while N > 1 Result = Result *N; N=N-1; end disp ( Result ) -> 120 if N = 5
  • 76. switch statement: switch condition case 1 Statement; case 2 Statement; otherwise Statement; end
  • 77. X=input(‘Enter the case number =n’); switch X case 1 disp (' one '); case 2 disp (' two '); otherwise disp (' error '); end
  • 78. X=input('Enter the case letter =n','s'); switch X case 'A' disp (' a letter '); case 'B' disp (' B letter '); otherwise disp (‘ error '); end
  • 79. Break: Use break to exit the loop ( for , while ). for i = 1 : 360 X = sind(i); if(X==1) break; end end fprintf (' X = %d n Angle = %d degree n', X , i );
  • 80. To make function-file select New then function. function [outputArg1,outputArg2] = untitled (inputArg1,inputArg2) %UNTITLED Summary of this function goes here % Detailed explanation goes here outputArg1 = inputArg1; outputArg2 = inputArg2; end First look when open function file untitled title of function outputArg1,outputArg2 Outputs inputArg1,inputArg2 Inputs
  • 81. function [Area_of_circle] = Area(Diameter) %UNTITLED3 Summary of this function goes here % Detailed explanation goes here format long Area_of_circle = (Diameter^2)*.25 * pi; end Function called Area calculate area of circle by one input Diameter and one output Area_of_circle Area (10) Calling in command ans = 78.539816339744831
  • 82. function [Area , Distance] = Circle(Diameter) % Area of circle Function calculate Area and Circumference % Distance : Circumference of circle Area = (Diameter ^ 2 )*.25*pi; Distance = Diameter*pi; end Use next expression when have two or more outputs [ Area Distance ] = Circle (10) Area = 78.5398 Distance = 31.4159
  • 83. Symbols in matlab: syms X Y Z T Define symbols A = X^2 – 2*X +1 B = X^2 - 16 C = X^3 + X^2 + 2*X +1 solve ( Equation == 0 ) Solving equations for all degrees But must define symbols at first syms X solve ( X - 1 == 0) >>> X = 1 solve (X^2 - 10*X + 16 == 0) >>> X= 2 8
  • 84. Solving equations together: Define symbols then put the unknown symbols in [ ] then equal it to solve (equations in one side equal to zero ) Solve A = X - Y = 6 , B = X + Y = -2 syms X Y [ X Y ] = solve ( X - Y - 6 , X + Y + 2 ) X = 2 Y = - 4
  • 85. Differentiation: Define symbols then diff ( Equation ) syms x Y = x^3 + x^2 + 2*x +1 diff ( Y ) Answer = 3*x^2 + 2*x + 2 By default matlab differentiate for x ( small letter )
  • 86. Differentiation for any symbol and more than one: syms X Y = X^3 + X^2 + 2*X +1 diff ( Y , X ) Differentiate for X symbol Answer = 3*X^2 + 2*X + 2 diff ( Y , 2 ) or diff ( diff ( Y ) ) Differentiate twice Answer = 6*X + 2
  • 87. Substitution in Differentiation: subs ( Answer , [ A B X ] , [ 1 2 3] ) syms X A B Y = A*X^3 + B*X^2 + 2*X +1 Answer = diff ( Y , X ) Answer = 3*A*X^2 + 2*B*X + 2 subs ( Answer , [ A B X ] , [ 1 2 3] ) ans = 41
  • 88. Simple in Differentiation: simplify ( Answer ) Simple the answer of differentiation syms X Y = X^3 * exp ( - X^2) * sin ( X ) Answer = diff ( Y , X ) Answer = X^3*exp (-X^2)*cos(X) + 3*X^2*exp (-X^2)*sin(X) - 2*X^4*exp(- X^2)*sin(X) simplify ( Answer ) ans = X^2*exp(-X^2)*(3*sin(X) + X*cos (X) - 2*X^2*sin(X))
  • 89. Expand in Differentiation: expand ( Answer ) Expand the answer of differentiation Answer = X^2*exp(-X^2)*(3*sin(X) + X*cos (X) - 2*X^2*sin(X)) expand ( Answer ) Answer = X^3*exp (-X^2)*cos(X) + 3*X^2*exp (-X^2)*sin(X) - 2*X^4*exp(- X^2)*sin(X)
  • 90. Integration: Define symbols then int ( Equation ) syms x Y = x^3 + x^2 + 2*x +1 int ( Y ) Answer = x^4/4 + x^3/3 + x^2 + x By default matlab integrate for x ( small letter )
  • 91. Integration for any variable : int ( Equation , Integration for ) syms X Y F = Y*X^3 + + Y^2+X^2 + 2*Y^3*X +1 int ( F , Y ) Answer = (X*Y^4)/2 + Y^3/3 + Y*(X^2 + 1) + (X^3*Y^2)/2 For integration more than one time: int ( int ( F ) ) Answer = (X^5*Y)/20 + X^2*(Y^2/2 + 1/2) + X^4/12 + (X^3*Y^3)/3
  • 92. Substitution in integration : int ( Equation , Integration for , Star , End ) syms X Y = 3*X^2 + 2*X + 1 int ( Y , X , 0 , 1 ) Answer = 3 You can use command subs ( ) : subs ( Answer , [ X ] , [ 1 ] ) - subs ( Answer , [ X ] , [ 0 ] )
  • 93. Integration for X then for Y and substitution: syms X Y F = Y*X^3 + Y^2 + X^2 + 2*Y^3*X +1 int ( int ( F , X , 0 , 1 ) , Y , 0 , 2 ) Answer = 59/6 Wil integrate F for X and substitution form 0 to 1 then integrate answer for Y and substitution form 0 to 2
  • 94. Limits: Define symbols then limit ( Equation , limit by , limit to ) syms X Y = sin ( X ) / X limit ( Y , X , 0 ) Answer = 1 By default matlab do limits without define ( limit by ) if there are one symbol in equation only ( Equation has X or Y not X and Y ) limit ( Y , 0 )
  • 95. Partial fraction for numerical: [ C , D , E ] = residue ( A , B ) A = [ - 4 8 ]; B = [ 1 6 8 ]; [ C D E ] = residue ( A , B ) C = - 12 8 D = - 4 - 2 E = [] You can change A,B,C,D and E with any symbols but the arrangement required the first is numerator and the second is maqam. [ r p k ] = residue ( b , a ) == [ C D E ] = residue ( A , B )
  • 96. When define A and B must arrange power and if power missed equal zero Y = (x^2+5)/(x^3+2*x) A = [ 1 0 1 ] ; B = [ 1 0 2 0 ] ; [C D E] = residue ( A , B ) C = 0.2500 0.2500 0.5000 D = 0.0000 + 1.4142i 0.0000 - 1.4142i 0.0000 + 0.0000i E = [ ]
  • 97. The unification of the stations: [ A , B ] = residue ( C , D , E ) Must C and D vertical matrix (one column)) C = [ - 12 ; 8 ]; D = [ - 4 ; - 2 ]; E = []; [ A B ] = residue (C , D , E ) A = - 4 8 B = 1 6 8
  • 98. Partial fraction for symbols: syms X A = - 4*X+8 ; B = X^2+6*X+8 ; diff ( int ( A / B ) ) Answer = 8/(X + 2) - 12/(X + 4) More better diff ( int ( A / B , X ) , X )
  • 99. Differential equation: dsolve ('equation') y’’ + 5* y’ + 8* y =10 D2y+6*Dy+8*y=10 dsolve ('D2y+6*Dy+8*y=10') Answer = C2*exp(-2*t) + C1*exp (-4*t) + 5/4
  • 100. Substitution in differential equation: dsolve (' Equation ','y(0)=0') y’’ + 5* y’ + 8* y =10 D2y+6*Dy+8*y=10 dsolve ('D2y+6*Dy+8*y=10',‘y(0)=0',‘y(1)=1') Answer = 5/4 - (exp(-4*t)*(5*exp(2) - exp(4)))/(4*(exp(2) - 1)) - (exp(- 2*t)*(exp(4) - 5))/(4*(exp(2) - 1))