SlideShare a Scribd company logo
1 of 17
11
M File Script and Function
Lecture Series – 5
by
Shameer Koya
Files used in MATLAB
 .m files
 Both functions and scripts are stored in .m files
 .mat files
 The MATLAB workspace (or specific variables) can be saved in
.mat files
 These files are easy to save and load, and MATLAB accessing
them is very efficient
 .fig files (next week)
 Plots can be saved in .fig files, and then the figure can be edited
without reloading data
2
MATLAB Editor/Debugger
3
.m files
 Code can be saved in .m files and run in the
command window – exact implementation
depends on whether the code is a function or a
script
4
Script
 Simplest kind of m-file
 Type up a bunch of commands and save as
filename.m
 Type filename in command window to run
 Example: first_program.m
5
Function
 Functions are more complex than scripts
 Functions have their own local variables
 Functions return output as specified, and can accept
input as specified
6
Commenting
 Comment your code!
 Any line starting with % is a comment
 Comments can be added to the end of existing lines
by adding a %
 Note that anything after % will be ignored
 In editor screen comments are green
 Any comments written at the beginning of an m-
file will be displayed by the command help
filename
7
Flow control
 Conditional control – if, else, switch
 Loop control – for, while, continue, break
 Program termination – return
8
Conditional control – if, else, elseif
if test statement
statements
elseif test statement
statements
else
statements
end
Note that ==,~=,>,<
are all scalar tests.
9
if I == J
A(I,J) = 2;
elseif abs(I-J) == 1
A(I,J) = -1;
else
A(I,J) = 0;
end
Loop control – for, while
for varname = min:max
statements
end
while condition is true
statements
end
10
N=10;
for I = 1:N
for J = 1:N
A(I,J) = 1/(I+J-1);
end
end
I=1; N=10;
while I<=N
J=1;
while J<=N
A(I,J)=1/(I+J-1);
J=J+1;
end
I=I+1;
end
CS 111
11
MATLAB Examples
 Find the number of positive numbers in a vector
x = input( 'Enter a vector: ' );
count = 0;
for ii = 1:length(x),
if ( x(ii) > 0 ),
count = count + 1;
end
end
fprintf('Number of positive numbers is %dn', count);
MATLAB Examples
 Program to find grade from the mark
 s=input('Enter the Mark: '); % enter the mark
 if s>= 90
 disp ('Grade: A');
 elseif s>=80
 disp ('Grade: B');
 elseif s>=70
 disp ('Grade: C');
 elseif s>=60
 disp ('Grade: D');
 else
 disp ('Grade: F');
 end
12
13
MATLAB Examples
Plot the switching response of a given RC circuit
 Where Vci is the initial capacitor voltage; Vcf is the voltage the capacitor will reach if it
