MATLAB Programming & Its Applications For Electrical Engineers
MATLAB Programming & Its Applications For Electrical Engineers
WORKSHOP SCHEDULE
Day 1
Basics of MATLAB & Familiarization Mathematical Operations using MATLAB Working with Script files & user 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
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.
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
ARITHMETIC OPERATIONS
Arithmetic operation Addition Subtraction Multiplication Right Division Left Division Exponentiation Symbol + * / \ ^
EXPONENTIAL OPERATIONS
Function exp(x) log(x) log10(x) sqrt(x) Description Exponential (ex) Natural logarithm Base 10 logarithm Square root
TRIGONOMETRIC OPERATIONS
Function sin(x) csc(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
sind(x)
asin(x) sinh(x) atanh(x)
clear
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
Row Vector :
X=[7
5 3 1]
Column Vector :
Y=[7;
5; 3; 1]
12
EXERCISE:
Solve
5 x 3 y 2 z 10 2x 4 y 9z 9
3x 8 y 4 z 20
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 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
18
19
20
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
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
27