0% found this document useful (0 votes)
60 views

Matlab Tutorial

This document provides a MATLAB tutorial covering various topics like element operations on matrices and arrays, plotting functions, creating subplots, mesh plots, writing custom functions, solving equations, using loops, and implementing a prey-predator dynamics model. It includes 9 problems demonstrating key MATLAB capabilities like matrix manipulation, plotting, function writing, solving equations, and numerical integration of differential equations.

Uploaded by

yy_yogesh007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Matlab Tutorial

This document provides a MATLAB tutorial covering various topics like element operations on matrices and arrays, plotting functions, creating subplots, mesh plots, writing custom functions, solving equations, using loops, and implementing a prey-predator dynamics model. It includes 9 problems demonstrating key MATLAB capabilities like matrix manipulation, plotting, function writing, solving equations, and numerical integration of differential equations.

Uploaded by

yy_yogesh007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

MATLAB Tutorial

Course 2016

for

Nonlinear

Dynamics

Jawaharlal Nehru University

Contents

PROBLEM-1
PROBLEM-2
PROBLEM-3
PROBLEM-4
PROBLEM-5
PROBLEM-6
PROBLEM-7
PROBLEM-8
PROBLEM-9

%_________________________________________________

PROBLEM-1 % is comment in MATLAB ------------------------------------------x = [2 3 4 5];


y = -1:1:2;
% % % Element wise element operation of matrices and arrays.
r1 = x.^y;
r2 = x.*y;
r3 = x./y;
% This provides usual matrix multiplication.
r4 = x*y;

PROBLEM-2
----------- This is about extraction of elements from matrix
%

(a) Set up the following matrix in the command window

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

% -- For the function z=f(x,y), the MATLAB function meshgrid is used to


% generate complete set 0f points in the x-y plane for the three-dimensionalplotting/functio
% finally plotted by using one of the functions mesh, surf, surfl or surfc.
clf
% Saddle plot
[x,y] = meshgrid(-4.0:0.2:4.0,-4.0:0.2:4.0);
z = x.^2 - y.^2;
figure(1);
surfc(x,y,z);
xlabel(x -axis);
ylabel(y-axis);
4

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

PROBLEM-7 %-- Determine the solution of quadratic equation in MATLAB----%____________________________________


% Function file
% function [xl,x2]= rootquad (a,b,c);
% This function solves a simple quadratic equation a*x^2+ b*x + c = 0
% % given the coefficients a,b,c. The solutions are
% % assigned ta xl and xZ
%
% d = b*b -4*a*c;
% xl = (-b + sqrt(d))/(2*a);
% x2 = (-b + sqrt(d))/(2*a);
% ------- Calling function- twoways to do -------------------[sol1 sol2] = rootquad(4,6,4);
sol1
sol2
[sol3 sol4] = feval(@rootquad,4,6,4);
sol3
sol4
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;

% dX(1) = alpha*X(1) - beta*X(1)*X(2);


% dX(2) = gamma*X(1)*X(2) - delta*X(2);
% Now Dynamics
[t,x] = ode45(PREY, [0 20], [10 10]);
figure(1)
plot(t,x);
xlabel(Time); ylabel(Population);
legend(prey, predators);
figure(2)
plot(x(:,1),x(:,2))
xlabel(prey); ylabel(predators);

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

You might also like