SlideShare a Scribd company logo
4
Most read
6
Most read
7
Most read
MATLAB Sample
Scripts
With Electrical Engineering
Applications
Shameer A Koya
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)
% Input your Height and Weight
weight = input('Type your weight (kg): ');
height = input('Type your height (m): ');
bmi = weight / height^2;
% Display the BMI
fprintf('Your Body Mass Index is %fn", bmi);
Script 3
% Program to calculate Electricity bill.
w = input('Enter power of your device (in watts):
');
h = input('Enter time (in hours): ');
r = input('Enter electricity rate (in dollars per
KWH): ');
ec = w * h/1000 * r;
disp(’Your Electricity bill is’)
Disp(ec)
Power transfer vs Load resistance curve
RL = 1:0.01:10;
Vs = 12;
Rs = 2.5;
P = (Vs^2*RL)./(RL+Rs).^2;
plot(RL,P)
xlabel('Load resistance')
ylabel('Power dissipated')
1 2 3 4 5 6 7 8 9 10
9
10
11
12
13
14
15
Load resistance
Powerdissipated
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’)
Series RLC circuit
 % Program to plot current vs frequency semilog plot of a
series RLC circuit
 R = input('Enter value of resistance in ohms:');
 L = input('Enter value of inductance in Henary:');
 C = input('Enter value of capacitance in Farads:');
 V = input('Supply voltage in volts:')
 f = 0 : 1: 1000000;
 XL = 2 * pi .* f * L;
 XC = 1./(2 * pi .* f * C);
 Z = (R^2-(XL-XC).^2).^0.5;
 I = V ./ Z;
 semilogx(f,real(I));
 title('Current Vs Log frequency plot' );
 xlabel('fLogf');
 ylabel('Current in A');
8
DC Machines Characteristics
MATLAB program to calculate the characteristics of separately excited DC motor.
%Required parameters are Ra, k, Rf, Vt, Ns, k, Vf
clc;
Ra=0.5 ;Rf=250;Vt=250;k=2;Vf=250;
T = 0:100; % vector of torque
If=Vf/Rf;
Ia=T/k;
Ea=Vt-Ia*Ra;
w=Ea/k/If;
N=w*60/2/pi;
plot(T,N)
xlabel('Torque')
ylabel('Speed in RPM')
title('Speed-Torque Curve')
plot(T,Ia)
xlabel('Torque')
ylabel('Armature Current')
study the characteristics of shunt and series DC motors
0 10 20 30 40 50 60 70 80 90 100
0
5
10
15
20
25
30
35
40
45
50
Torque
Armaturecurrent
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);
% Plot the terminal voltage versus load
plot(I,abs(VT_lag),'b');
hold on;
plot(I,abs(VT_unity),'k--');
plot(I,abs(VT_lead),'r:');
legend('0.8 PF lagging','1.0 PF','0.8 PF leading');
grid on;
hold off;
TransmissionLineVoltageRegulation
12
% INPUT THE LINE PARAMETERS
Z = input ('Enter Line Impedence, Z line: ');
Y = input ('Enter Line Admittance, Y line: ');
P = input ('Enter Recieving end Power, Pr: ');
VL = input ('Enter Recieving end Voltage, Vr: ');
PF = input ('Enter Recieving end Power factor, pfr: ');
% CALCULATION OF ABCD CONSTANTS
A=(1+Z*Y/2);
D=A;
B=Z;
C=Y*(1+Z*Y/4);
A1=acos(PF);
VR=VL/sqrt(3);
% CALCULATION OF RECEIVING END CURRENT
IR= P/(sqrt(3)*VL*PF);
IR=IR*(cos(A1)-j*sin(A1));
% CALCULATION OF SENDING END VOLTAGE
VS=A*VR+ B*IR;
VSA=abs(VS);
VSL=sqrt(3)*VSA;
% CALCULATION OF VOLTAGE REGULATION
VVRR=(VSA/abs(A)-VR)/VR;
VREGULATION = VVRR*100;
% CALCULATION OF SENDING END CURRENT
IS=C*VR+D*IR;
ISA=abs(IS);
% CALCULATION OF PHASE ANGLE
PA=angle(VS)*180/pi;
PB = angle (IS) *180/pi;
PS=(PA-PB)*pi/180;
PFS=cos(PS);
PS=sqrt(3)*ISA*VSL*PFS; % CALCULATION OF SENDING END POWER
EFFICIENCY=(P/PS)* 100; % CALCULATION OF EFFICIENCY
fprintf ('Efficiency is %dn', EFFICIENCY);
fprintf ('Voltage Regulation is %d n', VREGULATION);

