A WORKSHOP ON
MATLAB PROGRAMMING & ITS APPLICATIONS FOR ELECTRICAL ENGINEERS Organized by Department of Electrical Engineering MARUDHAR ENGINEERING COLLEGE, Raisar, NH-11, Bikaner Created by - Rohan Sharma-
WORKSHOP SCHEDULE
Day 1 Basics of MATLAB & Familiarization Mathematical Operations using MATLAB Working with Script files & defined functions Applications of MATLAB in Electrical Circuits
Day2 Numerical Methods using MATLAB MATLAB Applications in Electrical Power System Applications of MATLAB in Power Electronics Simulink Toolbox for Electrical Engineers
2
Basics of MATLAB
MATLAB is a software package for high performance computations & visualizations. It has very wide application in the field of engineering & technology.
The word MATLAB stands for MATrices LABoratory.
3
MATLAB WINDOWS [A] Matlab desktop Workspace: All the information and details of every variable entered into the command window is displayed here. Command History stores history of every command. Every operation which we want to perform is to be entered in command window. [B] Editor Window – This window is used to write any program, which includes many command statements. By using this window one can edit any previously written command. [C] Figure Window – All the figures are shown in a separate figure window. 4
MATLAB FILE TYPES .m – Files: Every script and function file is written in this format, with ‘.m’ extension. .p – Files: This format is fixed for coded files, source codes are neither visible nor be edited in these files. .mex – Files: Any program written in some other language like C, JAVA or FORTRAN can be imported into matlab using ‘.mex’ extension files. .mat – Files: These are the machine code of m-files. .fig – Files: any figure can be shown by machine code using ‘.fig’ extension. 5
SOME USEFUL COMMANDS >>ver >>computer >>dir >>clc >>pwd
>> help >> exit >> quit >> help
>> clf >> clock >> date >> home
6
ARITHMETIC OPERATIONS Arithmetic operation Addition Subtraction Multiplication Right Division Left Division Exponentiation
Symbol + * / \ ^
7
EXPONENTIAL OPERATIONS Function exp(x) log(x) log10(x) sqrt(x)
Description Exponential (ex) Natural logarithm Base 10 logarithm Square root
8
TRIGONOMETRIC OPERATIONS Function sin(x) csc(x)
sind(x) asin(x) sinh(x) atanh(x)
Description Computes the sine of x, where x is in radians. Computes the cosec of x, where x is in radians. Computes the sin of x, where x is in degrees. Computes the arcsine or inverse sine of x, where x is in radians. Computes the hyperbolic sine of x, where x is in radians. Computes the inverse hyperbolic tangent of x. 9
COMPLEX NUMBER FUNCTIONS Function
Description Computes the complex conjugate of the conj(x) complex number x. Thus, if x is equal to a+ib, then conj(x) will be equal to a-ib. Computes the real potion of the complex real(x) number x. Computes the imaginary potion of the imag(x) complex number x. Computes the absolute value of magnitude abs(x) of the complex number x. Computes the angle of the complex number angle(x) x. 10
COMMANDS FOR MANAGING VARIABLES Command
Description
clear
Removes all variables from the memory.
clear a, b Clears only variables a and b from memory. who Lists the variables currently in workspace. whos Displays a lists of the variables currently in the memory and their size together with information about their bytes & class. 11
WORKING WITH ARRAYS
Row Vector : X=[7
5 3 1]
Column Vector : Y=[7;
5; 3; 1]
12
EXERCISE: Solve
the given equations in MATLAB.
5 x 3 y 2 z 10
3x 8 y 4 z 20 2x 4 y 9z 9
Hint: A=[3X3] B=[3X1]
Ans=inv(A)*B 13
SCRIPT FILES: Typing commands in Command Window is fine for simple tasks, but for more complex ones we can store the typed input into a file and tell MATLAB to get its input from the file. Such files must have the extension “.m”. There are two kinds of m-files: the script-files and the function files. Script files do not take the input arguments or return the output arguments. The function files may take input arguments or return output arguments.
14
EXERCISE : Write
a program to draw the following curves in a spitted single window by using the subplot command. Also use legend command to distinguish between different plots. p = 3x + 5 q = ex r = 1/x s = sin(x) 15
SOLUTION:
x = 0:.1:2*pi; subplot(2,2,1); plot(3*x+5) legend('p') title('Plot of 3x+5') subplot(2,2,2); plot(exp(x)); legend('q') title('Plot of exponent of x') subplot(2,2,3); plot(1./x); legend('r') title('Plot of 1/x') subplot(2,2,4); plot(sin(x)); legend('s') title('Plot of Sin(x)‘) 16
INPUT - OUTPUT STATEMENTS :
Input function: Syntax: variable = input(‘string’) Example: x = input(‘Enter the value of x’)
Display function: Syntax: disp(‘string’) Example: disp(‘I am learning MATLAB’)
fprintf function: Syntax: fprintf(‘string, variable, escape character’, data) Example: fprintf(‘The sum of a & b is =%d \n’, sum) 17
USE MATLAB IN ELECTRICAL CIRCUITS :
18
19
20
SOLUTION USNG MATLAB :
Z = [40 -10 -30; -10 30 -5; -30 -5 65]; V = [10 0 0]'; % solve for the loop currents I = inv(Z)*V; % current through RB is calculated IRB = I(3) - I(2); fprintf('the current through R is %f Amps \n',IRB) % the power supplied by source is calculated PS = I(1)*10; fprintf('the power supplied by 10V source is %f watts \n',PS) 21
MATLAB answers are : the current through R is 0.037037 Amps the power supplied by 10V source is 4.753086 watts
22
FUNCTIONS IN MATLAB :
Defining of a function function addtwo(x,y)
% addtwo(x,y) Adds two numbers, vectors, whatever, and print the result = x+y x+y
23
Example: >> x=2.3; >> y=3.3; >> addtwo(x,y) ans = 5.6000
24
Defining of a function function [r,theta] = cart2plr(x,y) % cart2plr Convert Cartesian coordinates to polar coordinates % [r,theta] = cart2plr(x,y) computes r and theta with % r = sqrt(x^2 + y^2); % theta = atan2(y,x); r = sqrt(x^2 + y^2); theta = atan2(y,x);
25
Example: >> r
[r,th]=cart2plr(2,-.7)
=
2.1190
th
=
-0.3367 26
End of day one.!
27