charges for an infinite amount of time.
 





 
msforteVVV
mstforV
tV RCtt
cfcicf
ci
c
5.1)(
5.10
/)( 0
14
MATLAB Examples
 Vci = input('Enter initial capacitor voltage, Vci: ');
 Vcf = input('Enter Final capacitor voltage, Vcf: ');
 R = input('Enter Resistance value, R: ');
 C = input('Enter Capacitance value, C: ');
 t0 = input('Enter Switching time, t0: ');
 tf = input('Enter Simulation end time, tf: ');
 t=linspace(0,tf,1000);
 Vc=zeros(1,1000);
 for i=1:1000
 if t(i)<t0
 Vc(i)=Vci;
 else Vc(i)=Vcf+(Vci-Vcf)*exp(-(t(i)-t0)/(R*C));
 end
 end
 plot(t*1000,Vc);
 title('RC Step Response')
 ylabel('Capacitor voltage')
 xlabel('time in msec')
 grid on
Plot the switching response of a given RC circuit
15
MATLAB Examples
 Find the index of the largest number in a vector
x = input( 'Enter a vector: ' );
max_value = x(1);
max_index = 1;
for ii = 2:length(x),
if ( x(ii) > max_value ),
max_value = x(ii);
max_index = ii;
end
end
fprintf( 'Max value is %dn', max_value );
fprintf( 'Its index is %dn', max_index );
 What if the max value occurs more than once?
 Write program to find step response of RL circuit
 Program to make p w m signals
16
17
Thanks
Questions ??

More Related Content

What's hot

BEC- 26 control systems_unit-II
BEC- 26 control systems_unit-IIBEC- 26 control systems_unit-II
BEC- 26 control systems_unit-IIShadab Siddiqui
 
Matlab (Presentation on MATLAB)
Matlab (Presentation on MATLAB)Matlab (Presentation on MATLAB)
Matlab (Presentation on MATLAB)Chetan Allapur
 
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulinkMATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulinkreddyprasad reddyvari
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLABRavikiran A
 
PLC (PROGRAMMABLE LOGIC CONTROLLER)
PLC (PROGRAMMABLE LOGIC CONTROLLER)PLC (PROGRAMMABLE LOGIC CONTROLLER)
PLC (PROGRAMMABLE LOGIC CONTROLLER)Manoj Gowda K
 
Basics Electronics Engineering ppt.
Basics Electronics Engineering ppt.Basics Electronics Engineering ppt.
Basics Electronics Engineering ppt.PRINCE SHARMA
 
Transfer function and mathematical modeling
Transfer  function  and  mathematical  modelingTransfer  function  and  mathematical  modeling
Transfer function and mathematical modelingvishalgohel12195
 
Classical and Modern Control Theory
Classical and Modern Control TheoryClassical and Modern Control Theory
Classical and Modern Control TheoryQazi Ejaz
 
Signal flow graph Mason’s Gain Formula
Signal flow graph Mason’s Gain Formula Signal flow graph Mason’s Gain Formula
Signal flow graph Mason’s Gain Formula vishalgohel12195
 
Variables in matlab
Variables in matlabVariables in matlab
Variables in matlabTUOS-Sam
 
Modern Control - Lec07 - State Space Modeling of LTI Systems
Modern Control - Lec07 - State Space Modeling of LTI SystemsModern Control - Lec07 - State Space Modeling of LTI Systems
Modern Control - Lec07 - State Space Modeling of LTI SystemsAmr E. Mohamed
 
State equations for physical systems
State equations for physical systemsState equations for physical systems
State equations for physical systemsSarah Krystelle
 

What's hot (20)

Impulse Response ppt
Impulse Response pptImpulse Response ppt
Impulse Response ppt
 
BEC- 26 control systems_unit-II
BEC- 26 control systems_unit-IIBEC- 26 control systems_unit-II
BEC- 26 control systems_unit-II
 
Matlab (Presentation on MATLAB)
Matlab (Presentation on MATLAB)Matlab (Presentation on MATLAB)
Matlab (Presentation on MATLAB)
 
Matlab
MatlabMatlab
Matlab
 
Lecture 4: Classification of system
Lecture 4: Classification of system Lecture 4: Classification of system
Lecture 4: Classification of system
 
Introduction to control systems
Introduction to control systemsIntroduction to control systems
Introduction to control systems
 
Class 20 effect of kp, ki & kd and pid control mode
Class 20   effect of kp, ki & kd and pid control modeClass 20   effect of kp, ki & kd and pid control mode
Class 20 effect of kp, ki & kd and pid control mode
 
Nyquist plot
Nyquist plotNyquist plot
Nyquist plot
 
Plc notes
Plc notesPlc notes
Plc notes
 
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulinkMATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
 
Introduction to MATLAB
Introduction to MATLABIntroduction to MATLAB
Introduction to MATLAB
 
PLC (PROGRAMMABLE LOGIC CONTROLLER)
PLC (PROGRAMMABLE LOGIC CONTROLLER)PLC (PROGRAMMABLE LOGIC CONTROLLER)
PLC (PROGRAMMABLE LOGIC CONTROLLER)
 
Basics Electronics Engineering ppt.
Basics Electronics Engineering ppt.Basics Electronics Engineering ppt.
Basics Electronics Engineering ppt.
 
Transfer function and mathematical modeling
Transfer  function  and  mathematical  modelingTransfer  function  and  mathematical  modeling
Transfer function and mathematical modeling
 
Classical and Modern Control Theory
Classical and Modern Control TheoryClassical and Modern Control Theory
Classical and Modern Control Theory
 
Signal flow graph Mason’s Gain Formula
Signal flow graph Mason’s Gain Formula Signal flow graph Mason’s Gain Formula
Signal flow graph Mason’s Gain Formula
 
Variables in matlab
Variables in matlabVariables in matlab
Variables in matlab
 
Modern Control - Lec07 - State Space Modeling of LTI Systems
Modern Control - Lec07 - State Space Modeling of LTI SystemsModern Control - Lec07 - State Space Modeling of LTI Systems
Modern Control - Lec07 - State Space Modeling of LTI Systems
 
ppt on PLC
ppt on PLCppt on PLC
ppt on PLC
 
State equations for physical systems
State equations for physical systemsState equations for physical systems
State equations for physical systems
 

Viewers also liked

MATLAB SIMULATIONS OF SERIES RESONANT CIRCUIT
MATLAB SIMULATIONS OF SERIES RESONANT CIRCUITMATLAB SIMULATIONS OF SERIES RESONANT CIRCUIT
MATLAB SIMULATIONS OF SERIES RESONANT CIRCUITMinh Anh Nguyen
 
Matlab introduction
Matlab introductionMatlab introduction
Matlab introductionAmeen San
 
Matlab matrices and arrays
Matlab matrices and arraysMatlab matrices and arrays
Matlab matrices and arraysAmeen San
 
Matlab ploting
Matlab plotingMatlab ploting
Matlab plotingAmeen San
 
Matlab complex numbers
Matlab complex numbersMatlab complex numbers
Matlab complex numbersAmeen San
 
The experiment of the series resonant circuit
The experiment of the series resonant circuitThe experiment of the series resonant circuit
The experiment of the series resonant circuita-mairu
 
Microprocessor and Microcontroller lec4
Microprocessor and Microcontroller lec4Microprocessor and Microcontroller lec4
Microprocessor and Microcontroller lec4Ameen San
 
Matlab dc circuit analysis
Matlab dc circuit analysisMatlab dc circuit analysis
Matlab dc circuit analysisAmeen San
 
Matlab polynimials and curve fitting
Matlab polynimials and curve fittingMatlab polynimials and curve fitting
Matlab polynimials and curve fittingAmeen San
 
Microprocessor and Microcontroller lec5
Microprocessor and Microcontroller lec5Microprocessor and Microcontroller lec5
Microprocessor and Microcontroller lec5Ameen San
 
Plc analog input output programming
Plc analog input output programmingPlc analog input output programming
Plc analog input output programmingEngr Alam
 
Plc analog Tutorial
Plc analog TutorialPlc analog Tutorial
Plc analog TutorialElectro 8
 
Matlab dc motor modeling
Matlab dc motor modelingMatlab dc motor modeling
Matlab dc motor modelingAmeen San
 
Matlab simpowersystem
Matlab simpowersystemMatlab simpowersystem
Matlab simpowersystemAmeen San
 
Mat lab solving equations simulink
Mat lab solving equations simulinkMat lab solving equations simulink
Mat lab solving equations simulinkAmeen San
 
Matlab solving rlc circuit
Matlab solving rlc circuitMatlab solving rlc circuit
Matlab solving rlc circuitAmeen San
 
PLC input and output devices
PLC input and output devices PLC input and output devices
PLC input and output devices Ameen San
 

Viewers also liked (20)

MATLAB SIMULATIONS OF SERIES RESONANT CIRCUIT
MATLAB SIMULATIONS OF SERIES RESONANT CIRCUITMATLAB SIMULATIONS OF SERIES RESONANT CIRCUIT
MATLAB SIMULATIONS OF SERIES RESONANT CIRCUIT
 
Matlab introduction
Matlab introductionMatlab introduction
Matlab introduction
 
Matlab matrices and arrays
Matlab matrices and arraysMatlab matrices and arrays
Matlab matrices and arrays
 
Matlab ploting
Matlab plotingMatlab ploting
Matlab ploting
 
Matlab complex numbers
Matlab complex numbersMatlab complex numbers
Matlab complex numbers
 
The experiment of the series resonant circuit
The experiment of the series resonant circuitThe experiment of the series resonant circuit
The experiment of the series resonant circuit
 
Introto pl cs
Introto pl csIntroto pl cs
Introto pl cs
 
Microprocessor and Microcontroller lec4
Microprocessor and Microcontroller lec4Microprocessor and Microcontroller lec4
Microprocessor and Microcontroller lec4
 
Matlab dc circuit analysis
Matlab dc circuit analysisMatlab dc circuit analysis
Matlab dc circuit analysis
 
Matlab polynimials and curve fitting
Matlab polynimials and curve fittingMatlab polynimials and curve fitting
Matlab polynimials and curve fitting
 
Microprocessor and Microcontroller lec5
Microprocessor and Microcontroller lec5Microprocessor and Microcontroller lec5
Microprocessor and Microcontroller lec5
 
Resonant circuits
Resonant circuitsResonant circuits
Resonant circuits
 
Plc analog input output programming
Plc analog input output programmingPlc analog input output programming
Plc analog input output programming
 
Plc analog Tutorial
Plc analog TutorialPlc analog Tutorial
Plc analog Tutorial
 
Chapter 2 ladder
Chapter 2 ladderChapter 2 ladder
Chapter 2 ladder
 
Matlab dc motor modeling
Matlab dc motor modelingMatlab dc motor modeling
Matlab dc motor modeling
 
Matlab simpowersystem
Matlab simpowersystemMatlab simpowersystem
Matlab simpowersystem
 
Mat lab solving equations simulink
Mat lab solving equations simulinkMat lab solving equations simulink
Mat lab solving equations simulink
 
Matlab solving rlc circuit
Matlab solving rlc circuitMatlab solving rlc circuit
Matlab solving rlc circuit
 
PLC input and output devices
PLC input and output devices PLC input and output devices
PLC input and output devices
 

Similar to Matlab m files and scripts

Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013amanabr
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Kurmendra Singh
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01Wingston
 
Microcontroller lec 3
Microcontroller  lec 3Microcontroller  lec 3
Microcontroller lec 3Ibrahim Reda
 
Matlab Programming Tips Part 1
Matlab Programming Tips Part 1Matlab Programming Tips Part 1
Matlab Programming Tips Part 1Shameer Ahmed Koya
 
Matlab for diploma students(1)
Matlab for diploma students(1)Matlab for diploma students(1)
Matlab for diploma students(1)Retheesh Raj
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++msharshitha03s
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointJavaTpoint.Com
 
07-MIPS-Functions.pptx COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE
07-MIPS-Functions.pptx COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE07-MIPS-Functions.pptx COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE
07-MIPS-Functions.pptx COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGEMuhammadUmarAwanCSIT
 
Python programing
Python programingPython programing
Python programinghamzagame
 
Section-6-User-Defined-Functions.pdf
Section-6-User-Defined-Functions.pdfSection-6-User-Defined-Functions.pdf
Section-6-User-Defined-Functions.pdfjohnkyllelumacang699
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa Thapa
 
Advanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptAdvanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptMuhammad Sikandar Mustafa
 

Similar to Matlab m files and scripts (20)

Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
 
Matlab-3.pptx
Matlab-3.pptxMatlab-3.pptx
Matlab-3.pptx
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx
 
Microcontroller lec 3
Microcontroller  lec 3Microcontroller  lec 3
Microcontroller lec 3
 
Matlab Programming Tips Part 1
Matlab Programming Tips Part 1Matlab Programming Tips Part 1
Matlab Programming Tips Part 1
 
Matlab for diploma students(1)
Matlab for diploma students(1)Matlab for diploma students(1)
Matlab for diploma students(1)
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
 
07-MIPS-Functions.pptx COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE
07-MIPS-Functions.pptx COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE07-MIPS-Functions.pptx COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE
07-MIPS-Functions.pptx COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE
 
C notes for exam preparation
C notes for exam preparationC notes for exam preparation
C notes for exam preparation
 
Python programing
Python programingPython programing
Python programing
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programme
 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programme
 
Section-6-User-Defined-Functions.pdf
Section-6-User-Defined-Functions.pdfSection-6-User-Defined-Functions.pdf
Section-6-User-Defined-Functions.pdf
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptx
 
Advanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptAdvanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter ppt
 

More from Ameen San

Application of Capacitors to Distribution System and Voltage Regulation
Application of Capacitors to Distribution System and Voltage RegulationApplication of Capacitors to Distribution System and Voltage Regulation
Application of Capacitors to Distribution System and Voltage RegulationAmeen San
 
Distribution System Voltage Drop and Power Loss Calculation
Distribution System Voltage Drop and Power Loss CalculationDistribution System Voltage Drop and Power Loss Calculation
Distribution System Voltage Drop and Power Loss CalculationAmeen San
 
Load Characteristics
Load CharacteristicsLoad Characteristics
Load CharacteristicsAmeen San
 
ELECTRICAL DISTRIBUTION TECHNOLOGY
ELECTRICAL DISTRIBUTION TECHNOLOGYELECTRICAL DISTRIBUTION TECHNOLOGY
ELECTRICAL DISTRIBUTION TECHNOLOGYAmeen San
 
Stepper motor
Stepper motor Stepper motor
Stepper motor Ameen San
 
PLC application
PLC applicationPLC application
PLC applicationAmeen San
 
PLC arithmatic functions
PLC arithmatic functionsPLC arithmatic functions
PLC arithmatic functionsAmeen San
 
PLC data types and addressing
PLC data types and addressingPLC data types and addressing
PLC data types and addressingAmeen San
 
PLC Counters
PLC CountersPLC Counters
PLC CountersAmeen San
 
PLC Traffic Light Control
PLC Traffic Light ControlPLC Traffic Light Control
PLC Traffic Light ControlAmeen San
 
PLC Internal Relays
PLC Internal RelaysPLC Internal Relays
PLC Internal RelaysAmeen San
 
PLC Intro to programming
PLC Intro to programmingPLC Intro to programming
PLC Intro to programmingAmeen San
 
PLC Logic Circuits
PLC Logic CircuitsPLC Logic Circuits
PLC Logic CircuitsAmeen San
 
PLC Applications
PLC ApplicationsPLC Applications
PLC ApplicationsAmeen San
 
Protection Devices and the Lightning
Protection Devices and the LightningProtection Devices and the Lightning
Protection Devices and the LightningAmeen San
 
Circuit Breakers
Circuit BreakersCircuit Breakers
Circuit BreakersAmeen San
 
Engineering Economy
Engineering EconomyEngineering Economy
Engineering EconomyAmeen San
 

More from Ameen San (20)

Application of Capacitors to Distribution System and Voltage Regulation
Application of Capacitors to Distribution System and Voltage RegulationApplication of Capacitors to Distribution System and Voltage Regulation
Application of Capacitors to Distribution System and Voltage Regulation
 
Distribution System Voltage Drop and Power Loss Calculation
Distribution System Voltage Drop and Power Loss CalculationDistribution System Voltage Drop and Power Loss Calculation
Distribution System Voltage Drop and Power Loss Calculation
 
Load Characteristics
Load CharacteristicsLoad Characteristics
Load Characteristics
 
ELECTRICAL DISTRIBUTION TECHNOLOGY
ELECTRICAL DISTRIBUTION TECHNOLOGYELECTRICAL DISTRIBUTION TECHNOLOGY
ELECTRICAL DISTRIBUTION TECHNOLOGY
 
Stepper motor
Stepper motor Stepper motor
Stepper motor
 
PLC application
PLC applicationPLC application
PLC application
 
PLC arithmatic functions
PLC arithmatic functionsPLC arithmatic functions
PLC arithmatic functions
 
PLC data types and addressing
PLC data types and addressingPLC data types and addressing
PLC data types and addressing
 
PLC Counters
PLC CountersPLC Counters
PLC Counters
 
PLC Traffic Light Control
PLC Traffic Light ControlPLC Traffic Light Control
PLC Traffic Light Control
 
PLC Timers
PLC TimersPLC Timers
PLC Timers
 
PLC Internal Relays
PLC Internal RelaysPLC Internal Relays
PLC Internal Relays
 
PLC Intro to programming
PLC Intro to programmingPLC Intro to programming
PLC Intro to programming
 
PLC Logic Circuits
PLC Logic CircuitsPLC Logic Circuits
PLC Logic Circuits
 
PLC Applications
PLC ApplicationsPLC Applications
PLC Applications
 
Protection Devices and the Lightning
Protection Devices and the LightningProtection Devices and the Lightning
Protection Devices and the Lightning
 
Protection
ProtectionProtection
Protection
 
Relays
RelaysRelays
Relays
 
Circuit Breakers
Circuit BreakersCircuit Breakers
Circuit Breakers
 
Engineering Economy
Engineering EconomyEngineering Economy
Engineering Economy
 

Recently uploaded

CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture designssuser87fa0c1
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 

Recently uploaded (20)

CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture design
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 

Matlab m files and scripts

  • 1. 11 M File Script and Function Lecture Series – 5 by Shameer Koya
  • 2. Files used in MATLAB  .m files  Both functions and scripts are stored in .m files  .mat files  The MATLAB workspace (or specific variables) can be saved in .mat files  These files are easy to save and load, and MATLAB accessing them is very efficient  .fig files (next week)  Plots can be saved in .fig files, and then the figure can be edited without reloading data 2
  • 4. .m files  Code can be saved in .m files and run in the command window – exact implementation depends on whether the code is a function or a script 4
  • 5. Script  Simplest kind of m-file  Type up a bunch of commands and save as filename.m  Type filename in command window to run  Example: first_program.m 5
  • 6. Function  Functions are more complex than scripts  Functions have their own local variables  Functions return output as specified, and can accept input as specified 6
  • 7. Commenting  Comment your code!  Any line starting with % is a comment  Comments can be added to the end of existing lines by adding a %  Note that anything after % will be ignored  In editor screen comments are green  Any comments written at the beginning of an m- file will be displayed by the command help filename 7
  • 8. Flow control  Conditional control – if, else, switch  Loop control – for, while, continue, break  Program termination – return 8
  • 9. Conditional control – if, else, elseif if test statement statements elseif test statement statements else statements end Note that ==,~=,>,< are all scalar tests. 9 if I == J A(I,J) = 2; elseif abs(I-J) == 1 A(I,J) = -1; else A(I,J) = 0; end
  • 10. Loop control – for, while for varname = min:max statements end while condition is true statements end 10 N=10; for I = 1:N for J = 1:N A(I,J) = 1/(I+J-1); end end I=1; N=10; while I<=N J=1; while J<=N A(I,J)=1/(I+J-1); J=J+1; end I=I+1; end
  • 11. CS 111 11 MATLAB Examples  Find the number of positive numbers in a vector x = input( 'Enter a vector: ' ); count = 0; for ii = 1:length(x), if ( x(ii) > 0 ), count = count + 1; end end fprintf('Number of positive numbers is %dn', count);
  • 12. MATLAB Examples  Program to find grade from the mark  s=input('Enter the Mark: '); % enter the mark  if s>= 90  disp ('Grade: A');  elseif s>=80  disp ('Grade: B');  elseif s>=70  disp ('Grade: C');  elseif s>=60  disp ('Grade: D');  else  disp ('Grade: F');  end 12
  • 13. 13 MATLAB Examples Plot the switching response of a given RC circuit  Where Vci is the initial capacitor voltage; Vcf is the voltage the capacitor will reach if it charges for an infinite amount of time.          msforteVVV mstforV tV RCtt cfcicf ci c 5.1)( 5.10 /)( 0
  • 14. 14 MATLAB Examples  Vci = input('Enter initial capacitor voltage, Vci: ');  Vcf = input('Enter Final capacitor voltage, Vcf: ');  R = input('Enter Resistance value, R: ');  C = input('Enter Capacitance value, C: ');  t0 = input('Enter Switching time, t0: ');  tf = input('Enter Simulation end time, tf: ');  t=linspace(0,tf,1000);  Vc=zeros(1,1000);  for i=1:1000  if t(i)<t0  Vc(i)=Vci;  else Vc(i)=Vcf+(Vci-Vcf)*exp(-(t(i)-t0)/(R*C));  end  end  plot(t*1000,Vc);  title('RC Step Response')  ylabel('Capacitor voltage')  xlabel('time in msec')  grid on Plot the switching response of a given RC circuit
  • 15. 15 MATLAB Examples  Find the index of the largest number in a vector x = input( 'Enter a vector: ' ); max_value = x(1); max_index = 1; for ii = 2:length(x), if ( x(ii) > max_value ), max_value = x(ii); max_index = ii; end end fprintf( 'Max value is %dn', max_value ); fprintf( 'Its index is %dn', max_index );  What if the max value occurs more than once?
  • 16.  Write program to find step response of RL circuit  Program to make p w m signals 16