You are on page 1of 7

Homework #4

October 14, 2021


CHE 492 - Computer Methods in Chemical Engineering - Dr. Jason Bara
By: Abdulaziz Alhouti

1. Question 1:

1.1. Objective:
The first question investigates the possibility of a two-letter URL existing for an educational
institution by generating URLs from all letters of the alphabet and pinging the website. If the
URL exists, the pinged website will return the page's title, allowing the user to count how
many websites exist and how likely are they to have the word "University" in their title. A
MATLAB script is used to generate the data, which is then analyzed.

1.2. Methods:
In order to generate 2 letter URLs, I created a string containing every letter of the alphabet
from 'aa' to 'zz' and a for loop that assigns the second letter of the 2 letter URL within a
larger for loop that assigns the first letter of the 2 letter URL within each iteration, yielding
26 x 26 combinations. The strcat()function was then used to build the two-letter URLs
and checked whether they existed by trying to gather the URL's data using the webread()
function within a try-catch statement that overcomes errors caused by non-existent URLs
and produces a statement alerting us we have a bad URL. The university names are stored in
an array, and the function count() is used to calculate the number of websites with the
word "University" in their title. Figure 1 shows a code snippet that explains this loop. The
entire script can be found in Appendix A.

%Create a for loop that assigns the first letter of a 2 letter URL to variable 'first' and changes
this letter within each iteration to obtain all possible 2 letter combinations
for i = 1:1:numel(alphabet)
%This for loop is used to assign the second letter within the 2 letter URLs in order to obtain the
2 URL letter combination 26x26=676 possibilities
for j = 1:1:numel(alphabet)
%Create the 2 letter URL by adding www. before the URL and .edu after it
try %To alter the default error behavior, receives content from the web service given by the
pinged URL, returns it as data in the try block, and catches resulting errors in the catch block.
catch
%If the URL doesn't exist, displays the message 'Bad URL.'
end %end of try-catch statement
%Extract the name of the university that appears in the title from the source pages and checks
whether the name of the University exists
if %if conditions are fulfilled return name of the University
else %if condition isn't fulfilled return name “not exist”
end %end of if statement
%Collect the names of the universities in an array
end %end of secondary for loop
end %end of primary for loop

Figure 1. Code snippet of the for loop used to check for universities with 2 letter URLs

1
1.3. Results:
After running the MATLAB script, I had to check for the names and number of universities
obtained with 2 letter URLs as well as the ones having the word “University” in their title.
Checking the matrix namesarray I found that several URLs didn’t belong to university
websites although they ended with the “.edu” extension as can be seen in Table 1 below. This
got me curious to check one of those URLs which appeared to be an online scamming
website. Knowing that, I filtered the results obtained and eliminated the names of the
websites which weren’t an educational institution. Finally, I checked the true number of
universities obtained which was 87 out of which 43 one which had the word University in
their Title. Table 1 below shows a sample of the results obtained by the MATLAB run.

Table 1. Sample of the results obtained by the MATLAB script (Educational Institutions
marked in green vs. non-educational one marked in red)

Names of Universities with 2 letter URLs


Walters State Community College
Arizona Community College
Baldwin Wallace University
E.I. School Of Professional Makeup
Carson-Newman University
Emmanuel College
미주 한인 신학교 | 그레이스미션대학교
Lenoir-Rhyne University
Iowa Wesleyan University is a fully accredited, Liberal Arts College in Iowa
Cloud Computing
Boston University

Table 1. shows educational websites marked in green and non-educational ones marked in red
which must be further discussed for the precision of the results obtained.
1.4. Discussion
The findings caught my interest as to why such non-educational institutions had the following
URLs and existed. Under further investigation, I found out that most of those URLs were
websites for scamming purposes. Moreover, I was also interested to check 3 letter URL
universities and thus introduced a third for loop within the second for loop as well a
variable third which represent the third character in the URL and thus generated 17576
possibilities of 3 letter URLs. The MATLAB script did work; however, it took a long time to
run all those combinations and therefore terminated its execution halfway after 2 hours of run
time. This made me think about the emerging technology which will give rise to highly
complicated and fast executing quantum computers.

2
2. Question 2:
2.1. Objective:
In this question, we will investigate the routes and minimum ship speed required for it to
reach home port safely without being targeted by an enemy submarine. The relative speed of
the ship-to-submarine is studied throughout MATLAB simulations in order to determine the
minimum speed ratio and therefore determine the ship’s speed.

2.2. Methods:
First, I assigned a variable subradius representing the submarine’s scanning radius which
scans for the ship in a circular pattern of radius r. I then created a while loop that increase
the submarines scanning radius while also moving the ship. I then created an if statement
within the while loop which acted as the conditional directing the ship in its normal course
if it wasn’t discovered by the submarine covering half distance to the port; however, if these
conditions weren’t true an else condition is used to change the path of the ship with an
angle theta. The ships cartesian coordinates were converted to polar coordinates using the
cart2pol() function in order to later change its trajectory. Several MATLAB test runs
were simulated and plotted using plot() function. This problem was complicated for me
to solve it using only MATLAB functions, therefore, I tested out an assumption to calculate
the distance travelled by the ship until its first discovered by the submarines as can be seen in
Figure 2 below. The full MATALAB script can be found in Appendix B.

Figure 2. MATLAB simulation showing the moment the ship was discovered by the
submarine

From figure 2. We can determine the distance travelled by the ship until it was discovered by
the submarine by reading the corresponding x and y coordinates of the ship. We can also use
the tic toc command to time the execution of the script in order to get the ships relative
speed to the submarine.
3