More Related Content

PPTX
Matlab m files and scripts
PDF
PID controller in control systems
PPT
Map Reduce
PPT
Chapter 3: Data & Signals
PPT
Introduction to Algorithms & flow charts
PPT
Socket Programming
PPT
Chap 2 discrete_time_signal_and_systems
PPTX
CONTROL SYSTEMS PPT ON A LEAD COMPENSATOR CHARACTERISTICS USING BODE DIAGRAM ...
Matlab m files and scripts
PID controller in control systems
Map Reduce
Chapter 3: Data & Signals
Introduction to Algorithms & flow charts
Socket Programming
Chap 2 discrete_time_signal_and_systems
CONTROL SYSTEMS PPT ON A LEAD COMPENSATOR CHARACTERISTICS USING BODE DIAGRAM ...

What's hot (20)

PDF
Double Side Band – Suppressed Carrier (DSB-SC) Modulation Demodulation using ...
PPTX
Channel capacity
PPTX
Go Back N ARQ
PDF
9. Block Diagram,ontrol signal to bring the process variable output of the pl...
PPTX
Air Compressor reciprocating single stage
PDF
User defined functions in matlab
PDF
Dataflow Analysis
PPT
Chapter 32
PDF
Modern Control System (BE)
PPSX
Adoptive retransmission in TCP
PDF
7-vcc-how-to-design-for-manufacture-and-assembly (1).pdf
PPT
Flow control and error control
PPT
Digital Audio Broadcasting By SAIKIRAN PANJALA
PDF
Chapter 01 Planning Computer Program (re-upload)
PPTX
Multivector and multiprocessor
DOCX
Amdahl`s law -Processor performance
PPTX
network ram parallel computing
PPT
Chapter 3 slides (Distributed Systems)
PPT
Digital to analog conversion
PPT
Chapter 03 cyclic codes
Double Side Band – Suppressed Carrier (DSB-SC) Modulation Demodulation using ...
Channel capacity
Go Back N ARQ
9. Block Diagram,ontrol signal to bring the process variable output of the pl...
Air Compressor reciprocating single stage
User defined functions in matlab
Dataflow Analysis
Chapter 32
Modern Control System (BE)
Adoptive retransmission in TCP
7-vcc-how-to-design-for-manufacture-and-assembly (1).pdf
Flow control and error control
Digital Audio Broadcasting By SAIKIRAN PANJALA
Chapter 01 Planning Computer Program (re-upload)
Multivector and multiprocessor
Amdahl`s law -Processor performance
network ram parallel computing
Chapter 3 slides (Distributed Systems)
Digital to analog conversion
Chapter 03 cyclic codes
Ad

Viewers also liked (20)

PPTX
Introduction to Matlab Scripts
PPTX
Anonymous and Inline Functions in MATLAB
PPTX
Matlab Script - Loop Control
PPTX
User defined Functions in MATLAB Part 1
PPTX
MATLAB Programming - Loop Control Part 2
PPTX
Matlab solving rlc circuit
PDF
Dsp file
PPTX
Matlab assignment help
PPTX
Matlab: Procedures And Functions
PPTX
MATLAB programming tips 2 - Input and Output Commands
PPTX
Mat lab workshop
PPTX
User Defined Functions in MATLAB part 2
DOC
Jumping statements
DOCX
Matlab time series example
PPT
Loops in matlab
PDF
Journal Club - Best Practices for Scientific Computing
PPTX
User Defined Functions in MATLAB Part-4
PPTX
Do While and While Loop
PDF
Circuit analysis i with matlab computing and simulink sim powersystems modeling
PPTX
Cruise control simulation using matlab
Introduction to Matlab Scripts
Anonymous and Inline Functions in MATLAB
Matlab Script - Loop Control
User defined Functions in MATLAB Part 1
MATLAB Programming - Loop Control Part 2
Matlab solving rlc circuit
Dsp file
Matlab assignment help
Matlab: Procedures And Functions
MATLAB programming tips 2 - Input and Output Commands
Mat lab workshop
User Defined Functions in MATLAB part 2
Jumping statements
Matlab time series example
Loops in matlab
Journal Club - Best Practices for Scientific Computing
User Defined Functions in MATLAB Part-4
Do While and While Loop
Circuit analysis i with matlab computing and simulink sim powersystems modeling
Cruise control simulation using matlab
Ad

