Matlab

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

x = 0:pi/100:2*pi;

y1 = 2*cos(x);
y2 = cos(x);
y3 = 0.5*cos(x);
plot(x,y1,'--',x,y2,'-',x,y3,'*')

Relational Operators
Relational operators can also work on both scalar and non-scalar data. Relational operators for
arrays perform element-by-element comparisons between two arrays and return a logical array of
the same size, with elements set to logical 1 (true) where the relation is true and elements set to
logical 0 (false) where it is not.

The following table shows the relational operators available in MATLAB:

Example
 Create a script file and type the following code:

a = 100;
b = 200;
if (a >= b)
max = a
else
max = b
end

When you run the file, it produces following result:


max =
200

 Apart from the above-mentioned relational operators, MATLAB provides the following
commands/functions used for the same purpose:

% comparing two values


a = 100;
b = 200;
if (ge(a,b))
max = a
else
max = b
end
% comparing two different values
a = 340;
b = 520;
if (le(a, b))
disp(' a is either less than or equal to b')
else
disp(' a is greater than b')
end

When you run the file, it produces the following result:


max =
200
a is either less than or equal to b

DECISION MAKING
if... end Statement

a = 10;
% check the condition using if statement
if a < 20
% if condition is true then print the following
fprintf('a is less than 20\n' );
end
fprintf('value of a is : %d\n', a);

When you run the file, it displays the following result:


a is less than 20
value of a is : 10

if...else...end Statement

a = 100;
% check the boolean condition
if a < 20
% if condition is true then print the following
fprintf('a is less than 20\n' );
else
% if condition is false then print the following
fprintf('a is not less than 20\n' );
end
fprintf('value of a is : %d\n', a);

When the above code is compiled and executed, it produces the following result:
a is not less than 20
value of a is : 100

if...elseif...elseif...else...end Statements
clc
a = 100;
%check the boolean condition
if a == 10
% if condition is true then print the following
fprintf('Value of a is 10\n' );
elseif( a == 20 )
% if else if condition is true
fprintf('Value of a is 20\n' );
elseif a == 30
% if else if condition is true
fprintf('Value of a is 30\n' );
else
% if none of the conditions is true '
fprintf('None of the values are matching\n');
fprintf('Exact value of a is: %d\n', a );
end

When the above code is compiled and executed, it produces the following result:
None of the values are matching
Exact value of a is: 100

The Nested if Statements


It is always legal in MATLAB to nest if-else statements which means you can use one if or elseif
statement inside another if or elseif statement(s).

a = 100;
b = 200;
% check the boolean condition
if( a == 100 )
% if condition is true then check the following
if( b == 200 )
% if condition is true then print the following
fprintf('Value of a is 100 and b is 200\n' );
end
end
fprintf('Exact value of a is : %d\n', a );
fprintf('Exact value of b is : %d\n', b );

When you run the file, it displays:


Value of a is 100 and b is 200
Exact value of a is : 100
Exact value of b is : 200

The switch Statement


A switch block conditionally executes one set of statements from several choices. Each choice is
covered by a case statement.

grade = 'F';
switch(grade)
case 'A'
fprintf('Excellent!\n' );
case 'B'
fprintf('Well done\n' );
case 'C'
fprintf('Well done\n' );
case 'D'
fprintf('You passed\n' );
case 'F'
fprintf('Better try again\n' );
otherwise
fprintf('Invalid grade\n' );
end

LOOP TYPES
The while Loop
a = 10;
% while loop execution
while( a < 20 )
fprintf('value of a: %d\n', a);
a = a + 1;
end

When you run the file, it displays the following result:

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

The for Loop


A for loop is a repetition control structure that allows you to efficiently write a loop that needs to
execute a specific number of times.
Example
Create a script file and type the following code:
for a = 0:0.1:1
disp(a)
end
When you run the file, it displays the following result:
0
0.1000
0.2000
0.3000
0.4000
0.5000
0.6000
0.7000
0.8000
0.9000
1

The break Statement


The break statement terminates execution of for or while loop. Statements in
the loop that appear after the break statement are not executed.
In nested loops, break exits only from the loop in which it occurs. Control passes
to the statement following the end of that loop.

