Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Homework #4

October 14, 2021


CHE 492 - Computer Methods in Chemical Engineering - Dr. Jason Bara
By: Fawaz Alsaiede

1. Question 1:

1.1. Objective:
The first topic investigates the likelihood of an educational institution having two letter URLs
through generating URLs from the entire alphabet and pinging the website. The pinged website
will return the page’s title if the URL exists, thus determining the number of such existing
websites as well as the likelihood of them having the word “University” in their title. The data
is generated using a MATLAB script and the results collected are later analyzed.

1.2. Methods:
I needed to first come up with a method to generate 2 letter URLs, therefore, I created a string
containing all the alphabet from ‘a’ to ‘z’ and created 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 thus obtaining 26 x 26 combination or in other words 676 possibilities. I
then used the strcat()function to generate the 2 letter URLs and checked whether they
existed or not by trying to gather the URL’s data using the webread() function within a
try and catch statement that overrides errors generated by nonexistent URLs and returns a
statement informing us we have a bad URL. The university names obtained are collected within
an array where the count() function is used to determine the number of websites that have
the word “University” in their title. A code snippet explaining this loop is shown in Figure 1.
See Appendix A for the complete script.

for i = 1:1:numel(alphabet) %Create the first for loop that creates 2 letter URLs by assigning the
%first letter to 'first' and iterating to the next letter within each iteration
for j = 1:1:numel(alphabet) %Creates a for loop within the first for loop to assign the second
letter within the 2 letter URLs to 'second' and iterating to the next letter within each iteration
%concatenate the destination string 'str' and the source string 'first and second' and the result
is stored in the destination string 'str'
%concatenates the destination string 'str' and the source string 'first and second ‘and the result
is stored in the destination string 'str'
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
catch
%displays the statement 'bad URL' if the URL doesn't exist
end %end of try-catch statement
%extracts the name of the University included in the title from the pages source
if if statement checks if the university's name exists if condition is fulfilled it returns the
name of the university and stores it temporarily in name
else
%if condition isn't fulfilled means that the name doesn’t exist and thus returns "not exist"
end %end of if statement
%stores the names of the universities in the namesarray
end %end of second for loop
end %end of first for loop

Figure 1. Code snippet of the loop used to check for the existence of the generated 2 letter URLs

1
1.3. Results:
After running the MATLAB script, I had to check for the matrix namesarray which I
designated to collect the names of the universities with 2 letter URLs as well as the parameters
L and S which were designated to collect the number of universities obtained and the ones
which had the word “University” in their title respectively. Checking the designated variables,
I saw that the number of universities obtained was 60 and the one which had the word
University in their Title was 29. Figure 2 below shows the percentage of universities which
have the word “University” in their title vs. those which don’t.

Figure 2. Pie chart showing the percentage of universities which have the word “University” in
their title vs. those which don’t.
Figure 2 shows a 48.33% for the university titles which include the word “University” vs. a 47.33%
for the universities which don’t.
1.4. Discussion
The results obtained piqued my interest and motivated me to research the meaning behind the
sections used in a URL and what do they represent. This research revealed that according to
statistics 2 letter URLs were more visually distinct and attractive to people visiting those
websites and therefore resulted in more web traffic on them. Moreover, 2 letter URLs reveal
the main domain of the respective institution holding this website with its respective
subdomain. I was also interested to check for Canadian Universities with 2 letter URLs by
exchanging '.edu' with '.ca' in url = strcat(). The result obtained were filtered to
get educational institutions only and unexpectedly only 23 Universities were found to have 2
letter URLs, however, the word “University” was included in their title with a percentage
95.65% of the universities having 2 letter URLs.

2
2. Question 2:
2.1. Objective:
The second question investigates how fast a ship must be in order to avoid being targeted by
an enemy submarine and be able to set a wide course around it and reach home port safely. A
MATLAB code is used to investigate many scenarios where the relative speed of the ship to
the submarine is taken as the controlling variable as well as the ship’s maneuvers to change its
course if being detected by the submarine sonar.

2.2. Methods:
First, I assighed xy coordinates to each of the ship, the submarine and the home port.I then
assigned a subradius variable considered as the subs sonar which scans a circular perimeter
at a certain distance and increases its scan radius within a designated while loop that moves
the ship by ∆𝑥 and ∆𝑦 coordinates. I used an if statement within the while loop in order
to keep the ship on its directed trajectory as long as it’s not detected by the submarine’s radar.
The moment the ship was detected by the radar it would change its trajectory by changing its
direction and moving forward towards the home port. To achieve this I used the cart2pol()
in order to translate the ship’s coordinates from cylindrical to polar and change its direction by
adding a factor to it’s direction theta. The MATLAB simulation was plotted using the plot()
function and hold on command. In order to solve the problem of the required ship’s speed
relative to the submarine, I would suggest calculating the relative ship-to-submarine velocity
as follows:
0.5
𝑉𝑠ℎ𝑖𝑝 (𝑉𝑥2 + 𝑉𝑦2 )
=
𝑉𝑠𝑢𝑏𝑚𝑎𝑟𝑖𝑛𝑒 (𝑉𝑟2 )0.5

