This repository has been archived on 2025-12-15. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
2024-09-20 14:17:13 +03:00
..
2024-09-20 14:17:13 +03:00
2024-09-20 14:17:13 +03:00

Solutions

Task 1.1

a = (28.5*(3^3)-sqrt(1500))/((11^2)+3.73)
c = 23 * (-8+(sqrt(607)/3)) + (40/7 + (4.7)^2)^2
e = (24 + (4.5)^3)/((exp(1))^(4.4)-log10(12560))
g = cos(5*pi/6)*(sin(7*pi/8))^2 + (tan((pi/2)*log(8)))/(sqrt(7)+2)

Task 1.2

a = -9.3;
b = 1.85;
c = a/b;
d = 0.5*(c*b+2*a);

s1 = d - (a+b)/c + ((a+b)^2)/sqrt(abs(a*b*c))
 
s2 = log((c-d)*(b-a)) + (a+b+c+d)/(a-b-c-d)

Task 1.3

bmi_a1 = 17.8;
if bmi_a1 < 18.5
    a1 = -1;
elseif bmi_a1 > 24.9
    a1 = 1;
else a1 = 0
end

bmi_a2 = 24;
if bmi_a2 < 18.5
    a2 = -1;
elseif bmi_a2 > 24.9
    a2 = 1;
else a2 = 0
end

Task 1.4

result = 0;
for n = 4:97
    if rem(n,4) == 0
        disp(n)
        result = result + sqrt(n);
    end

Task 1.5

balance = 3000;
for n = 1:25
    balance = balance*(1.06);
end

Task 1.6

balance = 2000;
for n = 1:30
    if balance > 10000
        balance = balance*(1.09);
    else balance = balance*(1.07)
    end
end

Task 1.7

savings = 0;
years = -1;
while savings < 1000000
    savings = savings*(1.09) + 5000;
    years = years + 1;
end

Task 1.8

m = 0;
last_instalment = 0;
loan = 22000;
while loan > 0
    loan = loan*(1.005);
    loan = loan - 300;
    m = m + 1;
end
last_instalment = loan + 300;
% When the loan is 220000, Matlab runs into an infinite loop as the monthly payment cannot compensate for the extra loan generated by interest rate. The loan keeps growing bigger, and Gomez can never pay off his loan.

Task 2.1

a = [6, 8*3, 81, (exp(1))^(2.5), sqrt(65), sin(pi/3), 23.05];
b = [44, 9, log(51), 2^3, 0.1, 5*tand(25)];
c = 0:3:42;
d = (18:-4:2)';
e = linspace(3, -36, 14)';

Task 2.2

t = linspace(0, 2*pi, 100);
a = log(2 + t + t.^2);
b = (exp(1).^t).*(1 + cos(3.*t));
c = cos(t).^2 + sin(t).^2;

Task 2.3

R = 8.31;
n = 0.1;
T = (0:1:100)+273;
V = (500:10:2500)./(10^6);
P1 = (n*R*(50+273))./V;
P2 = (n*R.*T)/(1500/(10^6));

Task 2.4

x = [5 3 0 7 9 5 7 4 0 4 2 7];
a = x+16;
x(1:2:end) = x(1:2:end) + 3;
c = sqrt(x);
d = x.^2;

Task 2.5

A = [ 12  13  13  14  16  14  15  12  13  12];
H = [143 158 161 150 159 159 150 144 157 137];
W = [ 39  48  49  42  51  51  47  31  43  31];
mean_ahw = [mean(A); mean(H); mean(W)];
n150 = length(H(H>150));
mw13 = mean(W(A<=13));
ntall = length(A(H>mean_ahw(2,1) & W<mean_ahw(3,1)));
if A(H>=max(H)) == max(A)
    age_height_check = true;
else
    age_height_check = false;
end

Task 2.6

A = [ 12  13  13  14  16  14  15  12  13  12];
H = [143 158 161 150 159 159 150 144 157 137];
W = [ 39  48  49  42  51  51  47  31  43  31];
D = [A; H; W]';
mean_ahw = [mean(D(:,1)); mean(D(:,2)); mean(D(:,3))];
n150 = length(D((D(:,2)>150)));
mw13 = mean(D(D(:,1)<=13,3));
ntall = length(D((D(:,2)>mean_ahw(2,1)) & (D(:,3)<mean_ahw(3,1)),1));
if D(((D(:,2)>=max(D(:,2)))),1) == max(D(:,1))
    age_height_check = true;
else
    age_height_check = false;
end

Task 2.7

x = [5 3 8 6 9 1 4 7];
MyMax = x(1);
MyMin = x(1);
for i = x(2:end)
    if i>MyMax
        MyMax = i;
    elseif i<MyMin
        MyMin = i;
    end
end

Task 2.8

a = sum(1:1:1000);
bb = 501:1:1000;
b = sum(bb(2:2:end)) + sum(bb(1:2:end).^2);
cc = 1:2:1000;
c = sum(cc(1:2:end).^2) + sum((cc(2:2:end).^2).*-1);

Task 3.1

x = linspace(1.5,3.5,200);
y = (-1/3).*(x-2).*(x-2.5).*(x/2-2.75).*(x/5-3.25).*(x-4) - 1.45;
z = [1 1.5 2 1.5 1];
t = [3 5 3 1 3];
figure;
plot(x,y,'g');
hold on
plot(z,t,'r');
hold off
xlim([0 4]);
ylim([-2 6]);

Task 3.2

load Data_AHW_big.mat
G = D(D(:,1)<=1,:);
B = D(D(:,1)>=2,:);
Ngirls = length(G);
Nboys = length(B);
N = [Ngirls Nboys];
Mgirls = [mean(G(:,2)) mean(G(:,3)) mean(G(:,4))];
Mboys = [mean(B(:,2)) mean(B(:,3)) mean(B(:,4))];
M = [Mgirls; Mboys];
if Mgirls(2) < Mboys(2)
    compH = true;
else 
    compH = false;
end
if Mgirls(3) < Mboys(3)
    compW = true;
else
    compW = false;
end
GirlsH = [];
GirlsW = [];
BoysH = [];
BoysW = [];
for x = 11:17
    GirlsH = [GirlsH; mean(G(G(:,2)>=x & G(:,2)<(x+1),3))];
    GirlsW = [GirlsW; mean(G(G(:,2)>=x & G(:,2)<(x+1),4))];
    BoysH = [BoysH; mean(B(B(:,2)>=x & B(:,2)<(x+1),3))];
    BoysW = [BoysW; mean(B(B(:,2)>=x & B(:,2)<(x+1),4))];
end
CompAgeH = [];
CompAgeW = [];
for i = 1:length(GirlsH)
    if GirlsH(i,1) < BoysH(i,1)
        CompAgeH = logical([CompAgeH; true]);
    else
        CompAgeH = logical([CompAgeH; false]);
    end
    if GirlsW(i,1) < BoysW(i,1)
        CompAgeW = logical([CompAgeW; true]);
    else
        CompAgeW = logical([CompAgeW; false]);
    end
end

Task 3.3

load Data_AHW_big.mat
G = D(D(:,1)<=1,:);
B = D(D(:,1)>=2,:);
figure 
axis([min(D(:,3)) max(D(:,3)) min(D(:,4)) max(D(:,4))])
subplot(1,2,1)
scatter(G(:,3), G(:,4), "b.")
xlabel('height')
ylabel('weght')
title('Girls')
subplot(1,2,2)
scatter(B(:,3), B(:,4), "b.")
xlabel('height')
ylabel('weght')
title('Boys')
figure
scatter(G(:,3), G(:,4), "m*"); hold on
scatter(B(:,3), B(:,4), "bo")
axis([min(D(:,3)) max(D(:,3)) min(D(:,4)) max(D(:,4))])
title('Girls and boys')
legend('Girls','Boys')
GirlsH = [];
GirlsW = [];
BoysH = [];
BoysW = [];
for x = 11:17
    GirlsH = [GirlsH; mean(G(G(:,2)>=x & G(:,2)<(x+1),3))];
    GirlsW = [GirlsW; mean(G(G(:,2)>=x & G(:,2)<(x+1),4))];
    BoysH = [BoysH; mean(B(B(:,2)>=x & B(:,2)<(x+1),3))];
    BoysW = [BoysW; mean(B(B(:,2)>=x & B(:,2)<(x+1),4))];
end
age = 11:1:17;
figure
tiledlayout(2,1);
nexttile
plot(age, GirlsH, 'm-o'); hold on
plot(age, BoysH, 'b-s'); hold off
legend('Girls','Boys')
xlabel('Age')
ylabel('Height')
axis tight
nexttile
plot(age, GirlsW, 'm-o'); hold on
plot(age, BoysW, 'b-s'); hold off
legend('Girls','Boys')
xlabel('Age')
ylabel('Weight')
axis tight

Task 3.4

data = [ 75  67  43  56  78  49  66  72 120
        164 168 152 169 170 157 167 181 170];
figure
grid on
hold on
axis tight
for i = 1:length(data)
    bmi = data(1,i)/((data(2,i)/100)^2);
    if bmi>= 18.5 && bmi<=24.9
        plot(data(2,i), data(1,i), "g*"); 
    elseif bmi<18.5
        plot(data(2,i), data(1,i), "b*"); 
    else
        plot(data(2,i), data(1,i), "r*"); 
    end
end
hold off
xlabel('Height [m]')
ylabel('Weight [kg]')

Task 3.5

R = 8.31;
n = 0.1;
T2 = (0:1:100)+273;
V1 = (500:10:2500)./(10^6);
P1 = (n*R*(50+273))./V1;
P2 = (n*R.*T2)/(1500/(10^6));
figure
subplot(2,1,1)
plot(V1, P1, 'b');
xlabel('Volume [m^3]')
ylabel('Pressure [Pa]')
axis tight
grid on
subplot(2,1,2)
plot(T2, P2, 'b');
xlabel('Temperature [K]')
ylabel('Pressure [Pa]')
axis tight
grid on

Task 3.6

x = linspace(-2,2,50);
y = sin(2.*x).*exp(x-4);
a = randi(50,1,10);
figure
axis tight
plot(x,y, "k"); hold on
for i = 1:10
    plot(x(a(i)),y(a(i)),'r.')
end
grid on
title('Random samples')
xlabel('x')
ylabel('y')
legend('y = sin(2x)exp(x-4)','Randomly sampled points')
legend('Location','south')

Task 4.1

v = [10 6 -9 4 0 2 0 -1 3 0 0 -4 -6 2];
nP = 0;
nN = 0;
nZ = 0;
for i = v
    if i > 0
        nP = nP + 1;
    elseif i < 0
        nN = nN + 1;
    else
        nZ = nZ + 1;
    end
end

Task 4.2

v = [10 6 -9 4 0 2 0 -1 3 0 0 -4 -6 2];
nP = sum([double(v(1:end) > 0)]);
nN = sum([double(v(1:end) < 0)]);
nZ = sum([double(v(1:end) == 0)]);

Task 4.3

balance = 2000;
balanceV = [balance];
years = 0:1:30;
for n = 1:30
    if balance > 10000
        balance = balance*(1.09);
    else 
        balance = balance*(1.07);
    end
    balanceV = [balanceV balance];
end
figure
plot(years, balanceV, "s-");
axis tight
xlabel("Years");
ylabel("Balance");

Task 4.4

m = 0;
loan = 22000;
loanV = loan;
instalmentV = 0;
while loan > 0
    loan = loan*(1.005);
    loan = loan - 300;
    instalmentV = [instalmentV 300];
    loanV = [loanV loan];
    m = m + 1;
end
months = 0:1:m;
last_instalment = loan + 300;
loanV(end) = 0;
instalmentV(end) = last_instalment;
figure 
bar(instalmentV, "r"); hold on
plot(months, loanV, "b.-")
axis tight
xlabel("months")
legend("instalments","loan balance")

Task 4.5

load WashingtonApril2002Temp.mat;
figure
h(1) = plot(T,"k-"); hold on
l = gobjects();
h(2) = yline(mean(T),"g",LineWidth=2);
for i = 1:length(T)
    if T(i) >= mean(T)*1.1
        h(3) = plot(i,T(i),"r*",MarkerSize=7);
    elseif T(i) <= mean(T)*0.9
        h(5) = plot(i,T(i),"b*",MarkerSize=7);
    elseif T(i)
        h(4) = plot(i,T(i),"g*",MarkerSize=7);
    end
    if T(i) == max(T)
        h(6) = plot(i,T(i),"ro",MarkerSize=10);
        line([i;i],[mean(T);max(T)],"Color","red","LineStyle",":","LineWidth",1);
        line([0;i],[max(T);max(T)],"Color","red","LineStyle",":","LineWidth",1);
    end
    if T(i) == min(T)
        h(7) = plot(i,T(i),"bo",MarkerSize=10);
        line([0;i],[min(T);min(T)],"Color","blue","LineStyle",":","LineWidth",1);
        line([i;i],[min(T);mean(T)],"Color","blue","LineStyle",":","LineWidth",1);
    end       
end
line()
h(9) = yline(mean(T)*0.9,"b--");
h(8) = yline(mean(T)*1.1,"r--");
axis tight
grid on
ylim([min(T)-5 max(T)+5]);
xlabel("days");
ylabel("temperatures");
legend(h([1 2 3 4 5 6 7 8 9]),"daily temperatures","average temperature","high temperature","moderate temperature","low temperature","maximal temperature","minimal temperature","high temperature limit","low temperature limit");

Task 5.1

function [months, balance] = savings(deposit, interest, goal)
    balance = 0;
    months = -1;
    while balance < goal
        balance = balance*(1+interest) + deposit;
        months = months + 1;
    end

Task 5.2

function distance = distanceCalculator(x,y)
    v = [];
    for i=1:length(x)
        v = [v (x(i)-y(i))^2];
    end
    distance = sqrt(sum(v));
end

Task 5.3

pointX = [1 2 5 7 9 8 0 6 8 6];
pointY = [4 3 3 5 6 2 1 5 5 3];
adp = [4.5,4];
D = [];
for i=1:length(pointX)
    plot(pointX(i), pointY(i), "k.",MarkerSize=10); hold on
    D = [D distanceCalculator([pointX(i) pointY(i)], adp)];
end
plot(adp(1),adp(2),"rx",MarkerSize=10);
for i=1:length(D)
    if D(i) == min(D)
        plot(pointX(i), pointY(i), "ro",MarkerSize=12); 
    end
end
function distance = distanceCalculator(x,y)
    distance = sqrt((x(1)-y(1))^2 + (x(2)-y(2))^2);
end

Task 5.4

function result = isInsideSquare(x,y,a,b,s)
    if x<=(a+s) && y<=(b+s) && x>=a && y>=b
        result = true;
    else
        result = false;
    end
end

Task 5.5

function result = isInsideSquare2(point,corner,s)
    if point(1)<=(corner(1)+s) && point(2)<=(corner(2)+s) && point(1)>=corner(1) && point(2)>=corner(2)
        result = true;
    else
        result = false;
    end
end

Task 5.6

point1 = [3 3];
corner1 = [1 2];
s1 = 4;
point2 = [2 3];
corner2 = [3 4];
s2 = 1;
figure 
axis square
line([corner1(1) corner1(1)],[corner1(2) corner1(2)+s1]); hold on
line([corner1(1) corner1(1)+s1],[corner1(2)+s1 corner1(2)+s1]);
line([corner1(1)+s1 corner1(1)+s1],[corner1(2)+s1 corner1(2)]);
line([corner1(1)+s1 corner1(1)],[corner1(2) corner1(2)]); 
if isInsideSquare(point1, corner1, s1)
    plot(point1(1), point1(2), "g.",MarkerSize=10);
else
    plot(point1(1), point1(2), "rx",MarkerSize=10); 
end
xlim([0 8])
ylim([0 8])

figure
axis equal
line([corner2(1) corner2(1)],[corner2(2) corner2(2)+s2]); hold on
line([corner2(1) corner2(1)+s2],[corner2(2)+s2 corner2(2)+s2]);
line([corner2(1)+s2 corner2(1)+s2],[corner2(2)+s2 corner2(2)]);
line([corner2(1)+s2 corner2(1)],[corner2(2) corner2(2)]);
if isInsideSquare(point2, corner2, s2)
    plot(point2(1), point2(2), "g.",MarkerSize=10);
else
    plot(point2(1), point2(2), "rx",MarkerSize=10); 
end
xlim([0 6])
ylim([0 6])



function result = isInsideSquare(point,corner,s)
    if point(1)<=(corner(1)+s) && point(2)<=(corner(2)+s) && point(1)>=corner(1) && point(2)>=corner(2)
        result = true;
    else
        result = false;
    end
end

Task 6.1

load NYSEstock.mat;
s1high = length(stock1(stock1(:,5)>stock2(:,5)));
s1low = length(stock1(stock1(:,5)<stock2(:,5)));
s1ret = diff(stock1(:,5));
s2ret = diff(stock2(:,5));
if mean(s1ret)>mean(s2ret)
    mret = 1;
else
    mret = 2;
end
if max(s1ret)>max(s2ret)
    dret = 1;
else
    dret = 2;
end
plot(stock1(:,5),"r"); hold on
plot(stock2(:,5),"b"); hold off
plot(s1ret,"r"); hold on
plot(s2ret,"b"); hold off

Task 6.2

function [equal, higher, lower, highest, lowest] = stockAnalyzer(stock)
    equal = [];
    higher = [];
    lower = [];
    highest = [];
    lowest = [];
    temp1 = sort(stock(:,6),"descend");
    temp2 = temp1(end-99:end);
    temp1 = temp1(1:100);
    for i=1:length(stock)
        if stock(i,2) == stock(i,5)
            equal = [equal; i];
        elseif stock(i,2) > stock(i,5)
            higher = [higher; i];
        else
            lower = [lower; i];
        end
        if ismember(stock(i,6),temp1)
            highest = [highest; i];
        elseif ismember(stock(i,6),temp2)
            lowest = [lowest; i];
        end
    end
end

Task 6.3

load NYSEstock.mat
[a b c d e] = stockAnalyzer(stock2);
figure
hold on
plot(stock2(:,5),"k-");
for i=1:length(stock2)
    if ismember(i,a)
        plot(i,stock2(i,5),"g.",MarkerSize=5);
    elseif ismember(i,b)
        plot(i,stock2(i,5),"b.",MarkerSize=5);
    elseif ismember(i,c)
        plot(i,stock2(i,5),"r.",MarkerSize=5);
    end
end
xlabel("days");
ylabel("closing price");
figure 
hold on
plot(stock2(:,6),"k-");
for i=1:length(stock2)
    if ismember(i,d)
        plot(i,stock2(i,6),"r.",MarkerSize=5);
    elseif ismember(i,e)
        plot(i,stock2(i,6),"b.",MarkerSize=5);
    end
end
xlabel("days");
ylabel("trading volume");
figure
hold on
scatter(stock2(:,5),stock2(:,6));
xlabel("closing price");
ylabel("trading volume");
function [equal, higher, lower, highest, lowest] = stockAnalyzer(stock)
    equal = [];
    higher = [];
    lower = [];
    highest = [];
    lowest = [];
    temp1 = sort(stock(:,6),"descend");
    temp2 = temp1(end-99:end);
    temp1 = temp1(1:100);
    for i=1:length(stock)
        if stock(i,2) == stock(i,5)
            equal = [equal; i];
        elseif stock(i,2) > stock(i,5)
            higher = [higher; i];
        else
            lower = [lower; i];
        end
        if ismember(stock(i,6),temp1)
            highest = [highest; i];
        elseif ismember(stock(i,6),temp2)
            lowest = [lowest; i];
        end
    end
end

Task 6.4

Tree5 = buildTree(5);
Tree11 = buildTree(11);

function Tree = buildTree(x)
    N = x;
    n = N-3;

    left = '/';
    right = '\';
    star = '*';

    Tree = [repmat('-',1,2*n-1),
        repmat(' ',1,n-1) 'I' repmat(' ',1,n-1),
        repmat(' ',1,n-1) 'U' repmat(' ',1,n-1)];
    for k = n:-1:1
        spaces = repmat(' ',1,n-k);
        layer = [];
        if k == 1
            layer = [layer star];
        else
            for l = 1:2*k-1
                if l==1
                    layer = [layer left];
                elseif l==k*2-1
                    layer = [layer right];
                else
                    layer = [layer star];
                end
            end
        end
        Layer = [spaces layer spaces];
        Tree = [Layer; Tree];
    end
end

Exam 1

Let x = [3 16 9 12 -1 0 -12 9 6 1]. Provide the command(s) that will:

  • set values that are multiples of 3 to 3 (make use of rem - for instance a number n is a multiple of 5 if rem(n,5) equals to 0);
  • extract the values of x that are greater than 10 into a vector called y;
  • set the values in x that are less than the mean to 0;
  • set the values in x that are above the mean to their difference from the mean.

Write your code is such a way that it works for any vector x.

x = [3 16 9 12 -1 0 -12 9 6 1];
% The instructions did not specifiy if the mean should be calculated based
% on the original x or the modified x, so in my solutions will recalculate
% the mean whenever a element of x changes.

% set values that are multiples of 3 to 3 
for i=1:length(x)
    if rem(x(i),3) == 0
        x(i)=3;
    end
end

% extract the values of x that are greater than 10 into a vector called y
y = x(x>=10);

% set the values in x that are less than the mean to 0
for i=1:length(x)
    mean(x)
    if x(i) < mean(x)
        x(i)=0;
    end
end

% set the values in x that are above the mean to their difference from the mean
for i=1:length(x)
    if x(i) > mean(x)
        x(i)=x(i)-mean(x);
    end
end

Exam 2

Task 1

Write a script that will use the random-number generator rand to determine:

  • the number of random numbers it takes before a number between 0.6 and 0.7 occurs;
  • the number of random numbers it takes before the mean of those numbers is within 0.01 and 0.5

If you simply type x=rand, x will be one randomly generated number from interval [0,1].\

% n1 is the number of random numbers it takes before a number between 0.6 
% and 0.7 occurs
v1 = [];
while true
    x=rand;
    if x>=0.6 && x<=0.7
        % break the loop if the randomly generated number is between 0.6
        % and 0.7
        break;
    else
        % otherwise add the number to vector v1
        v1 = [v1 x];
    end
end
% assign length of vector v1 to n1
n1 = length(v1);

% n2 is the number of random numbers it takes before the mean of those 
% numbers is within 0.01 and 0.5
v2 = [];
while true
    if mean(v2)>=0.01 && mean(v2)<=0.5
        % break the loop if the mean of previously generated numbers are
        % between 0.01 and 0.5, this equals to 0 during the first iteration
        % of the loop
        break;
    else
        % otherwise add the number to vector v2
        y=rand;
        v2 = [v2 y];
    end
end
% assign length of vector v2 to n2
n2 = length(v2);

Task 2

Write a script that for any given integer n and computes the following:

  • as long as the value of n is greater than 1 (notice this will require while-loop), replace n with (n/2) if the integer is even,
  • otherwise, replace n with (3n + 1).

Make the code count the number of values in (or the length of) the sequence that results. For instance, if n = 10, the sequence of integers is 5, 16, 8, 4, 2, 1, so the length is 6.

Then loop the script in such a way that it runs the algorithm for all n from 2 to 100, collects the respective sequence lengths into a vector.

Plot that vector.

% vector v is used to stored sequence lengths
v = [];
for n=2:100
    a = [];
    while n>1
        if rem(n,2) == 0
            % replace n with (n/2) if the integer is even
            n = n/2;
        else % otherwise replace n with (3n + 1)
            n = 3*n + 1;
        end 
        % add n to placeholder vector a
        a = [a n];
    end
    % add the length of vector a to vector v
    v = [v length(a)];
end
% plot the sequence lengths using vector v
plot(v)

Exam 3

Task 1

Write a code which will calculate the total electricity bill respective to the total consumption, following the charges:

  • For first 50 units respective price 0.50/unit
  • For next 100 units respective price 0.75/unit
  • For next 100 units respective price 1.20/unit
  • For unit above 250 respective price 1.50/unit

Using the following piece of code round(400*rand(20,1)) generate twenty random consumption values (note they will range between 0 and 400), and calculate their respective total bills.

Then plot them as points, in such a way that x axis contains just given consumption's index (location in the consumption vector), and y axis contains the consumption value, but each of the four categories is shown with a distinct marker style.

% create vector of random consumption values
consumption = round(400*rand(20,1));

% bill vector stores the respective total bill of each consumption value
bill = [];

% open figure in fullscreen and enable retaining current plot
figure('units','normalized','outerposition',[0 0 1 1]) 
hold on

% start loop to iterate thorugh consumption values
for i=1:length(consumption)
    % start if-else to calculate the bill and plot the graph
    if consumption(i) > 250
        bill = [bill; 50*0.5 + 100*0.75 + 100*1.2 + (consumption(i)-250)*1.5];
        plot(i,consumption(i),"r*");
    elseif consumption(i) > 150 
        bill = [bill; 50*0.5 + 100*0.75 + (consumption(i)-150)*1.2];
        plot(i,consumption(i),"bo");
    elseif consumption(i) > 50
        bill = [bill; 50*0.5 + (consumption(i)-50)*0.75];
        plot(i,consumption(i),"gs");
    else
        bill = [bill; consumption(i)*0.5];
        plot(i,consumption(i),"k.");
    end
    % add the bill of the consumption value next to the plotted point; this
    % was not required but because I calculated the bill already, why not
    % use it?
    text(i+0.1,consumption(i),string(bill(i)));
end

% add labels to the graph
xlabel("index");
ylabel("consumption values");

Task 2

Given an array AN×3 (that is, array A which is sized N by 3), we draw N discs in a 2D plane. In the array A, the first column represents the x coordinate of the center of each disc, the second column represents the y coordinate of the center of each disc, and the third column contains the radius of the disc.

Create a Matlab code which draws all the discs defined by array A, where the biggest disc(s) (with biggest radius) is drawn in red, the smallest one(s) is drawn in green, and the remaining ones in blue. Plot also the circle centers (with color respective to the circle itself) as points with marker size equal 5.

  • Set the plot axis so that the circles are actually circles (not flattened to ellipses).

  • Use Help to learn how to add text into the figure (in figure area, not titles or labels). Then add circle center coordinates for all circles in the figure in form (x0,y0) next to all circle centers.

Test your code on an array A containing at least 3 different discs. An example output for array A = [0 0 2; 2 0 1; 4 4 5] should look as presented in figure below.

% create array A
A  = [0 0 2; 2 0 1; 4 4 5] ;

% vector t ∈ [0,2π]
t = 0:pi/50:2*pi;

% open figure in fullscreen, enable retaining current plot, and make axes
% into equal length
figure('units','normalized','outerposition',[0 0 1 1])
hold on
axis square

% iterate through all elements of A
for i=1:length(A)
    % check if the element has the biggest radius, and draw the circle 
    % and the center point in red
    if A(i,3) == max(A(:,3))
        plot((A(i,3) * cos(t) + A(i,1)),(A(i,3) * sin(t) + (A(i,2))),"r-");
        plot(A(i,1),A(i,2),"r.",MarkerSize=5);
    % check if the element has the smallest radius, and draw the circle 
    % and the center point in green
    elseif A(i,3) == min(A(:,3))
        plot((A(i,3) * cos(t) + A(i,1)),(A(i,3) * sin(t) + (A(i,2))),"g-");
        plot(A(i,1),A(i,2),"g.",MarkerSize=5);
    % other elements will be drawn in blue
    else
        plot((A(i,3) * cos(t) + A(i,1)),(A(i,3) * sin(t) + (A(i,2))),"b-");
        plot(A(i,1),A(i,2),"b.",MarkerSize=5);
    end
    % add coordinates to cirle center
    text(A(i,1)+0.15,A(i,2),'('+string(A(i,1))+","+string(A(i,2))+')');
end