Example
Create a script file and type the following code:

a = 10;
% while loop execution
while (a < 20 )
fprintf('value of a: %d\n', a);
a = a + 1;
if( a > 15)
% terminate the loop using break statement
break;
end
end
When you run the file, it displays the following result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
The continue Statement
The continue statement is used for passing control to next iteration of for or
while loop.
The continue statement in MATLAB works somewhat like the break statement.
Instead of forcing termination, however, 'continue' forces the next iteration of
the loop to take place, skipping any code in between.
Example
Create a script file and type the following code −
a = 9;
%while loop execution
while a < 20
a = a + 1;
if a == 15
% skip the iteration
continue;
end
fprintf('value of a: %d\n', a);
end
When you run the file, it displays the following result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
value of a: 20
The Nested Loops
MATLAB allows to use one loop inside another loop. Following section shows few examples to
illustrate the concept.

Example
Let us use a nested for loop to display all the prime numbers from 1 to 100.
Create a script file and type the following code:

for i = 2:100
for j = 2:100
if(~mod(i,j))
break; % if factor found, not prime
end
end
if(j > (i/j))
fprintf('%d is prime\n', i);
end
end

2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime
53 is prime
59 is prime
61 is prime
67 is prime
71 is prime
73 is prime
79 is prime
83 is prime
89 is prime
97 is prime

MATRIX
a = [ 1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20]
a(2,5)
v = a(:,4)
w = a(:, 2:3)
sa = a(3:4,4:5)
a( 4 , : ) = []
a(: , 5)=[]

Ans.

a=
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
ans =
10
v=
4
9
14
19
w=
2 3
7 8
12 13
17 18
sa =
14 15
19 20
a=
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
a=
1 2 3 4
6 7 8 9
11 12 13 14
Matrix Operations
In this section, let us discuss the following basic and commonly used matrix
operations:
 Addition and Subtraction of Matrices
 Division of Matrices
 Scalar Operations of Matrices
 Transpose of a Matrix
 Concatenating Matrices
 Matrix Multiplication
 Determinant of a Matrix
 Inverse of a Matrix

Concatenating Matrices
You can concatenate two matrices to create a larger matrix. The pair of square brackets '[]' is the
concatenation operator.
MATLAB allows two types of concatenations:
Horizontal concatenation
Vertical concatenation
When you concatenate two matrices by separating those using commas, they
are just appended horizontally. It is called horizontal concatenation.
Alternatively, if you concatenate two matrices by separating those using
semicolons, they are appended vertically. It is called vertical concatenation.

a = [ 10 12 23 ; 14 8 6; 27 8 9]
b = [ 12 31 45 ; 8 0 -9; 45 2 11]
c = [a, b]
d = [a; b]
Ans.
a=

10 12 23
14 8 6
27 8 9

b=

12 31 45
8 0 -9
45 2 11

c=

10 12 23 12 31 45
14 8 6 8 0 -9
27 8 9 45 2 11

d=

10 12 23
14 8 6
27 8 9
12 31 45
8 0 -9
45 2 11

PLOTTING
x = [-100:5:100];
y = x^2;
plot(x, y)
Example
Let us draw the graph of two polynomials
f(x) = 3x4 + 2x3+ 7x2 + 2x + 9 and
g(x) = 5x3 + 9x + 2
Create a script file and type the following code:

x = [-10 : 0.01: 10]


y = 3*x.^4 + 2 * x.^3 + 7 * x.^2 + 2 * x + 9
g = 5 * x.^3 + 9 * x + 2
plot(x, y, 'r', x, g, 'g')

ALGEBRA
 solve('x-5=0')
ans =

5
 y = solve('x-5 = 0')
y=

5
 solve('v-u-3*t^2=0', 't')
ans =

(3^(1/2)*(v - u)^(1/2))/3
-(3^(1/2)*(v - u)^(1/2))/3
Solving Quadratic Equations in MATLAB
The solve function can also solve higher order equations. It is often used to
solve quadratic equations. The function returns the roots of the equation in an
array.
The following example solves the quadratic equation x2 -7x +12 = 0. Create a
script file and type the following code:
 eq = 'x^2 -7*x + 12 = 0';
