After the completion of this workshop, you will be able to:
1. Create and working with Arrays of numbers
2. Create simple plots
3. Plot the given function
4. Do Symbolic Computation
5. Understand SIMULINK
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
“Introduction to MATLAB & SIMULINK”
1. “Introduction to MATLAB & SIMULINK”
Presented by
Amarjeetsingh Thakur
Asst. Professor
Dept. of Electronics & Communication Engg.
KLETU ,Hubli, Karnataka, India
1
4. Creating and working with Arrays of
numbers
>> x=[4 5 6 ] % x is a row vector with three
elements
>> y=[1 2 3]
>> z=x+y % z=5 7 9
>> z=x-y %z= 3 3 3
>> j=[1;2;3] % x is a column vector with three
elements
>> z=x.*y % z= 4 10 18
>> z=x./y % z=4.0000 2.5000 2.0000
4
5. Contd…
x=linspace(0,10,5) % x = 0 2.5000 5.0000 7.5000
10.0000
Create a vector x with 5 elements linearly
spaced between 0 and 10
z=sqrt(x) % Square root of x
5
7. Creating simp le p lots
%Draw a circle of unit radius
theta= linspace(0,2*pi,100); % Create a linearly
spaced 100 elements long vector Θ
x=cos(theta);
y=sin(theta); % Calculate x and y coordinates
plot(x,y); % Plot x vs y
axis(‘equal’); % Set length scales of two axes to be
same
xlabel(‘x’);
ylabel(‘y’);
title(‘Circle of unit radius’);
7
8. Contd…
% Plot sine wave
% Plot created on 2/1/2014
x=linspace(0,2*pi,100);
plot(x,sin(x));
plot(x,sin(x),’r’); % Color of sine wave is red
plot(x,sin(x),’+’);
xlabel(‘x’);
ylabel(‘sin(x)’);
title(‘Plot created by Amarsingh’);
8
9. Contd…
% overlapping of sine waves
x=linspace(0,2*pi,100);
plot(x,sin(x),x,sin(x),’o’)
xlabel(‘x’);
ylabel(‘sin(x)’);
9
13. MATLAb w ill make us to enjoy
symbolic comp utation
13
14. Symbolic Computation
>> syms x y % x & y as symbolic variables
>> f=(x+y)^2 % x^2 + 2*x*y + y^2
>>expand(f) % x^2 + 2*x*y + y^2
>>pretty(ans) % Use pretty to get the
expression in more readable form
14
21. Matrices & Vectors
% Create a random matrix 4X3
x=rand(4,3)
x(3:4,2:3) % get those elements of A that are
located in 3 to 4 and columns 2 to 3
x(:,4)=x(:,1) % Add a fourth column to x and set
it equal to the first column of x
x(2:4,2:4)=eye(3) % Replace the last 3X3
submatrix of x by a identity matrix
x([1 3],:)=[] % Delete the first and third rows of x
21
23. Contd..
% Magic square matrix
x=magic(3) % x = magic(n) returns an n-by-n
matrix constructed from the integers 1
through n^2 with equal row and column sums.
The order n must be a scalar greater than or
equal to 3.
23
25. Loop s, B ranche s, and cont rol -flow
1. For loops: A for loop is used to repeat a
statement or a group of statements for a
fixed number of times.
>>for m=1:100
>>num=1/(m+1)
>>end
25
32. B a s i c 2 - D p l o t s
% World population by continents.
>>cont=char(‘Asia’,’Europe’,’Africa’,
’N.America’,’S.America’);
>>pop=[3332;696;694;437;307];
>>barh(pop) % Bar chart
>>for i=1:5
gtext(cont(i,:));
>>end
>>xlabel(‘population in millions’)
>>title(‘World poplation (1992)’,’fontsize’,18)
32