Similar to MATLAB Scripts - Examples (20)

PDF
90981041 control-system-lab-manual
DOCX
Ece 415 control systems, fall 2021 computer project 1
PDF
Digital control for power electronic Conversters
PPTX
PWM Step-down Converter(NJM2309)
PPTX
PWM Buck Converter using Average Model
DOCX
ACS 22LIE12 lab Manul.docx
PPTX
Electrical Engineering Assignment Help
PPTX
「SPICEの活用方法」セミナー資料(28JAN2011) PPT
PDF
Psms lab manual
PPTX
Chapter two Part two.pptx
PDF
SlidesPartII_digital_control_power_electronics.pdf
PPTX
PSpiceで位相余裕度シミュレーション
PDF
Matlab fair-record-model
PDF
PID Tuning using Ziegler Nicholas - MATLAB Approach
PPTX
Abdul Haseeb
PDF
Quiz
PDF
Final Project
PDF
EE443 - Communications 1 - Lab 1 - Loren Schwappach.pdf
PDF
Design of a novel controller to increase the frequency response of an aerospace
PDF
Modeling of Self Excited Induction Generator
90981041 control-system-lab-manual
Ece 415 control systems, fall 2021 computer project 1
Digital control for power electronic Conversters
PWM Step-down Converter(NJM2309)
PWM Buck Converter using Average Model
ACS 22LIE12 lab Manul.docx
Electrical Engineering Assignment Help
「SPICEの活用方法」セミナー資料(28JAN2011) PPT
Psms lab manual
Chapter two Part two.pptx
SlidesPartII_digital_control_power_electronics.pdf
PSpiceで位相余裕度シミュレーション
Matlab fair-record-model
PID Tuning using Ziegler Nicholas - MATLAB Approach
Abdul Haseeb
Quiz
Final Project
EE443 - Communications 1 - Lab 1 - Loren Schwappach.pdf
Design of a novel controller to increase the frequency response of an aerospace
Modeling of Self Excited Induction Generator

Recently uploaded (20)

PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Cell Types and Its function , kingdom of life
PDF
RMMM.pdf make it easy to upload and study
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Basic Mud Logging Guide for educational purpose
PDF
Complications of Minimal Access Surgery at WLH
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
VCE English Exam - Section C Student Revision Booklet
Renaissance Architecture: A Journey from Faith to Humanism
Cell Types and Its function , kingdom of life
RMMM.pdf make it easy to upload and study
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
TR - Agricultural Crops Production NC III.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Sports Quiz easy sports quiz sports quiz
Final Presentation General Medicine 03-08-2024.pptx
Basic Mud Logging Guide for educational purpose
Complications of Minimal Access Surgery at WLH
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
STATICS OF THE RIGID BODIES Hibbelers.pdf
O7-L3 Supply Chain Operations - ICLT Program
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Microbial disease of the cardiovascular and lymphatic systems
Chapter 2 Heredity, Prenatal Development, and Birth.pdf