2.3. Results:
After running the MATLAB script, I decided to follow a visual approach to find a close
solution to the problem. I ran several simulations and discovered that the ship wasn’t
discovered by the submarine if its x-coordinates changed by 3.2 units and y-coordinates
changed by 2.2 units as can be seen in figure 3 below. This, however, isn’t the accurate
minimum speed of the ship required although I believe is very close to the true speed.

Figure 5. MATLAB simulation where ∆ x=3.3 and ∆ y =2.2

2.4. Discussion:
The results show that xship must change by 3.3 units and yship must change by 2.2 units in
order for the ship to reach its home port safely. Serval test runs can be utilized to produce
more accurate results than those acquired by averaging them over several test runs;
nonetheless, I feel that a more appropriate algorithmic technique to solving this problem can
be applied. This problem reminded me of the complicated algorithms used in world wars and
the techniques utilized to keep the marine force safe while on the move.
4
3. New Question:

Consider there is a bus with infinite seats, and an infinite number of passengers is getting on
this bus. Would the bus eventually run out of seats? State your assumptions.
5
Appendix A
A.1: Full script used to calculate the number of educational
institutions having URLs with 2 letters

A.1: Full script used to calculate the number of educational institutions having URLs with 2 letters:
%Search University Websites
alphabet = 'a':'z'; %Create a string from 'aa' to 'zz'
namesarray = []; %Create an empty array to fill it with name of the university having 2 letter
URLs
for i = 1:1:numel(alphabet) %Create the first for loop that creates 2 letter URLs by assigning
the
first = alphabet(i); %first letter to 'first' and iterating to the next letter within each
interation
for j = 1:1:numel(alphabet) %Creates a for loop within the first for loop to assigning the
second letter within the 2 letter URLs
second = alphabet(j); %to 'second' and iterating to the next letter within each
interation
str = strcat(first, second); %concatenates the destination string 'str' and the source
string 'first and second'
%and the result is stored in the destination string 'str'
url = strcat('https://www.',str,'.edu'); %generates the corresponding university URL by
combing 'str'
%within 'https://www.' and' .edu'
try %reads content from the web service specified by the pinged URL and returns the
content in data in the try block
%and catches resulting errors in the catch block in order to override the default
error behavior
code = webread(url);
catch
disp('bad url')%displays the statement 'bad URL' if the URL doesn't exist
end
name = extractBetween(code, '<title>', '</title>'); %extracts the name of the University
included
%in the title from the pages source
if ~isempty(name)%if statement checks if the university's name exists if condition is
fullfilled
name = name(1); %it returns the name of the university and stores it temporarily in
name
else
name = "not exist"; %if condition isn't fulfilled means that the name doesn’t exist
and thus returns "not exist"
end
namesarray = [namesarray, name]; %stores the names of the universities in the namesarray
end
end
namesarray = unique(namesarray); %remove all duplicates of university names
L=length(namesarray); %returns the number of universities
A = count(namesarray,"University"); %returns the number of occurrences of "University" is the
namesarray
S=sum(A,2); %returns the number of websites that have the word "University" in them
F=(100*S)/L; %calculate the percentage of websites that have the word "University" in them
y=[F,100-F]; %Create a matrix with the percentage of websites that have the word "University" in
them and those which don't
labels={'Univeristy Included in Title','Univeristy not Included in Title'};%create labels to the
pie chart components to be created later
pie(y, labels)%Create a pie chart to represent the percentage of websites that have the word
"University" in them and those which don't
1A

Appendix B
B.1: Full script used to simulate the scenarios of the ship’s path and
submarine tracking position
.

B.1: Full script used to simulate the scenarios of the ship’s path and submarine tracking position:
%Ship vs. Submarine Race
%2021-10-05
clf %deletes the plot without affecting previous ones
shipx = 1000; %x-y coordinates of the ship
shipy = 0;
subradius = 0; %submarine’s radius
portx = -1000; %x-y coordinates of the home port
porty = 0;
speedratio = 2.3; %relative speed of ship-to-sub
shippathx = [1000]; %x-y coordinates of the the ship path
shippathy = [0];
while subradius < 1000 %while loop of the submarine is scanning a circular patern of radius r
subradius = subradius + 1;
if shipx > 0 %if statement conditions are fulfilled the ship will move in its trajectory
shipx = shipx - 2; %ship moves by 2 units in negative x-direction
shipy = shipy - 1; %ship moves by 1 unit in negative y-direction
else %if statement conditions are not fulfilled change the ship changes trajectory
[theta, rho] = cart2pol(shipx, shipy);%cartesian to polar ship coordinates
movecircle = -1.3/(subradius*pi); %change ship direction
theta = theta + movecircle; %changes the trajectory of the ship by changing theta angle
[shipx, shipy] = pol2cart(theta, rho); %polar to cartesian coordinates
shipx = shipx - 1; %ship moves by 1 unit in negative x direction
end
shippathx = [shippathx, shipx]; %collect the ship path x-coordinates data in an array
shippathy = [shippathy, shipy]; %collect the ship path y-coordinates data in an array
plot(shippathx, shippathy,'LineWidth',5) %plot the ship path
xlim([-1000, 1000]); %sets the x-axis limits for the current axes
ylim([-1000, 1000]); %sets the x-axis limits for the current axes
hold on %retain plots in the current axes so that new plots added to the axes do not delete
existing plots
p = nsidedpoly(1000, 'Center', [0 0], 'Radius', subradius); %returns a regular circle of the
submarines scaning paths
%with the submarine at its center
plot(p, 'FaceColor', 'r') %plot the submarines sonar
%plot(1000, 0, '.r', 'MarkerSize', subradius*2)
pause(0.01); % temporarily stops MATLAB execution and waits for the user to press any key
end

1B

You might also like