Atlab: Programming
Atlab: Programming
Programming
By : Ahmed Moawad
LET’S BEGIN……
MATLAB is a tool that simplify the programming than
any other programming language like c , c# , …….
>> help ;
this function gives you a very useful tutorial ,
and information about using MATLAB in every
applications.
1
FIRST TIME : HOW TO INPUT A MATRIX OR
VECTOR IN MATLAB
Vector :
>> x = [1 2 3 4]
x=1 2 3 4
o Matrix :
>> y = [1 2 3 ; 5 1 4 ; 2 3 1]
y=1 2 3
5 1 4
2 3 1
2
CHARACTERISTICS OF MATRIX
Transpose :
>> xt = x ’ convert the columns to rows and vice versa
x=1
2
3
4
>> x = [1 : 5]
>>x = [1 : 2 : 5]
x=1 3 5
3
CONTINUE……
>>size (y) Number of columns and rows
>>length(y) The tallest dimension(row , column) of Matrix
1
>>z = y( 2 , 2:3 ) Choose the second & third columns.
5
z=1 4 Choose the second row.
>>zeros(M ,N); Matrix consists of zeros
>>ones(M,N); Matrix consists of ones
>>eye(M,N) ; The main diameter of Matrix is ones , others zeros
4
OPERATORS
Scalar Arithmetic Operations :
>> z = x * y; multiply every element in x to its corresponding in y.
+ .+ Summation
- .- Subtraction
/ ./ Division
^ .^ Exponential
5
FUNCTIONS BREAK (1):
>>sign(x) ; If an element in matrix x is +ve returns 1 ,-ve returns
-1.
>>exp (2) The exponential function.
ans = 7.3891
round (x); Approximate the number x to the nearest number
7
CONTINUE … .
While loop:
i.e.:
>>b =0; initial value of b
>>while b<4 carry out if b <4 , stop if else
b=b+1;
end
>>weight = b*2.2;
>>disp(b);
>>sprintf(‘The weight equals %f ’ , weight)
weight is float type
8
CONTINUE … ..
If Statement:
i.e.:
degree =input(‘please insert the degree(0-100): ’)
fn. that make user input the value which preferred
if degree>= 85
disp(‘Excellent’)
elseif( degree>= 75 & degree <85)
disp(‘very good’)
elseif( degree>= 65 & degree <75)
disp(‘good’)
elseif( degree>= 50 & degree <65)
disp(‘pass’)
else
disp(‘fail’)
end 9
CONTINUE … …
Switch …case:
i.e.:
month= input (‘please input the month(1-12): ’)
switch month
case { 1,3,5,7,8,10,12} more than one case>> {1,2,…..}
disp (31)
case{4 , 6 ,9 ,11}
disp (30)
case 2 only one case>> 1
disp (28)
end
10
FUNCTION BREAK(2):
>>Str = ‘ the sentence you want to write’
>>w = str [ 1 2 5 7 9]
w = thsne
x(2) = 4 change the second element value in the matrix x to 4.
X(3) = [ ] delete the third element .
mean(x) The mean of elements of x .
std(x) The standard diversion of elements of x.
[theta phi r ] = cart2sph [2 , 3 , 5]
any names indicates the sph. Conversion fn. the values of Cartesian coordinates
11
GRAPHS
Line plot :
i.e.:
>>t = [ 0 : 10 ];
>> y = sin(t); y is sinusoidal wave on t
>>Figure(1) Open figure , name it 1
>>plot (t,y) plot t(h-axis) versus y (v-axis) in figure.
>>xlabel(‘time’) write 9time) under h-axis
>>ylabel(‘input’) write (input) beside v-axis
>>title (‘Gain’) Make a title for plot
12
CONTINUE … .
Bar Graph:
i.e.:
>>x = -3 : 1 : 3;
>>y = x.^2;
>>bar(x,y) Bar Graph
>>figure(1)
>>subplot 221 divide the figure into number of plots
>>z= magic (3); special function
>>subplot 222 row no. column no. position
>>bar(z)
>>subplot (2,2,3)
>> bar(z , ‘grouped’) Style type
>>subplot(2,2,4)
>>bar (z , ‘stacked’)
13
CONTINUE … ..
Histogram :
>> hist (z , 7 ) Number of intervals in histogram
Pie Graph :
>>z = [10 4 5 8 2];
>>pie(z)
Polar Graph :
>>polar(t , y)
14
CONTINUE … …
Scatter plot :
>>x =[1:10];
>> y = 2.*rand (1,10)
>>subplot 221
>>scatter(x ,y) scatter points on plot
>>subplot 222
>> stem ( x , y) scatter points connected with the h-axis
>>subplot 223
>>scatter(x ,y , 3 , ‘y’ ) Mark color (yellow)
size of mark (scatter point)
15
CONTINUE … ….
20
THANK YOU