MATLAB Scripts - Examples

  • 1. MATLAB Sample Scripts With Electrical Engineering Applications Shameer A Koya
  • 2. 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.
  • 3. 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’)
  • 4. Script 2 % Program to calculate the BMI (body mass index) % Input your Height and Weight weight = input('Type your weight (kg): '); height = input('Type your height (m): '); bmi = weight / height^2; % Display the BMI fprintf('Your Body Mass Index is %fn", bmi);
  • 5. Script 3 % Program to calculate Electricity bill. w = input('Enter power of your device (in watts): '); h = input('Enter time (in hours): '); r = input('Enter electricity rate (in dollars per KWH): '); ec = w * h/1000 * r; disp(’Your Electricity bill is’) Disp(ec)
  • 6. Power transfer vs Load resistance curve RL = 1:0.01:10; Vs = 12; Rs = 2.5; P = (Vs^2*RL)./(RL+Rs).^2; plot(RL,P) xlabel('Load resistance') ylabel('Power dissipated') 1 2 3 4 5 6 7 8 9 10 9 10 11 12 13 14 15 Load resistance Powerdissipated
  • 7. 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. Series RLC circuit  % Program to plot current vs frequency semilog plot of a series RLC circuit  R = input('Enter value of resistance in ohms:');  L = input('Enter value of inductance in Henary:');  C = input('Enter value of capacitance in Farads:');  V = input('Supply voltage in volts:')  f = 0 : 1: 1000000;  XL = 2 * pi .* f * L;  XC = 1./(2 * pi .* f * C);  Z = (R^2-(XL-XC).^2).^0.5;  I = V ./ Z;  semilogx(f,real(I));  title('Current Vs Log frequency plot' );  xlabel('fLogf');  ylabel('Current in A'); 8
  • 9. DC Machines Characteristics MATLAB program to calculate the characteristics of separately excited DC motor. %Required parameters are Ra, k, Rf, Vt, Ns, k, Vf clc; Ra=0.5 ;Rf=250;Vt=250;k=2;Vf=250; T = 0:100; % vector of torque If=Vf/Rf; Ia=T/k; Ea=Vt-Ia*Ra; w=Ea/k/If; N=w*60/2/pi; plot(T,N) xlabel('Torque') ylabel('Speed in RPM') title('Speed-Torque Curve') plot(T,Ia) xlabel('Torque') ylabel('Armature Current') study the characteristics of shunt and series DC motors 0 10 20 30 40 50 60 70 80 90 100 0 5 10 15 20 25 30 35 40 45 50 Torque Armaturecurrent
  • 10. 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)')
  • 11. 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); % Plot the terminal voltage versus load plot(I,abs(VT_lag),'b'); hold on; plot(I,abs(VT_unity),'k--'); plot(I,abs(VT_lead),'r:'); legend('0.8 PF lagging','1.0 PF','0.8 PF leading'); grid on; hold off;
  • 12. TransmissionLineVoltageRegulation 12 % INPUT THE LINE PARAMETERS Z = input ('Enter Line Impedence, Z line: '); Y = input ('Enter Line Admittance, Y line: '); P = input ('Enter Recieving end Power, Pr: '); VL = input ('Enter Recieving end Voltage, Vr: '); PF = input ('Enter Recieving end Power factor, pfr: '); % CALCULATION OF ABCD CONSTANTS A=(1+Z*Y/2); D=A; B=Z; C=Y*(1+Z*Y/4); A1=acos(PF); VR=VL/sqrt(3); % CALCULATION OF RECEIVING END CURRENT IR= P/(sqrt(3)*VL*PF); IR=IR*(cos(A1)-j*sin(A1)); % CALCULATION OF SENDING END VOLTAGE VS=A*VR+ B*IR; VSA=abs(VS); VSL=sqrt(3)*VSA; % CALCULATION OF VOLTAGE REGULATION VVRR=(VSA/abs(A)-VR)/VR; VREGULATION = VVRR*100; % CALCULATION OF SENDING END CURRENT IS=C*VR+D*IR; ISA=abs(IS); % CALCULATION OF PHASE ANGLE PA=angle(VS)*180/pi; PB = angle (IS) *180/pi; PS=(PA-PB)*pi/180; PFS=cos(PS); PS=sqrt(3)*ISA*VSL*PFS; % CALCULATION OF SENDING END POWER EFFICIENCY=(P/PS)* 100; % CALCULATION OF EFFICIENCY fprintf ('Efficiency is %dn', EFFICIENCY); fprintf ('Voltage Regulation is %d n', VREGULATION);