Matlab software for math work is often used by mechanical engineers and mechanical engineering students for programming software for solving problems. The software can be used similarly to an engineering equation solver (EES) or it can be taken to the second level of jobs like Simulink. the software can do hard math and solve differential equations. Matlab software for math work is often used by mechanical engineers and mechanical engineering students for programming software for solving problems. The software can be used similarly to an engineering equation solver (EES) or it can be taken to the second level of jobs like Simulink. the software can do hard math and solve differential equations. Matlab software for math work is often used by mechanical engineers and mechanical engineering students for programming software for solving problems. The software can be used similarly to an engineering equation solver (EES) or it can be taken to the second level of jobs like Simulink. the software can do hard math and solve differential equations. Matlab software for math work is often used by mechanical engineers and mechanical engineering students for programming software for solving problems. The software can be used similarly to an engineering equation solver (EES) or it can be taken to the second level of jobs like Simulink. the software can do hard math and solve differential equations. Matlab software for math work is often used by mechanical engineers and mechanical engineering students for programming software for solving problems. The software can be used similarly to an engineering equation solver (EES) or it can be taken to the second level of jobs like Simulink. the software can do hard math and solve differential equations. Matlab software for math work is often used by mechanical engineers and mechanical engineering students for programming software for solving problems. The software can be used similarly to an engineering equation solver (EES) or it can be taken to the second level of jobs like Simulink. the software can do hard math and solve differential equations. Matlab software for math work is often used by mechanical engineers and mechanical engineering students for programming software for solving problems. The software can be used similarly to an engineering equation solver (EES) or it can be taken to the second level of jobs like Simulink. the software can do hard math and solve differential equations. Matlab software for math work is often used by mechanical engineers and mechanical engineering students for programming software for solving problems. The software can be used similarly to an engineering equation solver (EES) or it can be taken to the second level of jobs like Simulink. the software can do hard math and solve differential equations. Matlab software for math work is often used by mechanical engineers and mechanical engineering students for programming software for solving problems.
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
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
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')
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
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
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)
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))
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 )