Professional Documents
Culture Documents
MATLAB Sample
Scripts
With Electrical Engineering
Applications
Introduction
This lecture we will do some practice on Basic
MATLAB Scripts.
We will start with simple scripts and will discuss
some electrical engineering applications.
More scripts including conditional statements will
be discussed in the next lecture.
Please review lectures on basic input and output
commands.
Script 1
% Program to calculate Height of a Building
from time a stone takes to reach the basement.
% Use g=9.81and k=0.05
g=9.81;
k=0.05;
time= input (‘Enter the time taken: ‘);
height=g*(time+(exp(-k*time)-1)/k)/k;
disp(’Depth of well is ’)
disp(depth)
disp(’metres’)
Script 2
% Program to calculate the BMI (body mass index)
P = (Vs^2*RL)./(RL+Rs).^2;
14
plot(RL,P)
13
xlabel('Load resistance')
Power dissipated
12
ylabel('Power dissipated')
11
10
9
1 2 3 4 5 6 7 8 9 10
Load resistance
Curve fitting
% Second order curve fitting
%enter the input x and y vectors
x = [0 .1 .2 .3 .4 .5 .6 .7 .8 .9 1];
y = [-.447 1.978 3.28 6.16 7.08 7.34 7.66 9.56 9.48 9.30 11.2];
n = 2;
p = polyfit( x, y, n ) %Find the best quadratic fit to the
data
xi = linspace(0,1,100);
yi = polyval(p, xi); % evaluate the polynomial
plot(x,y,’-o’, xi, yi, ‘-’)
xlabel(‘x’), ylabel(‘y = f(x)’)
title(‘Second Order Curve Fitting Example’)
8
plot(T,N) 45
40
xlabel('Torque') 35
ylabel('Speed in RPM')
Armature current
30
title('Speed-Torque Curve') 25
20
plot(T,Ia) 15
xlabel('Torque') 10
ylabel('Armature Current') 5
0
0 10 20 30 40 50 60 70 80 90 100
Torque
study the characteristics of shunt and series DC motors
Induction Machine
Torque-Speed Curve for a squirrel cage Induction Motor
Ns=1500; % Synchronous speed;
R1=15.6 ;R2=14;X1=18; X2=23;Xm=260;Vt=400/sqrt(3);
s = 0.002:0.002:1; % vector of slip
N = Ns.*(1-s); % Speed, in RPM
Ws = 2*pi*Ns/60; % Synchronous speed in rad/sec
Rr = R2./ s; % Rotor resistance
Zr = j*X2 + Rr; % Total rotor impedance
Za = j*Xm*Zr./(j*Xm+Zr); % Air-gap impedance
Zt = R1 + j*X1 +Za; % Terminal impedance
Ia = Vt ./ Zt; % Terminal Current
I2 = j*Xm*Ia./(j*Xm+Zr); % Rotor Current
Pag = 3* (abs(I2)).^2.*Rr; % Air-Gap Power
Pm = Pag.* (1-s); % Converted Power
Trq = Pag/ Ws; % Developed Torque
subplot(2,1,1)
plot(N, Trq)
xlabel('Speed in RPM')
ylabel('Torque (Nm)')
subplot(2,1,2)
plot(Ia, Trq)
xlabel('Load Current')
ylabel('Torque (Nm)')
Synchronous Motor
%M-file to calculate and plot the terminal voltage % of a synchronous generator as a function of load
% for power factors of 0.8 lagging, 1.0, and 0.8 leading.
% Define values for this generator
EA = 277; % Internal gen voltage
I = 0:2:240; % Current values (A)
R = 0.03; % R (ohms)
X = 0.25; % XS (ohms)
% Calculate the voltage for the lagging PF case
VP_lag = sqrt( EA^2 - (X.*I.*0.8 - R.*I.*0.6).^2 )- R.*I.*0.8 - X.*I.*0.6;
VT_lag = VP_lag .* sqrt(3);
% Calculate the voltage for the leading PF case
VP_lead = sqrt( EA^2 - (X.*I.*0.8 + R.*I.*0.6).^2 )- R.*I.*0.8 + X.*I.*0.6;
VT_lead = VP_lead .* sqrt(3);
% Calculate the voltage for the unity PF case
VP_unity = sqrt( EA^2 - (X.*I).^2 );
VT_unity = VP_unity .* sqrt(3);