You are on page 1of 12

Adama Science and Technology University

School of Electrical Engineering and Computing


Department of Electronics and Communication

Course Name: Engineering Application Software


Course code ECE 2208
Tewodros Endale
2021
Outline

Chapter -2
Visualization and Programming

• Functions and Scripts


• Flow Control
• Line Plots
• Image/Surface Plots
• Efficient Codes
• Debugging

04/03/2023 Chapter One 2


Scripts
• Collection of commands executed in sequence
• do not accept inputs and do not return any outputs.
• They operate on data in the workspace.
• Written in MATLAB editor
• Saved as m-file (.m extension)

04/03/2023 Chapter One 3


Cont’d

• After creating and saving the file, you can • NOTE


run it in two ways: • Anything following % sign is
• Clicking the Run button on the editor interpreted as a comment
window or • The first contiguous comment becomes
• Just typing the filename (without help file
extension) in the command prompt
• The command window prompt displays the
result

04/03/2023 Chapter One 4


Functions
Function
• group of statements that together perform a declaration Input
task Must have the
reserved word:
• Note function
• No need for return: MATLAB 'returns' the
function [X, y, z] = funNAME(in1,in2)
variables whose names match those in the
function declaration (though, you can use to % your comment
break and go back to invoking function) % your comment
• Variable scope: Any variable created within
the function but not returned disappears Help file Function
after the function stops running (They’re Output name should
called “local variables”) If more than one match m-file
output, must be name
in brackets

04/03/2023 Chapter One 5


Cont’d

[ ]
1 1 1 1 ¨ 1 1
¨
𝑦 (𝑡 )+ + + ˙
𝑦 (𝑡 )+ 𝑦 (𝑡)= 𝑥 (𝑡 )
𝐶 2 𝑅1 𝑅 2 𝑅 3 𝑅1 𝑅2 𝐶1 𝐶2 𝑅1 𝑅 3 𝐶 1 𝐶 2

the characteristic equation is given by

λ  +
21 1
+
1
+
[
𝐶 2 𝑅 1 𝑅2 𝑅3

λ  +
1
𝑅1 𝑅2 𝐶1 𝐶2]=0

Operation-amplifier circuit

04/03/2023 Chapter One 6


Cont’d

• first case, assign nominal component values of R1 = R2 = R3 • Unlike script M-files, function M-files can accept input
= 10 kΩ and C1 = C2 = 1μF. arguments as well as return outputs
• function [lambda] = CH2MP2(R,C)

% CH2MP1.m : Chapter 2, MATLAB Program 1 % CH2MP2.m :

% Script M-file determines characteristic % Function M-file finds characteristic roots


of op-amp circuit.
roots of op-amp circuit.
% INPUTS: R = length-3 vector of resistances
% Set component values:
% C = length-2 vector of capacitances
R = [10e3, 10e3, 10e3]; C = [10e-7, 1e-6];
% OUTPUTS: lambda = characteristic roots
• Determine coefficients for
characteristic equation: % Determine coefficients for characteristic
equation:
A = [1, (1/R(1)+1/R(2)+1/R(3))/C(2),
1/(R(1)*R(2)*C(1)*C(2))]; A= [1, (1/R(1)+1/R(2)+1/R(3))/C(2),
1/(R(1)*R(2)*C(1)*C(2))];
% Determine characteristic roots:
% Determine characteristic roots:
lambda = roots(A); lambda = roots(A);

04/03/2023 Chapter One 7


Flow Control
Relational Operators Logical Operators
Relation Operator Syntax Equivalent Function M Function
Relation Operator Syntax
Less than A<B lt(A,B) Equivalent
Less than or equal A<=B le(A,B) Logical And A&B and(A,B)
to Logical Or A|B or(A,B)
Greater than or A>=B ge(A,B) Logical Xor A xor B xor(A,B)
equal to Logical And (short A&&B N/A
Greater than A>B gt(A,B) circuiting)
Equal A==B eq(A,B)
Logical Or (short A||B N/A
Not equal A~=B ne(A,B) circuiting)

Element ~A not(A)
complement

Boolean values: zero is false, nonzero is true

04/03/2023 Chapter One 8


Cont’d
if/else/elseif
Basic flow-control, common to all languages

IF ELSE ELSEIF
if cond if cond if cond1
commands commands1 commands1
end else elseif cond2
commands2 commands2
end else
q commands3
Conditional
statement: end
evaluates to true
or false

terminates the
No need for parentheses: command provide for the
last group of .
blocks are between reserved words statements execution of alternate
04/03/2023 Chapter One groups of statements 9
Cont’d
Switch/For/While
Basic flow-control, common to all languages

switch for While


switch_expression for values while expression
case statements statements
case_expression Execute one of end end
statements several groups
case of statements
case_expression
statements executes a group
... of statements in a
otherwise loop for a
statements specified number Repeat when
end of times condition is true
q
.

04/03/2023 Chapter One 10


Example
s = 10;
% grading system using IF H = zeros(s);
n = input('Enter a number: ');
if n>=90 for c = 1:s
grade = 'A'; for r = 1:s
elseif n >= 80 H(r,c) =
% switch and case
grade ='B'; 1/(r+c-1);
elseif n >= 70 n = input('Enter a number: ');
end
grade ='C'; end
switch n
elseif n >= 60 case -1
grade ='D'; disp('negative one’) n = 10;
elseif n <=59 case 0 f = n;
grade = ‘F'; disp('zero') while n > 1
end case 1 n = n-1;
disp('positive one') f = f*n;
otherwise end
disp('other value') disp(['n! = '
end num2str(f)])
04/03/2023 Chapter One 11
Thank you

04/03/2023 Chapter One 12

You might also like