Académique Documents
Professionnel Documents
Culture Documents
7
General Purpose Commands
I
Special Variables and Constants
ans Most recent answer.
eps Accuracy of floating-point precision.
i,j The imaginary unit 1.
Inf Infinity.
NaN Undefined numerical result (not a number).
pi The number .
II
Input/Output and Formatting Commands
Input/Output Commands
disp Displays contents of an array or string.
fscanf Read formatted data from a file.
format Controls screen-display format.
fprintf Performs formatted writes to screen or file.
input Displays prompts and waits for input.
; Suppresses screen printing.
%s Format as a string.
%d Format as an integer.
%f Format as a floating point value.
%e Format as a floating point value in scientific notation.
%g Format in the most compact form: %f or %e.
\n Insert a new line in the output string.
\t Insert a tab in the output string.
III
Vector, Matrix and Array Commands
Array Commands
cat Concatenates arrays.
find Finds indices of nonzero elements.
length Computers number of elements.
linspace Creates regularly spaced vector.
logspace Creates logarithmically spaced vector.
max Returns largest element.
min Returns smallest element.
prod Product of each column.
reshape Change size
size Computes array size.
sort Sorts each column.
sum Sums each column.
Special Matrices
eye Creates an identity matrix.
ones Creates an array of ones.
zeros Creates an array of zeros.
Matrix Arithmetic
cross Computes cross products.
dot Computes dot products.
IV
Cell Array Functions
cell Creates cell array.
celldisp Displays cell array.
cellplot Displays graphical representation of cell array.
num2cell Converts numeric array to cell array.
deal Matches input and output lists.
iscell Identifies cell array.
Structure Functions
fieldnames Returns field names in a structure array.
getfield Returns field contents of a structure array.
isfield Identifies a structure array field.
isstruct Identifies a structure array.
rmfield Removes a field from a structure array.
setfield Sets contents of field.
struct Creates structure array.
V
Plotting Commands
VI
Colors, Symbols and Line Types
Histogram Functions
bar Creates a bar chart.
hist Aggregates the data into equally spaced bins.
histc Aggregates the data into unequally spaced bins.
VII
Programming
Logical Functions
any True if any elements are nonzero.
all True if all elements are nonzero.
find Finds indices of nonzero elements.
finite True if elements are finite.
isnan True if elements are undefined.
isinf True if elements are infinite.
isempty True if matrix is empty.
isreal True if all elements are real.
VIII
M-Files
eval Interpret strings containing Matlab expressions.
feval Function evaluation.
function Creates a user-defined function M-file.
global Define global variables.
nargin Number of function input arguments.
nargout Number of function output arguments.
script Script M-files
Timing
cputime CPU time in seconds.
clock Current date and time as date vector.
tic, toc Start, stop a stopwatch timer.
IX
Mathematical Functions
Trigonometric Functions
acos(x) Inverse cosine; arcos x = cos 1 (x).
acot(x) Inverse cotangent; arccot x = cot 1(x).
acsc(x) Inverse cosecant; arcs x = csc 1 (x).
asec(x) Inverse secant; arcsec x = sec 1 (x).
asin(x) Inverse sine; arcsin x = sin 1 (x).
atan(x) Inverse tangent; arctan x = tan 1 (x).
atan2(y,x) Four-quadrant inverse tangent.
cos(x) Cosine; cos(x).
cot(x) Cotangent; cot(x).
csc(x) Cosecant; csc(x).
sec(x) Secant; sec(x).
sin(x) Sine; sin(x).
tan(x) Tangent; tan(x).
Hyperbolic Functions
acosh(x) Inverse hyperbolic cosine; cosh 1 (x).
acoth(x) Inverse hyperbolic cotangent; coth 1 (x).
acsch(x) Inverse hyperbolic cosecant; csch 1 (x).
asech(x) Inverse hyperbolic secant; sech 1 (x).
asinh(x) Inverse hyperbolic sine; sinh 1 (x).
atanh(x) Inverse hyperbolic tangent; tanh 1 (x).
cosh(x) Hyperbolic cosine; cosh(x).
coth(x) Hyperbolic cotangent; cosh(x)/sinh(x).
csch(x) Hyperbolic cosecant; 1/sinh(x).
sech(x) Hyperbolic secant; 1/cosh(x).
sinh(x) Hyperbolic sine; sinh(x).
tanh(x) Hyperbolic tangent; sinh(x)/cosh(x).
X
Complex Functions
abs(x) Absolute value; |x|.
angle(x) Angle of a complex number x.
conj(x) Complex conjugate of x.
imag(x) Imaginary part of a complex number x.
real(x) Real part of a complex number x.
Statistical Functions
erf(x) Computes the error function erf (x).
mean Calculates the average.
median Calculates the median.
std Calculates the standard deviation.
Numeric Functions
ceil Rounds to the nearest integer toward .
fix Rounds to the nearest integer toward zero.
floor Rounds to the nearest integer toward - .
round Rounds towards the nearest integer.
sign Signum function.
String Functions
findstr Finds occurrences of a string.
strcmp Compares strings.
char Creates character string array
XII
Numerical Methods
Interpolation Functions
interp1 Linear and cubic-spline interpolations of a function of one variable.
interp2 Linear interpolation of a function of two variables.
spline Cubic-spline interpolation.
unmkpp Computes the coefficients of cubic-spine polynomials.
XIII
ODE Solvers
ode23 Nonstiff, low-order solver.
ode45 Nonstiff, medium-order solver.
ode113 Nonstiff, variable-order solver.
ode23s Stiff, low-order.
ode23t Moderately stiff, trapezoidal rule solver.
ode23b Stiff, low-order solver.
ode15s Stiff, variable-order solver.
odeset Creates integrator options structure for ODE solvers.
XIV
Symbolic Math Toolbox
XV
Symbolic Solution of Algebraic and Transcendental Equations
solve Solves symbolic equations.
XVI
CHAPTER-1
MATLAB BASICS
Ex-1.1
Use of if constructs
A salesman is paid his normal wage up to 40 hours a week. He is paid overtime 30% more for next
10 hours and 50% after that. Write a program to calculate his salary. Prompt the user to enter the
number of hours and hourly wages.
Solution
The M-file developed is shown below:
Output
Enter the number of hours worked20
Enter the hourly wage in Rs.50
salary =
1000
Ex-1.2
Write a program to find the sum of even number between 0 and 20 using for loop.
Solution
sThe M-file given below shows the use of for loop to find sum of even number between 0 and 20.
%programe to use for loop
sum=0;
for j=0:2:20
sum=sum+j;
end;
sum
1
Output
sum =
110
Ex-1.3
Write a program to generate sinusoidal waveform.
Solution
The MATLAB program to generate a sinusoidal voltage and current (lag by 90o w.r.t voltage)
waveform using for loop is given below.
%plot sine wave using for loop
f=50; n=1;
w=2*pi*f;
for t=0:1e-3:0.04;
v=sin(w*t);
i=sin(w*t-pi/2);
xt(n)=t ;xv(n)=v ; xi(n)=i;
n=n+1;
end
plot(xt,xv,': g d',xt,xi,'-- r *');
grid;
title('plot sine wave using for loop ')
xlabel('time(sec)');
ylabel('voltage ¤t');
Output
2
Ex-1.4
Write a program to generate to find the sum of even numbers between 0 and 21 using a while loop.
Solution
The MATLAB program given below show the use of while loop to find sum of even numbers between
0and 21.
%while loop programe
n=0;
sum=0;
while n<21
sum=sum+n;
n=n+2;
end;
sum
Output
sum =
110
Ex-1.5
Enter the following arrays and check their size using who command.
a=[10 20i 10+20]
b=[-1;10;5]
c=[a' b]
d=[1 2 3;2 -2 0; 3 0 4]
e(3,3)=2
p=[zeros(3,1) ones(3,1) zeros(1,3)']
Solution
The M-file developed is shown below:
3
Output
ans =
1 3
ans =
3 1
ans =
3 2
ans =
3 3
ans =
3 3
ans =
3 3
Ex-1.6
Find the value of a a(1,2),b(3,1),c(2,2) in the Ex-5 problem.
Solution
The M-file developed is shown below:
A=a(1,2)
B=b(3,1)
C=c(2,2)
Output
A=
0.0000 +20.0000i
4
B=
C=
10
Ex-1.7
An array A is defined as given below:
A=[1.1 0.3 2.1 2.2; 0.2 1.2 -0.6 -1; 1.3 4.5 3 3.1]
Solution
The M-file developed is shown below:
Output
a=
b=
2.2000
5
-1.0000
3.1000
c=
0.2000 1.3000 0.3000 1.2000 4.5000 2.1000 -0.6000 3.0000 2.2000 -1.0000
3.1000
d=
e=
2.1000 2.2000
-0.6000 -1.0000
f=
1.2000
g=
0.3000
1.2000
Ex-1.8
Solve the following problem in the command window:
x=(25*32.5-4^3)/(31+5^2)
y=3*(3+5^2)/4+(124*3+2)/3-55^2/3
p=(sin(7*pi/3))^2*cos(5*pi/2)+tan(3*pi/2)/(13/2)
q=(325)^(1/3)/7^2*log(25)/(23+5^3)+(1025)^(1/4)
Solution
The M-file developed is shown below:
x=(25*32.5-4^3)/(31+5^2)
y=3*(3+5^2)/4+(124*3+2)/3-55^2/3
p=(sin(7*pi/3))^2*cos(5*pi/2)+tan(3*pi/2)/(13/2)
q=(325)^(1/3)/7^2*log(25)/(23+5^3)+(1025)^(1/4)
6
Output
x=
13.3661
y=
-862.6667
p=
8.3750e+14
q=
5.6613
Ex-1.9
Evaluate the following for x=2.3 and y=4.7:
z=x*y^3+x^5/(y-3547)+(x^3*y^5/(x/y))
Solution
x=2.3; y=4.7;
z=x*y^3+x^5/(y-3547)+(x^3*y^5/(x/y))
Output
z=
5.7261e+04
Ex-1.10
Create a row vector with first and last element as 50 and -50 with a decrement of 5.(A column vector
can be created by transpose of a row vector.)
Solution
The M-file developed is shown below:
y=(50:-5:-50)
X=(1:2:100)
transpose(y)
7
Output
ans =
50
45
40
35
30
25
20
15
10
5
0
-5
-10
-15
-20
-25
-30
-35
-40
-45
-50
Ex-1.11
Create a matrix
A=[5 15 25 35; 39 29 19 9; 42 45 48 51]
Use this matrix to create
(a) A four-element row vector containing the element of first row.
(b) A three element of row vector containing the element of fourth column.
(c) A six element row vector containing the element of first and second columns.
(d) An eight element row vector containing the element of first and second rows.
Solution
The M-file developed is shown below:
8
Output
a=
5 15 25 35
b=
35
9
51
c=
5 39 42 15 29 45
d=
5 15 25 35 39 29 19 9
Ex-1.12
Create a matrix
A=[5 15 25 30 35; 7 17 27 37 27; 39 29 19 42 9; 42 45 48 47 51]
Use this matrix to create
(a) A four-element column vector containing the elements of first column vector containing the
elements of first column.
(b) A three-element column vector containing the elements of fourth column.
(c) An eight element column vector containing the elements of first and second columns.
(d) A 10-element column vector containing the elements of first and second rows.
Solution
The M-file developed is shown below:
9
Output
a=
5
7
39
42
b=
30
37
42
c=
5 15
7 17
39 29
42 45
d=
5 15 25 30 35
7 17 27 37 27
Ex-1.13
Using the zeros and ones commands create a 3x5 matrix having second and fourth columns as 0s and
first ,third and fifth columns as 1s.
Solution
The M-file developed is shown below:
A=zeros(3,5)
B=ones(3,5)
C=A+B
C(:,2)=0
C(:,4)=0
10
Output
A=
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
B=
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
C=
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
C=
1 0 1 1 1
1 0 1 1 1
1 0 1 1 1
C=
1 0 1 0 1
1 0 1 0 1
1 0 1 0 1
Ex-1.14
Calculate the value of x for the following function x=(y^2+2)^2*y^2 for the value of y varying from -
4 to 4 with an increment of 0.5. Solve the problem by creating a vector of y.
11
Solution
The M-file developed is shown below:
Output
y=
Columns 1 through 12
-4.0000 -3.5000 -3.0000 -2.5000 -2.0000 -1.5000 -1.0000 -0.5000 0 0.5000 1.0000
1.5000
Columns 13 through 17
x=
1.0e+03 *
Columns 1 through 12
5.1840 2.4875 1.0890 0.4254 0.1440 0.0406 0.0090 0.0013 0 0.0013 0.0090
0.0406
Columns 13 through 17
12
CHAPTER-2
INTRODUTION TO PLOTTING
Ex-2.1
Plot the voltage (v=5sinwt) and current(i=2sin(wt-)) flowing through a circuit on the common axis.
Solution
To plot the voltage and current flowing through a circuit on the common axis ,two different vectors ,v
and i are to be created as a function of time t. Then to plot both the function on a common time axis.
Output
Fig-1
13
Ex-2.2
Use the hold command to obtain a multiple plot. Use the gtext command to add the text v1 and v2 to
the plots
Solution
The following program illustrates the use of the hold on command to generate multiple plots.
Output
Fig-2
14
Ex-2.3
Use the line command to generate a multiple plot.
Solution
The following program illustrates the use of line command to create multiple plots. The plot of t
versus v1 is obtained using plot command with dashed line style ,whereas the plot of t versus v2 is
obtained using line command.
Output
Fig-3
15
Ex-2.4
Plot the function [y=3(-0.2x+10) -5] for a value of x from 0.1 to 60 on linear ,semilog, and loglog axis.
Solution
The M-file developed is shown below:
Output
Fig-4
16
Ex-2.5
Plot the complex function x(t)=e-0.2t ( sint + j cost) for (0 ).
Solution
A complex function variable can be plotted in three different ways:
3.polar plot.
17
Output
Fig-5
Fig-6
Fig-7
18
Ex-2.6
Represent the year wise placement data of an educational institute described below using vertical and
horizontal bar plot, stairs and stem plot.
Output
Fig-8
19
Fig-9
Fig-10
Ex-2.7
Represent the grades assigned to a class using pie chart.
No. students 10 20 15 10 5
Grade A B C D E
Solution
The vector A is created to represent the number of students. Legend one, two, three, four, and five are
added to represent the grades A,B,C,D, and E respectively. MATLAB plots different sections of pie
chart with different default colours. Text shown is added using plot Editor.
%pie chart
A=[10 20 15 10 5];
figure(1);
pie(A);
title('PIE CHART');
20
legend('one','two','three','four','five');
%pie chart with explod
figure(2);
explode=[0,1,0,1,0];
pie(A,explode);
title('No of students with max & min grades');
legend('one','two','three','four','five');
Output
Fig-11
Fig-12
Ex-2.8
Plot the function f(x)=0.2x+sin(2x)/x directly using ezplot and fplot command.
Solution
The plot obtained using ezplot with default limits. Using fplot command with only x-axis limits.
The plot fixes the y-axis limits automatically. Hera also plot obtained using fplot command with
defined x-axis and y-axis limit.
The M-file developed is shown below:
%programe to ploy function directly
figure(1);
ezplot('0.2*x+sin(2*x)/x');
title('plot of f(x)=0.2*x+sin(2*x)/x');
grid on;
figure(2);
fplot('0.2*x+sin(2*x)/x',[-2*pi 2*pi]);
21
title('plot of f(x)=0.2*x+sin(2*x)/x using fplot' );
grid on;
figure(3)
fplot('0.2*x+sin(2*x)/x',[-2*pi 2*pi -pi pi]);
title('plot of f(x)=0.2*x+sin(2*x)/x using fplot' );
grid on;
Output
Fig-13
Fig-14
Fig-15
22
Ex-2.9
Plot the decaying oscillations of a mechanical system which can be represented as a function of
independent variable time t=0 using 2-D and 3-D plots.
Solution
A 2-D plot between two variable x and y or plot between (x,t) and (y,t) plotted using plot command.
3-D plot two variable x and y with respect to time.
23
Output
Fig-16
Fig-17
Fig-18
24
Ex-2.10
Plot the function
Z=X2Y/(X2+Y2)+Y
Solution
The mesh plot is made of the lines whereas in the surface plot area within the mesh lines are colored.
Output
25
Fig-19
Fig-20
Ex-2.11
Plot the function
Solution
The M-file developed is shown below:
%additional 3-Dplots
x=-3:0.25:3;
y=-3:0.25:3;
[X,Y]=meshgrid(x,y);
Z=(1.2.^(-0.5).*sqrt(X.^2+Y.^2)).*sin(X).*cos(0.8*Y);
figure(1);
meshz(X,Y,Z);
title('Meshz plot');
xlabel('x');
ylabel('y');
zlabel('z');
26
%mesh and contour plot
figure(2);
meshc(X,Y,Z);
xlabel('x');
ylabel('y');
zlabel('z');
title('meshcplot')
%surface and contour plot
figure(3);
surfc(X,Y,Z);
xlabel('x');
ylabel('y');
zlabel('z');
title('surfc plot');
%3-D contour plot
figure(4);
contour3(X,Y,Z,20);
xlabel('x');
ylabel('y');
zlabel('z');
title('3-D contour plot');
%2-D contour plot
figure(5);
contour(X,Y,Z,20);
xlabel('x');
ylabel('y');
zlabel('z');
title('2-D contour plot');
%3-D bar plot
figure(6);
bar3(Z);
xlabel('x');
ylabel('y');
zlabel('z');
title('3-D bar plot');
27
Output
Fig-21
Fig-22
28
Fig-23
Fig-24
29
Fig-25
Fig-27
30
CHAPTER-3
PROGRAMING IN MATLAB
Ex-3.1
Write a program to generate at least two cycle of three phase 50Hz,230V line to neutral and line to
line sinusoidal voltage waveform.
Solution
The instantaneous three-phase sinusoidal voltage waveforms can be represented mathematically
As follows:
Line-to-neutral voltage
Van=Vmsinwt
Vbn=Vmsin(wt-2/3)
Vcn=Vmsin(wt+2/3)
Line to-line voltage
Vab=( Van -Vbn)= Vmsin(wt+/6)
Vbc=( Vbn -Vcn)= Vmsin(wt-/2)
Vca=( Vcn -Van)= Vmsin(wt+5/6)
Where Vm= Van and w=2f
The M-file developed is shown below:
%define line to neutral voltage or phase voltage
vm=220;f=50; w=2*pi*f;
t=0:1e-3:0.04; x=w*t;
van=vm*sin(w*t);
vbn=vm*sin(w*t-(2*pi/3));
vcn=vm*sin(w*t+(2*pi/3));
figure(1);
plot(x,van,x,vbn,x,vcn)
legend('van','vbn','vcn')
title('3-phase wave form')
xlabel('angle(wt')
ylabel('phase voltage')
grid on
%define line to line voltge
vab=van-vbn;
vbc=vbn-vcn;
vca=vcn-van;
figure(2);
plot(x,vab,x,vbc,x,vca);
legend('vab','vbc','vca')
title('3-phase line voltage wave form')
xlabel('angle(wt)')
ylabel('line voltage')
grid on
%subplot graph
figure(3)
subplot(2,1,1);
31
plot(x,van,x,vbn,x,vcn)
legend('van','vbn','vcn')
title('3-phase wave form')
xlabel('angle(wt)')
ylabel('line voltage')
grid on;
subplot(2,1,2)
plot(x,vab,x,vbc,x,vca);
legend('vab','vbc','vca')
title('3-phase line voltage wave form')
xlabel('angle(wt)')
ylabel('line voltage')
grid on;
Output
Fig-1
Fig-2
32
Fig-3
Ex-3.2
Write the program to solve the function
f(y)=y3 /(y3+1)2
by writing a function file with name fun_3_2,with the input to the function is y and output f(y)
for following values of y:
(a) f(y)for y=52
Solution
The script and function file for the Ex-3.2 are shown below. The function named is fu_3_2 is saved in
the workspace directory with file name fun_5_2.m. The main file ask for vth einput variable y on
execution.
The M-file developed is shown below:
%function file in different scrit
function [ x] = fun_3_2(y )
%function definition line
%function to solve f(y) for given vlaue of y
%define x=f(y)
x=(y.^3.*sqrt(2*y+4))./(y.^3+1).^2;
end
33
Output
enter the value of y: 52
x=
7.3909e-05
7.3909e-05
Ex-3.3
Write a program to find the sum of integers between two given numbers using function approach.
Solution
The script and function file for the Ex-3.2 are shown below. The use is prompted to enter the range of
number p and q in the script file. A for loop used in the function file to calculate the sum of integers
between the given numbers.
%function file in different scrit
end
%main file scrit
%programefor the addition of integer numbers between two given numbers
%using function approach
p=input('enter the first number p: ');
q=input('enter the second number q: ');
s=input('enter the third number s: ');
%call function
result= fun_3_3(p,q);
%disp result
fprintf ('the sum of integer nubers between %d and %d is %d/n',p,q,result);
Output
example_3_3
enter the first number p: 0
enter the second number q: 5
enter the third number s: 2
the sum of integer nubers between 0 and 5 is 10
34
Ex-3.4
Write a program to find the step response of a series RL circuit shown in fig-4 plot current response
Fig-4
Solution
The mathematical expression to find the current flowing through the circuit for the problem can be
written by applying Kirchhoffs Voltage law as follow:
V=iR+Ldi/dt
This is a first order differential equation and can be rearranged in standard form as
di/dt= 1/L(V-iR)
File name- fun_RL_series
%function file in different scrit
function idot=fun_RL_series(t,i)
%function defination line
global R L V
idot=(1/L)*(V-i*R);
35
Output
Fig-5
36
CHAPTER-4
BASIC ELECTRICAL AND NETWORKS APPLICATIONS
Ex-4.1
Find the voltage across the capacitor in the network shown in fig-1
Fig-1
Solution
Here numbers of nodes =4 and numbers of loops =3. This problem can be easily solved by writing the
generalized equation directly in the matrix from either using mesh analysis
[Z] [I]=[V] . This method required three equations.
Vc=(I2-I3)(-j3)
The mesh equation in the matrix form can be written as:
[ ] [ ]= [ ]
Output
V=
12
0
0
37
Z=
I=
1.1663 - 1.1461i
0.8326 - 0.7921i
1.0348 - 0.5157i
Vc =
-0.8292 + 0.6067i
Vcm =
2.5099
r=
1.0275
Ex-4.2
Validation of Maximum power Transfer Theorem.
The Thevenin equivalent circuit of a network show in fig-2. Calculate the output power supplied for
the following load resistances : RL=5 ,10 ,25 ,50 ,50 ,100 ,250 ,500. Plot the graph of
load voltage ,load current and load power for different values of load resistances.
Fig-2
38
Solution
Load current can be obtained as:
IL=
Power input:
Pin=Vth*IL
Power output
PL=VL*IL
Efficiency of the network:
Efficiency =
The M-file developed is shown below:
%programe to validate maximum power therom
%first thevenins therom
RL=[5 10 25 50 100 250 500];
Vth=100; Rth=50;
IL=Vth./(Rth+RL);
VL=IL.*RL;
PL=VL.*IL;
Pin=Vth.*IL;
Effi=(PL./Pin)*100;
Table=[RL',IL',VL',PL',];
disp(' resistance current voltage power');
disp(' (ohm) (amps) (volts) (watts)');
disp(Table);
semilogx(RL, PL,RL,VL,RL,IL*10,RL,Effi);
grid on;
xlabel('R (ohms)');
ylabel('P_{L} (w),V_{L} (V), 10*I_{L} (A),Effi{%}');
gtext('P_{L}');
gtext('V_{L}');
gtext('I_{L}');
gtext('Effi(%)')
Output
resistance current voltage power
(ohm) (amps) (volts) (watts)
5.0000 1.8182 9.0909 16.5289
10.0000 1.6667 16.6667 27.7778
25.0000 1.3333 33.3333 44.4444
50.0000 1.0000 50.0000 50.0000
100.0000 0.6667 66.6667 44.4444
250.0000 0.3333 83.3333 27.7778
500.0000 0.1818 90.9091 16.5289
39
Fig-3
Ex-4.3
Verification of superposition theorem:
Fig-4
Solution
Matrix form of equation of this circuit [R][I]=[V]
[ ] [ ]=[ ]
Mesh current can be calculated as
[I]=inv[R][V]
Consider V1 only V2=0 then load current
IL1=I1-I2
Consider V2 only V1=0 then load current
IL2=I1-I2
Total load current
I= IL1+IL2
The M-file developed is shown below:
40
%programe to verify superposition theorem
%experiment number six point six
R1=20; R2=10; R3=10;
V1=5; V2=10;
%Creating resistance matrix R
R=[(R1+R3) -R3; R3 -(R2+R3)];
V=[V1;V2];
I = inv(R)*V
IL = I(1)-I(2)
%now for verify superposition theorem
%consider V1 and V2=0
V3 = [V1;0]
I1 = inv(R)*V3
IL1 = I1(1)-I1(2)
%now consider V2 and V1=0
V4 = [0;V2]
I2 = inv(R)*V4
IL2=I2(1)-I2(2)
ILS=IL1+IL2
Output
I=
0.0000
-0.5000
IL =
0.5000
V3 =
5
0
I1 =
0.2000
0.1000
IL1 =
0.1000
V4 =
41
0
10
I2 =
-0.2000
-0.6000
IL2 =
0.4000
ILS =
0.5000
Ex-4.4
Plot voltage and current waveform in a resistive circuit when consider a AC source.
Fig-5
Solution
V=Vmsin(wt)
V=IR
I=V/R
The M-file developed is shown below:
% program to plot voltgae and current in a resistive circuit
%enter system parameters
V=input('enter the value of rms source voltage (in volts): ');
R=input('enter the value of resistance (in ohms): ');
f=input('enter the source frequency (in Hz): ');
n=input('enter the number of cycle for plot : ');
dt=input('enter time increment : ');
Vm=sqrt(2)*V;
Im=Vm/R;
w=2*pi*f;
T=1/f;
42
t=0:dt:n*T;
V=Vm*sin(w*t);
i=Im*sin(w*t);
plot(t,V,t,i);
grid on;
gtext('V');
gtext('i')
xlabel('time (sec)');
ylabel('v (volts) & i(amps)');
Output
Fig-6
Ex-4.6
Study of voltage and current waveforms and power factor in an inductive circuit and capacitive with
ac voltage input.
Fig-7
43
Fig-8
Solution
V=Vmsin(wt)
V=IR+LdI/dt
V=IR+1
44
Output
pf1 =
0.8467
phi2 =
0.3082
pf2 =
0.9529
Fig-9
45
Ex-4.7
Write a program to plot the amplitude and phase response Frequency response of a Low Pass Filter:
Fig-10
Solution
Low-pass filter allow to low frequency. Its block to high frequency.
Vo/Vc =1/(1+jRwC)
| |= 1/
The M-file developed is shown below:
%programe to plot the amplitude and phase response of a low pass filter
R=1000;C=10e-6;
f=1:2:1000;
%describe filter voltage ratio equation
V_ratio=1./(1+j*2*pi*f*R*C);
%calculate amplitude response
amplitude=abs(V_ratio);
%calculate phase response
phase=angle(V_ratio);
%create plots
subplot(2,1,1);
loglog(f,amplitude);
grid on;
title('magnitude response');
xlabel('frequency (rad/sec)');
ylabel('voltage ratio (Vo/Vi)');
subplot(2,1,2);
semilogx(f,phase);
title('phase response');
xlabel('frequency(rad/sec)');
ylabel('phase(rad)');
grid on;
46
Output
Fig-11
Ex-4.8
Write a program to plot the amplitude and phase response Frequency response of a bandPass Filter :
Fig-12
Solution
Band-pass filter allow to range of frequency. Its block to high and low frequency.
VR=R/(R+j*(W*L-1/(W*C)))
47
%define to global variable
VR=R./(R+j*(W*L-1./(W*C)));
%calculate absulate response
V1=abs(VR);
%calculate phase response
phase=angle(VR)
VR=[V1;phase];
% write scrit for bandpass filter for given figure 6.46
R=1.2e3;C=8e-6;L=5e-3;
W=10e-2:10:10e4;
%calculate amplitude and phase response
VR=fun_bandpass(R,C,L,W);
magnitude=VR(1,:);
phase=VR(2,:);
subplot(2,1,1);
loglog(W,magnitude);
title('magnitude response');
xlabel('frequency(rad/sec)');
ylabel('voltage ratio');
grid on;
subplot(2,1,2);
semilogx(W,phase);
title('phase response');
xlabel('frequency(rad/sec)')
ylabel('phase');
grid on;
Output
Fig-13
48
Ex-4.9
Write a program to plot the amplitude and phase response Frequency response of a highPass Filter :
Fig-14
Solution
High -pass filter allow to low frequency. Its block to low frequency.
VR=jwRC/(1+jwRC)
The M-file developed is shown below:
%function script for high-pass filter
function VR=fun_highpass(R,C,W);
%define global variable
VR=j*W*R*C./(1+j*W*R*C);
%calculate amplitude response
VR1=abs(VR);
%calculate phase response
phase=angle(VR);
VR=[VR1;phase];
49
Output
Fig-15
CHAPTER-5
INTRODUCTION TO SIMULINK
Ex-5.1
Plot a sinusoidal waveform (1V peak and 1rad/sec), differentiate it ,and observe both the wave form
on scope.
Solution
Fig-1
50
Output
Fig-2
Ex-5.2
Use Simulink to solve the following differential equation for :
dy/dt=10sint , y(0)=0
Solution
Fig-3
Output
51
Fig-4
Ex-5.3
Solve following differential equation using Simulink:
dy/dt = -10y+f(t) , y(0)=1
Solution
Here initial condition y(0)=1 is set in integration block parameter.
Fig-5
Output
52
Fig-6
Ex-5.4
Study the dynamic response of inductor current and capacitor voltage in a parallel R-L-C circuit
shown in fig-7 using Simulink. System parameters are R=25, L=0.2H, C=2000uF and Vs=110V.
Fig-7
Solution
Developed of system equation (Mathematical modeling)
The system equation in terms of states Vc and IL can be obtained as follows:
Ic =Is-IL
Is= (Vs-Vc)/R
Vc=1/C
IL=1/L
dVc/dt =1/C((Vs-Vc)/R-IL)
dIL/dt=1/LVc
53
Simulink block shown in fig-8:
Fig-8
Output
Fig-9
Ex-5.5
Obtain dynamic response of an armature controlled dc motor its block diagram show in fig-10 for
change speed in 500 to 1000 rpm at 0.5sec. The motor parameters are as follows : Armature resistance
Ra=2.5, armature inductance La=0.03H, moment of inertia Jm=0.022 Kgm2, coefficient of viscous
friction fm=0.00295Nms ,back emf constant Kb=1 V/(rad/sec) ,motor torque constant KT=1.2Nm/A
54
Fig-10
Solution
Simulink block shown in fig-11:
Fig-11
55
Output
fi
Fig-12
Ex-5.6
Develop a Simulink modal to generate sine and cosine waveforms of magnitude 5 and frequency 50Hz
using sine and cosine trigonometric functions.
Solution
Simulink block shown in fig-13:
Fig-13
56
Output
Fig-14
Ex-5.7
Modify the modal developed in Ex-5.6 by adding a ramp signal generator of time period
Solution
Simulink block shown in fig-15:
Fig-15
57
Output
Fig-16
58