Institute Technology of Cambodia
Electrical and Energy Department Modern Control Systems (13GEE)
Homework
Name: PHOU Sros
ID:e20210251(I3-GEE-B2)
Exercise 1: Plot the roots of the polynomial 𝑝(𝑥) = 𝑥 3 + 2𝑥 2 + 3𝑥 + 𝑘, for the values of k:
0,0.5,1,1.5,...,10 in the same graphic window, using the marker ”*”. Use the Matlab functions: for,
roots, real, imag, plot, hold on, pause.
Answer
Using the Matlab functions: for, roots, real, imag, plot, hold on, pause, to solve the exercise above
we obtain as below.
Coding:
close all
clear
clc;
figure;
hold on;
for k=0:0.5:10 % defined the range of k's values
polynomial=[1 2 3 k]; %defined the polynomial
r= roots(polynomial);
%plot real part and imaginary part of roots polynomial
plot(real(r),imag(r), '*');
pause(0.5); % pause to see each plot
end
xlabel('Real part');
ylabel('imaginary part');
title('Roots of p(x) = x^3+2x^2+3x+k for different values of k');
grid on;
hold off;
pg. 1
Institute Technology of Cambodia
Electrical and Energy Department Modern Control Systems (13GEE)
Resulting of the code are given as below:
Exercise 2: Write a function plotfreq(n) that will display the following functions on the same
plot, over a time interval t = [0, 10].
𝑓1 (𝑡) = 𝑒 −𝑡 , 𝑓2 (𝑡) = 𝑠𝑖𝑛(2𝜋𝑡), 𝑓3 (𝑡) = 𝑓1 (𝑡). 𝑓2 (𝑡)
The function input argument n is the frequency of the sine function (f2). Call the function for
various values of n ∈ [0.5, 5].
Answer
Using MATLAB to write a function plotfreq(n).
Coding:
function plotfreq(n)
t=0:0.1:10; % over a time interval between 0 and 10
f_1=exp(-t);
f_2=sin(2*pi*t);
f_3=f_1.*f_2;
figure;
hold on;
pg. 2
Institute Technology of Cambodia
Electrical and Energy Department Modern Control Systems (13GEE)
% place all three line plots in the same figure window
plot(t,f_1,'r','LineWidth',1.5); %plot f1
plot(t,f_2,'g','LineWidth',1.5);%plot f2
plot(t,f_3,'b','LineWidth',1.5);%plot f3
xlabel('Time (t)');
ylabel('Function value');
title(['functions f1(t), f2(t),and f3(t) for n=', num2str(n)]);
legend('f1(t) = e^{-t}', ['f2(t) = sin(2\pi', num2str(n), 't)'], 'f3(t) = f1(t)
\cdot f2(t)');
grid on;
end
Call the function for various values of n ∈ [0.5, 5].
After that we can calling the function plotfreq(n) as below:
• In case 𝑛 = 0.5 we obtain :
%call the plotfreq function with different values of n within the range [0.5]
plotfreq(0.5)
Resulting show as below:
pg. 3
Institute Technology of Cambodia
Electrical and Energy Department Modern Control Systems (13GEE)
• In case 𝑛 = 5 we obtain :
%call the plotfreq function with different values of n within the range[5]
plotfreq(5)
Exercise 3: Write a function that generates the first n Fibonacci numbers. The function will be
called: fibonacci(n) and will return the vector: 1, 1, 2, 3, 5, ..., N (n numbers).
𝐹1 = 1, 𝐹2 = 1, … , 𝐹𝑘 = 𝐹𝑘−1 + 𝐹𝑘−2
Answer
Write a function that generates the first n Fibonacci numbers given above.
Using MATLAB to solve we obtain code as below:
function fib = fibonacci(~)
n=20;
fib = zeros(1,n); % Initialize a vector to store the Fibonacci numbers
fib(1) = 1; % Set the first Fibonacci number to 1
fib(2) = 1; % Set the second Fibonacci number to 1
for k = 3:n
pg. 4
Institute Technology of Cambodia
Electrical and Energy Department Modern Control Systems (13GEE)
fib(k) = fib(k-1) + fib(k-2); % Calculate the next Fibonacci number based
on the previous two numbers
end
end
Resulting of the code is shown below:
>> fibonacci
ans =
Columns 1 through 7
1 1 2 3 5 8
13
Columns 8 through 14
21 34 55 89 144 233
377
Columns 15 through 20
610 987 1597 2584 4181 6765
Exercise 4: Draw a fern fractal in Matlap, [1]. Use the following algorithm:
Answer:
Draw a fern fractal in Matlap, Use the following algorithm:
Reference:
[1] Michael Barnsley. Fractals Everywhere. Academic Press, 1993.
Coding :
function drawBarnsleyFernFractal1(iterations)
if nargin < 1
iterations = 60000; % Default number of iterations
end
x = 0;
y = 0;
figure;
hold on;
for i = 1:iterations
r = rand();
if r < 0.01
newX = 0;
pg. 5
Institute Technology of Cambodia
Electrical and Energy Department Modern Control Systems (13GEE)
newY = 0.16 * y;
elseif r < 0.86
newX = 0.85 * x + 0.04 * y;
newY = -0.04 * x + 0.85 * y + 1.6;
elseif r < 0.93
newX = 0.2 * x - 0.26 * y;
newY = 0.23 * x + 0.22 * y + 1.6;
else
newX = -0.15 * x + 0.28 * y;
newY = 0.26 * x + 0.24 * y + 0.44;
end
plot(newX, newY, '.', 'MarkerSize', 1);
x = newX;
y = newY;
end
title('Barnsley Fern Fractal');
axis off;
hold off;
end
After running the code we receive result as below:
pg. 6