0.5
∆𝑥 2 ∆𝑦 2
(( 𝑡 ) + ( 𝑡 ) )
𝑉𝑠ℎ𝑖𝑝
= 0.5
𝑉𝑠𝑢𝑏𝑚𝑎𝑟𝑖𝑛𝑒 ∆𝑟 2
(( 𝑡 ) )

((∆𝑥)2 + (∆𝑦)2 )0.5


𝑉𝑠ℎ𝑖𝑝 𝑡
=
𝑉𝑠𝑢𝑏𝑚𝑎𝑟𝑖𝑛𝑒 ∆𝑟
𝑡

𝑉𝑠ℎ𝑖𝑝 ((∆𝑥)2 + (∆𝑦)2 )0.5


=
𝑉𝑠𝑢𝑏𝑚𝑎𝑟𝑖𝑛𝑒 ∆𝑟

Using trial and error method, several trials can be run on MATLAB in order to check the
simulated results and change the values of ∆𝑥 and ∆𝑦 of the ship while keeping ∆𝑟 constant
and equal to 1. A code snippet explaining the aforementioned loop can be found in Figure 3
below. The full MATALAB script can be found in Appendix B.

3
while subradius < 1000 %while loop where the submarine sonar is scanning a circular perimeter
with radius r which is increasing with each iteration
if shipx > 0 %if statement conditions are fulfilled the ship will continue in its assigned
trajectory
%ship moves by 2 units in x-direction towards the port
%ship moves by 1 unit in y-direction towards the port
else %if statement conditions are not fulfilled change the trajectory of the ship
%transform the corresponding two-dimensional Cartesian coordinates of the ship arrays x and y
into polar coordinates theta and rho
%calculate the change in trajectory of ship
%change the trajectory of the ship by changing theta angle
%change the polar ship coordinates to cartesian coordinates
%ship moves by 1 unit towards the home port
end
%collect the ship path x- and y-coordinates data in an array and plot them
%collect submarine sonar radius data and plot them
End

Figure 3. Code snippet of the loop used to simulate the scenarios of the ship’s path and submarine
tracking position
2.3. Results:
After running the MATLAB script, and taking several values of ∆𝑥 and ∆𝑦 several scenarios
were obtained, some of which the ship reached home port safely. This made me realize that
there is a missing factor in the MATLAB script accounting for the change in the ship’s path
where it shouldn’t be based solely on the ship’s path and submarines sonar perimeter. The
simulations obtained can be seen in Figures 4, 5 where the ship was sunk and 6 where its
reached the home port safely respectively where ∆𝑥 and ∆𝑦 were varied while ∆𝑟 was held
constant and equal to 1.

Figure 5. MATLAB simulation where ∆𝑥 = 2 and ∆𝑦 = 1 Figure 6. MATLAB simulation where ∆𝑥 = 3 and ∆𝑦 = 2

Figure 7. MATLAB simulation where ∆𝑥 = 3.5 and ∆𝑦 = 2.5

4
2.4. Discussion:
The results demonstrate that in order for the ship to reach home port safely, its speed must lie
in the range where 3 < ∆𝑥 ≤ 3.5, 2 < ∆𝑦 ≤ 2.5. Serval test runs can be performed in order
to determine the minimum required speed to reach home port safely, however, this would take
extensive time. These results remain inaccurate as several factors weren’t accounted for which
made me curious about the algorithms developed during the 2 world wars in order for the
nations to navigate the sees while keeping their troops safe and sound. It must have been very
complex algorithms; however, the human factor had a great influence on life and death
decisions made by them.

5
3. New Question:

Consider a scenario in which n planes are arriving at an airport within one hour. The airport
manager has to land all planes safely, however, there is a 15 min interval between each plane
landing and the planes have fuel enough to circulate around the airport for an hour. How many
planes can land safely in the airport?

6
Appendix A
A.1: Full script used to calculate the number of educational
institutions having two letter URLs

A.1: Full script used to calculate the number of educational institutions having two letter URLs:
%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. However, it does not affect the background color of the figure
shipx = 1000; %x-y coordinates of the ship
shipy = 0;
subradius = 0; %sumarine's sonar radius
portx = -1000; %x-y coordinates of the home port
porty = 0;
speedratio = 2.3; %relative speed of ship to sub speed of sub = 0.972
shippathx = [1000]; %x-y coordinates of the the ship path
shippathy = [0];
while subradius < 1000 %while loop where the submarine sonar is scanning a circular perimeter
subradius = subradius + 1; %with radius r which is increasing with each iteration
if shipx > 0 %if statement conditions are fulfilled the ship will continue in its assigned
trajectory
shipx = shipx - 2; %ship moves by 2 units in x-direction towards the port
shipy = shipy - 1; %ship moves by 1 unit in y-direction towards the port
else %if statement conditons are not fulfilled change the trajectory of the ship
[theta, rho] = cart2pol(shipx, shipy);%transforms corresponding elements of the two-
dimensional Cartesian coordinate
%arrays x and y into polar coordinates theta and rho
movecircle = -1.3/(subradius*pi); %calculates the change in trajectory of ship
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 towards the home port
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 sonar
%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