Lab_01 CA
Lab_01 CA
LABORATORY MANUAL
CIRCUIT ANALYSIS
(ELE-201)
Semester Spring 2024
Student Name
Student ID
Batch
Lab Instructor:
Engr. Amna Sheikh
Index
S. No. Date Objective Sign
Lab 1
Lab 2
Lab 3
Lab 4
Lab 5
Lab 6
Lab 7
Lab 8
Lab 9
Lab 10
Lab 11
Lab 12
Lab 13
Lab 14
Lab 15
EXPERIMENT 01
OBJECTIVE
To introduce students to MATLAB software and Multisim and familiarize them with its
functions for circuit analysis.
EQUIPMENTS
THEORY
MATLAB is a high-performance language for technical computing. It integrates computation,
visualization and programming environment. Furthermore, MATLAB is a modern programming
language environment: it has sophisticated data structures, contains built in editing and
debugging tools, and supports object-oriented programming.
It has powerful built in routines that enables a very wide variety of computations. It also has easy
to use graphic commands that make the visualization of results immediately available. Specific
applications are collected in packages referred to as toolbox. There are toolboxes for signal
processing, symbolic computation, control theory, simulation, optimization, and several other
fields of applied science and engineering.
This lab is intended to make you familiar with MATLAB
Starting MATLAB
When you start MATLAB, a special window called MATLAB desktop appears. The desktop is a
window that contains other windows. The major tools within or accessible from the desktop are:
Current folder
Command window
Command history
Workspace
Help browser
The start screen looks like the one that is shown in Figure 1.
Notice that if you do not specify an output variable, MATLAB uses a default variable answer is
created (or overwritten, if it is already existed). To avoid this, you may assign a value to a
variable or output argument name. For example,
>> x= 1+2*3
x=
7
Will result in x being given the value 1 + 2 x 3 = 7. This variable name can always be used to
refer to the result of the previous computations. Therefore, computing 4x will result in:
>> 4*x
ans = 28.0000
Quitting MATLAB
To end your MATLAB session, type quit in the Command Window, or select File --> Exit
MATLAB in the desktop main menu.
Creating MATLAB Variables
MATLAB variables are with an assignment statement. The syntax of variable assignment is
Variable name = a value (or an expression)
For example
>> x = expression
Where expression is combination of numerical values, mathematical operators, variables.
● Manual entry
● Built in functions
● User defined functions
Overwriting Variable
Once a variable has been created, it can be reassigned. In addition, if you do not wish to see the
intermediate results, you can suppress the numerical output by putting a semicolon (;) at the end
of the line. Then the sequence of commands looks like this:
>> t = 5;
>> t = t+1
t=6
Error Messages
If we enter an expression incorrectly, MATLAB will return an error message. For example in the
following, we left out the multiplication sign , * , in the following expression
>> x = 10;
>> 5x
5x
Error unexpected MATLAB expression.
Making Correction
To make corrections, we can of course retype the expression. But if the expression is lengthy, we
make more mistakes by typing a second time. A previously typed command can be recalled with
the up-arrow key. When the command is displayed at the command prompt, it can be modified if
needed and executed.
The command clear or clear all removes all variables from the workspace. This frees up system
memory. In order to display a list of the variables currently in the memory type.
>> who
While, who will give more details which include size, space allocation, and class of the
variables.
Here are few additional useful commands:
To clear the command window, type clc
To abort the MATLAB computation, type ctrl-c.
Getting Help
To view the online documentation, select MATLAB Help menu or MATLAB Help directly in
the Command Window. The preferred method is to use the Help Browser. The Help Browser can
be started by selecting the ? Icon from the desktop toolbar. On the other hand, information about
any command is available by typing.
Mathematical Function
MATLAB offers many predefined mathematical function for technical computing which
contains large set of mathematical function.
Table 3 Lists some commonly used functions, where variables x and y can be numbers, vectors
or matrices.
COMMAND FUNCTION COMMAND FUNCTION
cos (x) Cosine abs (x) Absolute value
sin (x) Sine sign (x) Signum function
tan (x) Tangent max (x) Maximum value
acos (x) Arc cosine min (x) Minimum value
asin (x) Arc sine ceil (x) Round toward + ∞
atan (x) Arc tangent floor (x) Round toward - ∞
exp (x) Exponential round (x) Round to nearest integer
sqrt (x) Square root rem (x) Remainder after division
log (x) Natural logarithm angle (x) Phase angle
log10 (x) Common logarithm conj (x) Complex conjugate
Table 3: Elementary functions.
In addition to the elementary functions, MATLAB includes a number of predefined constant
values. A list of the most common values is given Table 4.
Pi The π number, π = 3.14159…
ⅈ, j The imaginary unit ⅈ , √ −1
Inf The infinity, ∞
NaN Not a number
Table 4: Predefined constant values.
For example
As a first example, the value of the expression y=e−a sin ( x ) +10 √ y , for a=5 , x=2 and y=8 is
computed by
>> a = 5; x = 2; y = 8;
>> y = exp(-a)*sin(x)+10*sqrt(y)
y= 28.2904
Basic Plotting
The simple 2D plotting command include:
COMMAND FUNCTION
Plot Plot in linear coordinates as a continuous function
Stem Plot in linear coordinates as discrete samples
Loglog Logarithmic x and y axes
semilogy Linear y and logarithmic x axes
semilogx Linear x and logarithmic y axes
bar Bar graph
errorbar Error bar graph
Hist Histogram
polar Polar coordinates
For example, to plot the function sin (x) on the interval [0; 2π], we first create a vector of x
values ranging from 0 to 2π, then compute the sine of these values, and finally plot the result:
Matrix Operation
In MATLAB, a matrix is a two-dimensional array of numbers. Matrix operations involve
manipulating these arrays to perform mathematical operations.
MATLAB provides a variety of built-in functions for matrix operations, such as matrix addition,
multiplication, and inversion, as well as functions for solving systems of linear equations and
finding eigenvalues and eigenvectors.
Here's an example of how to perform matrix addition in MATLAB:
% define two matrices
A = [1 2 3; 4 5 6; 7 8 9];
B = [9 8 7; 6 5 4; 3 2 1];
% add the matrices
C = A + B;
% display the result
disp(C);
COMMAND FUNCTION
xlabel Labels x-axis
ylabel Labels y-axis
title Puts a title on the plot
grid Adds a grid to the plot
gtext Allows positioning of text with the mouse
Allows placing text at specified coordinates of
text
the plot
axis Allows changing the x and y axes
figure Create a figure for plotting
figure(n) Make figure number n the current figure
Allows multiple plots to be superimposed on
hold on
the same axes
hold off Release hold on current plot
close(n) Close figure number n
Create an a×b matrix of plots with c the
subplot(a,b,c)
current figure
orient Specify orientation of a figure
LAB TASK 1
Define the resistance and current values in the circuit (R=10 ohm & I = 0.5:0.1:1 A).
Calculate the voltage using the formula
Display the result.
Verify the result by checking that the voltage is equal to the product of resistance and
current.
Plot the voltage versus current relationship in a circuit.
Source Code
Code Explanation
Output
LAB TASK 2
Write a MATLAB script to calculate the current through a resistor and power in the circuit using
Ohm's Law and Power Law.
Instructions:
Define the resistance and voltage values in the circuit (R=100 ohm & V=10 volts).
Calculate the current and power using the formula
Display the result using “fprintf” function.
Source Code
Code Explanation
Output
LAB TASK 3
Find node voltages in the given network and write code in MATLAB for given network also
verify the result with calculations and Multisim simulation.
Calculations:
MATLAB Code:
Code Explanation:
Output:
Multisim Simulation:
Hardware Picture:
Calculations:
MATLAB Code:
Code Explanation:
Output:
Multisim Simulation: