Add second year

This commit is contained in:
2023-12-07 01:19:12 +00:00
parent 3291e5c79e
commit 3d12031ab8
1168 changed files with 431409 additions and 0 deletions

View File

@ -0,0 +1,8 @@
clear;
d = roll_2_dice(10000, 100);
[freq, prop] = tabulate_2_dice(d);
disp(freq);
disp(prop);

View File

@ -0,0 +1,7 @@
function d = roll_2_dice(N, seed)
% function to roll 2 dice and return the combination of each device row in a vector
rng(seed);
% generating two 1 * N vectors of 6 simulated dice rolls and adding them
d = randi([1 6], 1, N) + randi([1 6], 1, N);
end

View File

@ -0,0 +1,14 @@
function [freq, prop] = tabulate_2_dice(d)
% function to calculate the frequency and proportion of each outcome based on a set of dice throws
freq = zeros(1,12);
% looping through all the values in d and incrementing the corresponding counter in freq
for i = d
freq(i) = freq(i) + 1;
end
% looping through each index in prop and calculating the proportion pertaining to that index
prop = zeros(1,12);
for i = [1:12]
prop(i) = freq(i) / sum(freq); % proportion of i is the frequency of i divided by the sum of all freqs
end
end

View File

@ -0,0 +1,28 @@
function[push, pop, peek] = mystack()
% function to return handles to the subfunction push, pop, & peek
push = @push;
pop = @pop;
peek = @peek;
end
function [stack] = push(stack, value)
% function to push a value onto the stack at location 1 and return the
% stack
stack = [value; stack];
end
function [stack] = pop(stack)
% function to pop the value at location 1 off the stack and return the
% stack
stack(1) = [];
end
function [value] = peek(stack)
% function to return the top value from the stack (arrau location 1)
% returns NaN if there is no value at location 1
if isempty(stack)
value = NaN;
else
value = stack(1);
end
end

View File

@ -0,0 +1,13 @@
% test script as specified in assignment spec
[push, pop, peek] = mystack();
stack = []
stack = push(stack, 100)
stack = push(stack, 200)
peek(stack)
stack = pop(stack)
peek(stack)

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 228 KiB

View File

@ -0,0 +1,15 @@
clear;
eng1 = imread("Engineering-Building.jpg");
eng1_gs = pic2grayscale(eng1);
eng1_gs_inv = transform_pic(eng1_gs);
eng1_gs_bin_50 = transform_threshold(eng1_gs,50);
eng1_gs_bin_75 = transform_threshold(eng1_gs,75);
eng1_gs_bin_100 = transform_threshold(eng1_gs,100);
% plotting images
subplot(3,2,1),imshow(eng1),title("Original Picture");
subplot(3,2,2),imshow(eng1_gs),title("Greyscale");
subplot(3,2,3),imshow(eng1_gs_inv),title("Inverted Greyscale");
subplot(3,2,4),imshow(eng1_gs_bin_50),title("Binary Threshold = 50");
subplot(3,2,5),imshow(eng1_gs_bin_75),title("Binary Threshold = 75");
subplot(3,2,6),imshow(eng1_gs_bin_100),title("Binary Threshold = 100");

View File

@ -0,0 +1,17 @@
function [returnImg] = pic2grayscale(img)
% which uses the NTSC Standard transformation to convert RGB to grayscale.
%0.2989 * R + 0.5870 * G + 0.1140 * B
% img to be returned
returnImg = zeros(size(img,1), size(img, 2));
% looping through the RGB image and calculating the grayscale value for
% each pixel in the corresponding returnImg
for i = 1:size(img,1)
for j = 1:size(img,2)
returnImg(i,j) = (0.2989 * img(i,j,1)) + (0.5870 * img(i,j,2)) + (0.1140 * img(i,j,3));
end
end
returnImg = uint8(returnImg);
end

View File

@ -0,0 +1,5 @@
function [img] = transform_pic(img)
% function which converts a 255 colour code to 0, 254 to 1, etc, and 0 to
% 255.
img = 255 - img;
end

View File

@ -0,0 +1,18 @@
function [img] = transform_threshold(img, threshold)
% function which converts the picture to binary format where any value
% above the threshold is white (1), and all values equal to or below are
% black (0).
% looping through each element in the matrix, and setting it to 1 if
% above the threshold, 0 otherwise
for i = 1:numel(img)
if img(i) > threshold
img(i) = 1;
else
img(i) = 0;
end
end
% casting the matrix to type logical once each element is either 1 or 0
img = logical(img);
end

View File

@ -0,0 +1,14 @@
function dydt = shark_tuna_model(t,x)
% function to model the prey-predator population relations over time of
% sharks & tuna
global k; % mentioning global variable x so that it can be used here
dydt = [0; 0];
% dS/dt = k1 S T - k2 S
dydt(1) = k(1)*x(1)*x(2)-k(2)*x(1);
% dT/dt = k3 T -k4 S T
dydt(2) = k(3)*x(2) - k(4) * x(1) * x(2);
end

View File

@ -0,0 +1,12 @@
clear;
global k;
k = [0.015 0.7 0.5 0.01].';
[t,y] = ode45(@shark_tuna_model, [0 50], [100 100]);
plot(t,y);
title("SHARK-TUNA POPULATION DYNAMICS LIMIT CYCLING");
xlabel("TIME");
ylabel("POPULATION NUMBERS");
legend("SHARKS", "TUNA", "Location", "northwest");

View File

@ -0,0 +1,15 @@
function dydt = SIR(t, x, c, i, alpha, beta, gamma)
dydt = [0; 0; 0; 0; 0];
S = x(1);
I = x(2);
R = x(3);
H = x(4);
RH = x(5);
N = S + I + R + H + RH;
dydt(1) = (-c*S) * (I/N) * i;
dydt(2) = (c*S) * (I/N) * i - (alpha*I);
dydt(3) = (alpha*I) - (beta*R);
dydt(4) = (beta*R) - (gamma*H);
dydt(5) = gamma*H;
end

View File

@ -0,0 +1,40 @@
clear;
i = 0.125;
alpha = 0.25;
beta = 0.02;
gamma = 0.10;
time_vec = 0:.25:100;
init_vec = [9999 1 0 0 0];
c = linspace(3, 8, 20);
infected_stock = zeros(length(time_vec), 20);
in_hospital = zeros(length(time_vec), 20);
for loopcounter = 1:20
[t,y] = ode45(@SIR, ...
time_vec, ...
init_vec, ...
odeset, ...
c(loopcounter), ...
i, ...
alpha, ...
beta, ...
gamma);
infected_stock(:, loopcounter) = y(:,2);
in_hospital(:,loopcounter) = y(:,4);
end
subplot(3, 1, 1);
plot(time_vec, infected_stock);
title("Infected Stock");
subplot(3, 1, 2);
plot(time_vec, infected_stock);
title("People in Hospital");
subplot(3,1,3);
scatter(c, max(in_hospital));
title("Contacts v Peak in Hospital");

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,76 @@
clear;
% 1. read in the file and confirm the number of records (336,766)
flights = readtable("Flights.csv");
numRecords = height(flights)
% 2. convert "origin" & "dest" to strings (from cell type)
flights.origin = string(flights.origin);
flights.dest = string(flights.dest);
% 3. check the number of missing values for the departure time
numMissing = sum(ismissing(flights.dep_delay))
% 4. filter all the missing values from the departure delay and check the
% difference in the number of records
flights_clean = flights(~isnan(flights.dep_delay), :);
numRecordsClean = height(flights_clean);
disp("Number of records in flights = " + numRecords);
disp("Number of records in flights_clean = " + numRecordsClean);
% 5. confirm the difference in records between the two tables
diff = numRecords - numRecordsClean
% 6. Remove any departure delay greater than 2 hours (120 minutes). This
% leaves 318,798 observations.
flights_final = flights_clean(flights_clean.dep_delay <= 120, :);
height(flights_final)
% 7. Generate the following table and graph, showing the average delay per
% month.
months = unique(flights_final.Month);
res1 = table(months, zeros(size(months)), 'VariableNames', {'Month', 'AvgDelayMonth'});
for i = months(1):length(months)
month_delays = flights_final.dep_delay(flights_final.Month == i);
avg_delay_month = mean(month_delays);
res1.AvgDelayMonth(i) = avg_delay_month;
end
res1
plot(res1.Month, res1.AvgDelayMonth, '-o');
title('Average Delay by Month');
% 8. Generate the following table and graph, showing the average delay per
% hour.
hours = transpose(1:24);
res2 = table(hours, zeros(size(hours)), 'VariableNames', {'Hour', 'AvgDelayHour'});
for i = hours(1):length(hours)
hour_delays = flights_final.dep_delay(flights_final.hour == i);
avg_delay_hour = mean(hour_delays);
res2.AvgDelayHour(i) = avg_delay_hour;
end
res2 = res2(~isnan(res2.AvgDelayHour),:);
res2
plot(res2.Hour, res2.AvgDelayHour, '-o');
title('Average Delay by Hour of the Day');
% 9. Generate the following table and graph, showing the average delay by
% month and by origin
res3 = renamevars(removevars(groupsummary(flights_final,["Month","origin"],"mean","dep_delay"),'GroupCount'), 'mean_dep_delay', 'AvrDelayMonthOrigin')
jfk = res3(res3.origin == 'JFK', {'Month', 'AvrDelayMonthOrigin'});
subplot(3,1,1);
plot(jfk.Month, jfk.AvrDelayMonthOrigin, '-o');
title("JFK");
ewr = res3(res3.origin == 'EWR', {'Month', 'AvrDelayMonthOrigin'});
subplot(3,1,2);
plot(ewr.Month, ewr.AvrDelayMonthOrigin, '-o');
title("EWR");
lga = res3(res3.origin == 'LGA', {'Month', 'AvrDelayMonthOrigin'});
subplot(3,1,3);
plot(lga.Month, lga.AvrDelayMonthOrigin, '-o');
title("LGA");

View File

@ -0,0 +1,9 @@
function [grades] = clean_grades(grades)
for i = 1:(size(grades,1)*size(grades,2))
if grades(i) < 0
grades(i) = 0;
elseif grades(i) > 100
grades(i) = 0;
end
end
end

View File

@ -0,0 +1,4 @@
function [grades] = create_grades(rows, columns, min, max, seed)
rng(seed);
grades = randi([min max], rows, columns);
end

View File

@ -0,0 +1,25 @@
function [grades] = fill_estimates(grades)
for i = 1:size(grades, 2)
% making copy
copy = grades(:, i);
nonzero = 0; % count of nonzero elements to calculate avg
sum = 0; % sum of nonzero elements
% looping through copy and counting number of nonzero elements
for j = 1:size(copy,1)
if copy(j) ~= 0
nonzero = nonzero + 1;
sum = sum + copy(j);
end
end
avg = round(sum / nonzero);
% looping through original and replacing any nonzero with average
for j = 1:size(copy,1)
if grades(j,i) == 0
grades(j,i) = avg;
end
end
end
end

View File

@ -0,0 +1,4 @@
clear;
gr1 = create_grades(10,5,-10,110,100);
gr2 = clean_grades(gr1);
gr3 = fill_estimates(gr2);

View File

@ -0,0 +1,22 @@
clear;
r = 0.2; % random val for r
K = linspace(1000, 1000000, 50);
time_vec = linspace(0, 100, 50);
P = 100; % random val for P
population = zeros(length(time_vec), 50);
% implementing the model as an anonymous function
dpdt = @(t,P,r,K) r*P * (1 - (P/K));
for loopcounter = 1:50
[t,y] = ode45(dpdt, ...
time_vec, ...
P, ...
odeset, ...
r, ...
K(loopcounter));
population(:, loopcounter) = y(:,1);
end
plot(time_vec, population);