PLOT THE FUNCTION e-X/3SIN(X)
BETWEEN 0≤X≤4Π
DISPLAY FACILITIES
CONTD..
LINE SPECIFIERS IN THE plot() COMMAND
plot(x,y,‘line specifiers’)
Line Specifier Line Specifier Marker Specifier
Style
Color Type
Solid - red r plus sign +
dotted : green g circle o
dashed -- blue b asterisk *
dash-dot -. Cyan c point .
magenta m square s
yellow y diamond d
black k
» x = 1:2:50; Plots
» y = x.^2;
» plot(x,y) 2500
2000
1500
1000
500
0
0 5 10 15 20 25 30 35 40 45 50
Plots
» plot(x,y,'*-')
» xlabel('Values of x')
» ylabel('y') 2500
2000
1500
y
1000
500
0
0 5 10 15 20 25 30 35 40 45 50
Values of x
MULTIPLE GRAPHS
t=0:pi/100:2*pi; 1
y1=sin(t); 0.8
y2=sin(t+pi/2); 0.6
plot(t,y1,t,y2); 0.4
grid on 0.2
0
-0.2
-0.4
-0.6
-0.8
-1
01234567
MULTIPLE PLOTS
t=0:pi/100:2*pi;
y1=sin(t);
y2=sin(t+pi/2);
subplot(2,2,1)
plot(t,y1)
grid on
subplot(2,2,2)
plot(t,y2);
grid on
subplot(i,j,k)
• i is the number of rows of subplots in the plot
• j is the number of columns of subplots in the plot
• k is the position of the plot
INTERESTING FEATURE OF GENERATING
SINE CURVE
x = 0:0.05:6; 1
y = sin(pi*x); 0.8
Y = (y >= 0).*y; 0.6
plot(x,y,':',x,Y,'-') 0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
012 345 6
CONTINUED..
x = 0:0.05:6; 1
y = sin(pi*x); 0.8
Y = (y >= 0).*y; 0.6
plot(x,y,„.',x,Y,'-') 0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
012 345 6
OPERATORS (RELATIONAL, LOGICAL)
POLYNOMIALS
MATLAB FUNCTIONS FOR POLYNOMIALS
Contd..
Representing Polynomials:
x4 - 12x3 + 25x + 116
» P = [1 -12 0 25 116];
» roots(P)
ans =
11.7473
2.7028
-1.2251 + 1.4672i
-1.2251 - 1.4672i
» r = ans;
» PP = poly(r)
PP =
1.0000 -12.0000 -0.0000 25.0000 116.0000
Polynomial Multiplication
a = x3 + 2x2 + 3x + 4
b = 4x2 + 9x + 16
» a = [1 2 3 4];
» b = [4 9 16];
» c = conv(a,b)
c=
4 17 46 75 84 64
Evaluation of a
Polynomial
a = x3 + 2x2 + 3x + 4
» polyval(a, 2)
ans =
26
Symbolic Math
» syms x
» int('x^3')
ans =
1/4*x^4
» eval(int('x^3',0,2))
ans =
4
»
Solving Nonlinear Equations
nle.m
Function of the program:-
function f = nle(x)
f(1) = x(1) - 4*x(1)*x(1) - x(1)*x(2);
f(2) = 2*x(2) - x(2)*x(2) + 3*x(1)*x(2);
Program:-
x0 = [1 1]';
x = fsolve('nle', x0)
Solution:-
x=
0.2500
0.0000
TO FIND EIGEN VALUES AND EIGEN
VECTORS OF MATRICES
CONTINUED…
SOLVING LINEAR EQUATIONS
(TRY MANUALLY)
Solve manually and tell me what is the answer..?
That is find out x=
y=
z=
SOLVING SET OF SIMULTANEOUS EQUATIONS
DIFFERENTATION
SOLVING DIFFERENTIAL EQUATIONS
PERFORMING INTEGRATION
NUMERICAL INTEGRATION
Numerical integration of the integral f (x) dx is called
quadrature. MATLAB provides the following built-in functions
for numerical integration:
quad:
It integrates the specified function over specified limits, based on
adaptive Simpson's rule.
The general call syntax for both quad and quadl is as follows:
Syntax:-
integral = quad(„function‟, a, b)
dblquad: (It calculates double integration)
MATLAB provides a function dblquad. The calling syntax for
dblquad is:
Syntax:-
I = dblquad(„function_xy‟, xmin, xmax, ymin, ymax)
FLOW CONTROL
CONTROL STRUCTURES
CONTROL STRUCTURES
CONTROL STRUCTURES
IF STATEMENT
(EXAMPLE)
n = input(„Enter the upper limit: „);
if n < 1
disp („Your answer is meaningless!‟)
end
x = 1:n; Jump to here if TRUE
term = sqrt(x); Jump to here if FALSE
y = sum(term)
EXAMPLE PROGRAM TO EXPLAIN IF LOOP
% Program to find whether roots are imaginary or not%
clc;
clear all;
a=input('enter value of a:');
b=input('enter value of b:');
c=input('enter value of c:');
discr = b*b - 4*a*c;
if discr < 0
disp('Warning: discriminant is negative, roots are imaginary');
end
Solution:-
Input:
enter value of a:1
enter value of b:2
enter value of c:3
Output:
Warning: discriminant is negative, roots are imaginary
EXAMPLE OF IF ELSE STATEMENT
Program:-
A = 2;
B = 3;
if A > B
'A is bigger'
elseif A < B
'B is bigger'
elseif A == B
'A equals B'
else
error('Something odd is happening')
end
Solution:-
ans =
B is bigger
IF STATEMENT EXAMPLE
Here are some examples based on the familiar quadratic formula.
1. discr = b*b - 4*a*c;
if discr < 0
disp('Warning: discriminant is negative, roots are imaginary');
end
2. discr = b*b - 4*a*c;
if discr < 0
disp('Warning: discriminant is negative, roots are imaginary');
else
disp('Roots are real, but may be repeated')
end
3. discr = b*b - 4*a*c;
if discr < 0
disp('Warning: discriminant is negative, roots are imaginary');
elseif discr == 0
disp('Discriminant is zero, roots are repeated')
else
disp('Roots are real')
end
EXAMPLE OF FOR LOOP
Problem: Draw graphs of sin(nπ x) on the interval −1 ≤ x ≤ 1 for
n = 1,2,....,8.We could do this by giving 8 separate plot
commands but it is much easier to use a loop.
Program:-
x=-1:0.05:1;
for n=1:8
subplot(4,2,n);
plot(x, sin(n*pi*x));
end
EXAMPLE OF FOR & WHILE LOOP
1.) % example of for loop%
Program:-
for ii=1:5
x=ii*ii
End
Solution: 3.) %example of while loop%
1 4 9 16 25 x=1
while x <= 100
2.) %example of while loop% x = 3*x
Program:- end
x=1 Solution:-
while x <= 10 X= 1 3 9 27
x = 3*x
End
Solution:
x=1 x=3 x= 9 x=27
SWITCH STATEMENT EXAMPLE
Program:-
n=input(„enter the value of n: ‟);
switch(rem(n,3))
case 0
m = 'no remainder'
case 1
m = „the remainder is one'
case 2
m = „the remainder is two'
otherwise
error('not possible')
end
Solution:-
enter the value of n: 8
m =the remainder is two
SPRING/MASS/DAMPER SYSTEM EXAMPLE
88
PROBLEM SOLVED BY USING
MATLAB AND SIMULINK
INTRODUCTION
PROBLEM
PROBLEM
FUNCTION OF THE PROGRAM
function f=programone (t,z)
m=3;
c=8;
k=100;
dzdt=[z(2); -(c/m)*z(2)-(k/m)*z(1)];
MAIN BODY OF THE PROGRAM
% For a single degree of freedom system in free vibration
clc;
clear all;
%Enter initial conditions
z0=[5;15];
%Enter time span for solution
tspan=[0 10];
%Call solver
[t,z]=ode45(„programone',tspan,x0);
%Set up plot
plot(t,z(:,1));
displacement in mmRESULT
single degree of freedom spring mass damper system behaviour
6
displacement
5
4
3
2
1
0
-1
-2
-3
0 5 10 15
time in seconds
DEMONSTRATION OF THE CONCEPT
WITH SIMPLE EXAMPLE
DEMONSTRATION OF THE CONCEPT
WITH SIMPLE EXAMPLE
DEMONSTRATION OF THE CONCEPT
WITH SIMPLE EXAMPLE
DEMONSTRATION OF THE CONCEPT
WITH SIMPLE EXAMPLEPROBLEM
(OSCILLATOR)
MODELLING PENDULUM WITH DAMPING