Matlab Tutorial
Matlab Tutorial
Course 2016
for
Nonlinear
Dynamics
Contents
PROBLEM-1
PROBLEM-2
PROBLEM-3
PROBLEM-4
PROBLEM-5
PROBLEM-6
PROBLEM-7
PROBLEM-8
PROBLEM-9
%_________________________________________________
PROBLEM-2
----------- This is about extraction of elements from matrix
%
A = [1 5 8 ;84 81 7; 12 34 71];
% -------- and examine the contents of-------------A(1,1), A(2,1), A(1,2) , A(3,3), A(1:2, :),
A(:,1),A(3,:), A(:,2:3)
ans =
1
ans =
84
ans =
5
ans =
71
ans =
1
84
5
81
8
7
34
71
ans =
1
84
12
ans =
12
ans =
5
81
34
8
7
71
PROBLEM-3
- How to create plots in MATLAB?
x = -4:0.05:4; y=exp(-0.5*x).*cos(5*x);
figure(1) ;
plot(x,y);
xlabel(x-axis) ; ylabel(y-axis);
hold on;
y=exp(-0.5*x).*sin(5*x) ;
plot(x,y) ;
grid;
% gtext(Your text... ) ; % uncomment to execute it
hold off
%
8
6
4
yaxis
2
0
2
4
6
8
4
0
xaxis
- PROBLEM-4
%------- How to create subplots? --------x = 0.1:.1:5;
subplot(2,3,1);plot(x, x) ;title(plot of x);
xlabel(x); ylabel(y);
subplot(2,3,2);plot(x,x.^2);title(plot of x^2);
xlabel(x); ylabel(y);
subplot(2,3,3),plot(x,x.^3);title(plot of x^3)
3
xlabel(x); ylabel(y );
subplot(2,3,4),plot(x,cos(x));title(plot of cos(x));
xlabel(x); ylabel(y);
subplot(2,3,5) , plot(x,exp(x).*cos(2*x)); title(plot of e^(x)*cos(2x));
xlabel(x); ylabel(y);
subplot(2,3,6) ,plot(x,sin(3*x));title(plot of sine(3x));
xlabel(x); ylabel(y);
plot of x2
plot of x
4
20
15
100
y
10
50
plot of e(x)*cos(2x)
plot of sine(3x)
0.5
0.5
50
0.5
50
100
5
x
5
x
plot of cos(x)
plot of x3
150
25
150
0.5
5
x
PROBLEM-5
How to create mesh plots
zlabel(z-axis);
% Parabola
z = x.^2 + y.^2;
figure(2);
surfl(x,y,z);
xlabel(x -axis);
ylabel(y-axis);
zlabel(z-axis);
% Contour plot
figure(3);
contour3(x,y,z,15);
axis([-4 4 -4 4] )
xlabel(x-axis);
ylabel(y_axis);
zlabel(z-axis);
20
zaxis
10
10
20
4
2
4
2
2
yaxis
2
4
x axis
35
30
zaxis
25
20
15
10
5
0
4
2
4
2
2
4
yaxis
x axis
4
3
2
yaxis
1
0
1
2
3
4
4
0
xaxis
PROBLEM-6 %----------- How to write function file in MATLAB?------------% ---------- Function file Myfunction --------------------% function v = Myfunction(x)
%
v = 1./(1 + exp(- x));
% end
%______________________
% Now using that function
x = 2:.04:4;
y = Myfunction(x) ;
plot(x,y);
xlabel(x); ylabel(y);
figure(2)
fplot(Myfunction,[2 4],10)
xlabel(x); ylabel(y);
1
0.98
0.96
0.94
0.92
0.9
0.88
2.2
2.4
2.6
2.8
3
x
3.2
3.4
3.6
3.8
0.98
0.96
0.94
0.92
0.9
0.88
2.2
2.4
2.6
2.8
3
x
3.2
3.4
3.6
3.8
sol1 =
-0.7500 + 0.6614i
sol2 =
-0.7500 + 0.6614i
sol3 =
-0.7500 + 0.6614i
sol4 =
-0.7500 + 0.6614i
PROBLEM-8
How to write loops in MATLAB?
% Fill b with square roots of 1 to 1000 using a for loop
clear;
tic;
for i = 1:1000
b(i)= sqrt(i);
end
t = toc;
disp([Time taken for loop method is, num2str(t)]);
% % Fill b with square roots of 1 to 1000 using a vector
clear;
tic;
a = 1:1:1000;
b= sqrt(a);
t = toc;
disp([Time taken for without loop method is, num2str(t)]);
Time taken for loop method is0.0017789
Time taken for without loop method is3.6536e-05
PROBLEM-9
Dynamics in matlab ( Prey-Predator Model)
% Function file
%______________________________
%
%
%
%
%
%
function dX = Pred_Prey(t, X)
dX = [0 ; 0];
alpha = 1;
beta = 0.05;
gamma = 0.02;
delta = 0.5;
10
70
prey
predators
60
Population
50
40
30
20
10
10
Time
12
14
16
18
20
45
40
35
predators
30
25
20
15
10
5
10
20
30
40
prey
11
50
60
70