s = solve(eq)
s=

3
4
 s = roots([1, -7, 12])
s=

4
3

The following example solves the fourth order equation x 4 − 7x3 + 3x2 − 5x + 9
= 0.
Create a script file and type the following code:
v = [1, -7, 3, -5, 9];
s = roots(v);
s=

6.6304
-0.3451 + 1.0778i
-0.3451 - 1.0778i
1.0598
 s = solve('5*x + 9*y = 5','3*x - 6*y = 4');
s.x
s.y
ans =

22/19

ans =

-5/57

CALCULUS
Calculating Limits
MATLAB provides the limit command for calculating limits. In its most basic
form, the limit command takes expression as an argument and finds the limit of
the expression as the independent variable goes to zero.
For example, let us calculate the limit of a function f(x) = (x 3 + 5)/(x4 + 7), as x
tends to zero.
 syms x
limit((x^3 + 5)/(x^4 + 7))
ans =

5/7
 limit(x^2 + 5, 3)
ans =
14
DIFFERENTIAL
MATLAB provides the diff command for computing symbolic derivatives. In its
simplest form, you pass the function you want to differentiate to diff command
as an argument.
For example, let us compute the derivative of the function f(t) = 3t 2 + 2t-2
Example
Create a script file and type the following code into it:
syms t
f = 3*t^2 + 2*t^(-2);
diff(f)
When the above code is compiled and executed, it produces the following result:
ans =
6*t - 4/t^3

INTEGRATION
For example, from our previous example:
syms x
int(2*x)
MATLAB executes the above statement and returns the following result:
ans =
x^2
To calculate

we write,
int(x, a, b)

For example, to calculate the value of we write:


int(x, 4, 9)
MATLAB executes the above statement and returns the following result:
ans =
65/2
STUDY OF A SIMPLE ON/OFF CONTROL SYSTEM
OBJECTIVE : To study the operation of a feedback, ON/OFF temperature control system by using
MATLAB simulink.

THEORY

 A temperature controller is an instrument used to control temperature. It does this by comparing


the process temperature (process variable) with the desired value (set value). The difference between
these values is known as the error (Deviation). Temperature controllers use this error to decide how
much heating or cooling is required to bring the process temperature back to the desired value. Once
this calculation is complete the controller will produce a signal that effects the change required. This
output signal is known as the manipulated value and is normally connected to a heater, control valve,
fan or some other "final control element" which actually injects or removes heat from the process.

The role of the temperature controller is to measure the temperature on the thermometer, compare it
to the set point and to calculate the amount of time the heater should remain switched on to maintain a
constant temperature.
Many factors change the amount of time that the heater needs to maintain the process temperature.
For example the size of the heater, the size of the oven, the amount of insulation surrounding the oven
and the ambient temperature are some of the more obvious. But other factors such as the circulation of
air within the oven, the humidity of the air. The mass of product being place inside the oven and many
more that are described in minute detail here.
On off control is like operating a switch. This type of temperature controller will turn on the heat
when the process variable is below the set point and turn it off when the process variable is above the
set point. These controllers normally include a delay, hysteresis and or a cycle time to reduce the
cycling when the process variable is close to the set point. In control systems, hysteresis can be used
to filter signals so that the output reacts less rapidly than it otherwise would, by taking recent history
into account. For example, a thermostat controlling a heater may switch the heater on when the
temperature drops below A, but not turn
it off until the temperature rises above B. (For instance, if one wishes to maintain a temperature of 20
°C then one might set the thermostat to turn the heater on when the temperature drops to below 18 °C
and off
when the temperature exceeds 22 °C.) Similarly, a pressure switch can be designed to exhibit
hysteresis, with pressure set-points substituted for temperature thresholds.
MATLAB simulink model

RESULT
Ans.

function dy=g1(x,y)
dy= 1+cos(x)
save it as g1.m

[x,y]=ode45('g1',[0 10],4)
a= 0:.1:10
b=a+sin(a)
plot(x,y,a,b,'--')

You might also like