0% found this document useful (0 votes)
7 views9 pages

MATLAB Control Flow in EEE 1106

Uploaded by

Rayhan Miya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

MATLAB Control Flow in EEE 1106

Uploaded by

Rayhan Miya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

DHAKA UNIVERSITY OF ENGINEERING

& TECHNOLOGY (DUET), GAZIPUR


DEPARTMENT OF ELECTRICAL AND ELECTRONIC
ENGINEERING

EEE 1106: Circuit Simulation Sessional

Experiment No. 7: Study of Control Flow and Loop Operation in MATLAB

Name: Rayhan Miah

Student No: 2302043

1st Year 2nd Semester


Section: A

Date of Performance:26-05-2025

Date of Submission: 23-06-2025

_________________________
Signature
Experiment 07 2302043

Objectives
The objectives of this experiments are

(i) To understand the operation of the Loop Function.

(ii) To learn about Control Flow Structures.

Introduction
In MATLAB, we can make our own functions. These are called user-defined functions.
Each function should be saved in a file and the file name must be the same as the function name.
This helps MATLAB understand which function to run.

Report Problems
Problem 01: Write a MATLAB code in M-File to determine the factorial of a positive integer number
using a for loop.
Solution:
clc;
clear all;
close all;
n = input('enter a integer num : ');
if n >= 0 && n == floor(n)
factorial_calculate = 1;
for i = 1:n
factorial_calculate = factorial_calculate * i;
end
fprintf('factorial = %d is %d\n', n, factorial_calculate)
else
fprintf('invalid number\n')
end
MATLAB result:

enter a integer num : 5


factorial = 5 is 120

Page 1 of 10
Experiment 07 2302043

Report Problems
Problem 02: Using a while loop, determine the Greater Common Divisor (GCD) of two
integer numbers.
clc;

clear;

close all;

a = input('Enter the first positive integer: ');

b = input('Enter the second positive integer: ');

if a > 0 && b > 0 && mod(a,1) == 0 && mod(b,1) == 0

min_num = min(a, b);

i = min_num;

while i >= 1

if mod(a, i) == 0 && mod(b, i) == 0

gcd = i;

break;

end

i = i - 1;

end

fprintf('GCD of %d and %d is = %d\n', a, b, gcd);

else

fprintf('Invalid input! Please enter two positive integers.\n');

end

MATLAB result :

enter the first positive integer: 30

enter the second positive integer: 40

GCD is10>>

Page 2 of 10
Experiment 07 2302043

Page 3 of 10
Experiment 07 2302043

Class Work Problems


Problem 01: Problem Statement
Solution:

clc;clear all;

close all;
L = 10*10^-3;
R = [5;10;20];
C = [0.1:0.2:1]*10^-6;
for i = 1:length(R)
for j = 1:length(C)
f0 = 1./(2*pi*sqrt(L*C(j)));
XL = 2*pi*f0*L;
Q0 = XL/R(i);
if Q0>5
fprintf('The Q-factor for R = %f ohm, L = %f mH , C = %f mF when Q-factor is greater than
5 is %f\n',R(i),L*1000,C(j)*1000000,Q0)
end
end
end
MATLAB Result
The Q-factor for R = 5.000000 ohm, L = 10.000000 mH , C = 0.100000 mF when Q-factor is greater than 5 is 63.245553

The Q-factor for R = 5.000000 ohm, L = 10.000000 mH , C = 0.300000 mF when Q-factor is greater than 5 is 36.514837

The Q-factor for R = 5.000000 ohm, L = 10.000000 mH , C = 0.500000 mF when Q-factor is greater than 5 is 28.284271

The Q-factor for R = 5.000000 ohm, L = 10.000000 mH , C = 0.700000 mF when Q-factor is greater than 5 is 23.904572

The Q-factor for R = 5.000000 ohm, L = 10.000000 mH , C = 0.900000 mF when Q-factor is greater than 5 is 21.081851

The Q-factor for R = 10.000000 ohm, L = 10.000000 mH , C = 0.100000 mF when Q-factor is greater than 5 is 31.622777

The Q-factor for R = 10.000000 ohm, L = 10.000000 mH , C = 0.300000 mF when Q-factor is greater than 5 is 18.257419

The Q-factor for R = 10.000000 ohm, L = 10.000000 mH , C = 0.500000 mF when Q-factor is greater than 5 is 14.142136

The Q-factor for R = 10.000000 ohm, L = 10.000000 mH , C = 0.700000 mF when Q-factor is greater than 5 is 11.952286

The Q-factor for R = 10.000000 ohm, L = 10.000000 mH , C = 0.900000 mF when Q-factor is greater than 5 is 10.540926

The Q-factor for R = 20.000000 ohm, L = 10.000000 mH , C = 0.100000 mF when Q-factor is greater than 5 is 15.811388

The Q-factor for R = 20.000000 ohm, L = 10.000000 mH , C = 0.300000 mF when Q-factor is greater than 5 is 9.128709

The Q-factor for R = 20.000000 ohm, L = 10.000000 mH , C = 0.500000 mF when Q-factor is greater than 5 is 7.071068

The Q-factor for R = 20.000000 ohm, L = 10.000000 mH , C = 0.700000 mF when Q-factor is greater than 5 is 5.976143

The Q-factor for R = 20.000000 ohm, L = 10.000000 mH , C = 0.900000 mF when Q-factor is greater than 5 is 5.270463

Page 4 of 10
Experiment 07 2302043

Class Work Problems


Problem 02: : Simulate the charging of a capacitor in a series RC circuit when a DC voltage is
suddenly applied. The capacitor charges gradually to the supply voltage.
Solution:
clc;
clear all;
close all;
Vin=5;
R=1000 ;
C=100*10^-6;
tao=R*C;
t=0:tao/5:5*tao;
Vc = zeros(1,length(t));
for i=1:length(t)
Vc(i) = Vin*( 1-exp(-t(i)/tao ));
fprintf('Capacitor Voltage at %.3f s is %.4f V.\n',t(i),Vc(i));
if V(i) >= 0.99*Vin
break;
end
end

MATLAB result:
Capacitor Voltage at 0.000 s is 0.0000 V.

Capacitor Voltage at 0.020 s is 0.9063 V.

Capacitor Voltage at 0.040 s is 1.6484 V.

Capacitor Voltage at 0.060 s is 2.2559 V.

Capacitor Voltage at 0.080 s is 2.7534 V.

Capacitor Voltage at 0.100 s is 3.1606 V.

Capacitor Voltage at 0.120 s is 3.4940 V.

Capacitor Voltage at 0.140 s is 3.7670 V.

Capacitor Voltage at 0.160 s is 3.9905 V.

Capacitor Voltage at 0.180 s is 4.1735 V.

Capacitor Voltage at 0.200 s is 4.3233 V.

Capacitor Voltage at 0.220 s is 4.4460 V.

Capacitor Voltage at 0.240 s is 4.5464 V.

Capacitor Voltage at 0.260 s is 4.6286 V.

Capacitor Voltage at 0.280 s is 4.6959 V.

Capacitor Voltage at 0.300 s is 4.7511 V.

Capacitor Voltage at 0.320 s is 4.7962 V.

Capacitor Voltage at 0.340 s is 4.8331 V.

Page 5 of 10
Experiment 07 2302043

Capacitor Voltage at 0.360 s is 4.8634 V.

Capacitor Voltage at 0.380 s is 4.8881 V.

Capacitor Voltage at 0.400 s is 4.9084 V.

Capacitor Voltage at 0.420 s is 4.9250 V.

Capacitor Voltage at 0.440 s is 4.9386 V.

Capacitor Voltage at 0.460 s is 4.9497 V.

Capacitor Voltage at 0.480 s is 4.9589 V.

Class Work Problems


Problem 03:A separately excited DC generator has its field winding powered by an external DC
supply. When the field is energized, the magnetic field builds up gradually due to the inductance of
the field coil. As a result, the output voltage of the generator does not instantly reach its maximum
value; instead, it follows an exponential rise toward a steady-state value.
clc;
close all;
clear all;
V_infinity = 240;
tao = 1.5;
t = 0;
dt = 0.1;
V= V_infinity*(1 - exp(-t/tao));
t= t + dt;
V_next =V_infinity*(1 - exp(-t/tao));
while (V_next - V) > 0.01
V = V_next;
t = t + dt;
V_next = V_infinity*(1 - exp(-t/tao));
end
fprintf('Voltage build-up stabilizes at %.2f V after %.1f seconds.\n',V,t)

Answer:

Voltage build-up stabilizes at 239.85 V after 11.2 seconds.

Page 6 of 10
Experiment 07 2302043

Class Work Problems

Problem: Develop a MATLAB program to compute the sum of the given series up to the N term, th

using either a for loop or a while loop, where N is a positive integer.

1 + (1² + 2²) + (1 + 2 + 3) + (1² + 2² + 3² + 4²) + (1 + 2 + 3 + 4 + 5) + (1² + 2² + 3² + 4² + 5² + 6²)


+…

Solution:

clc;
clear;
close all
N = input('Enter the number of terms (N): ');
if N > 0 && mod(N,1) == 0
total_sum = 0;
i = 1;
while i <= N
if mod(i,2) == 1
term_sum = sum(1:i);
else
term_sum = sum((1:i).^2);
end
total_sum = total_sum + term_sum;
i = i + 1;
end
fprintf('The total sum of the series up to %d terms is: %d\n', N, total_sum);
else
fprintf('Invalid input! Please enter a positive integer.\n');
end

MATLAB result:

Enter the number of terms (N): 10

The total sum of the series up to 10 terms is: 810

Discussion:

The programs were of different types and included many new things to learn. At first, the
programs were difficult to understand, but later they became easier.
Several plots were shown during the work. These include:
transformer efficiency curve,annual cost vs conductor area curve ,energy stored vs coupling curve
these plots helped to understand the topics more clearly.

Page 7 of 10
Experiment 07 2302043

Page 8 of 10

You might also like