% 1. You are given two sides of a triangle, a = 4.5 and b = 6. The angle
% between them is 35 degrees. Write a script to find the length of the
% third side and the area of the triangle.
%
% The formula for the third side of a triangle is:
% c^2 = a^2 + b^2 - 2ab cos(th)
% where th is the angle between the sides a abd b
% The formula for the area of a triangle is:
% area = 1/2 ab sin th
% Give data:
a = 4.5
b = 6
th = 35 * pi / 180
% Compute the third side
c = sqrt(a.^2 + b.^2 - 2 .* a .* b .* cos(th))
% Compute the area:
area = 0.5 .* a .* b .* sin(th)
clear
clc
% 2. In the bottom of the ninth inning, the bases are loaded and the
% Braves are down by three runs. Chipper Jones steps to the plate.
% Twice he swings and misses. The crowd heads for the exits. The
% next pitch is a fast ball down the middle. He swings and makes
% perfect contact with the ball, sending it up at a 45-degree angle
% toward the fence 400 ft away.
% a. Write a script to determine how fast he must hit the ball to land
% at the base of the fence, neglecting the air resistance.
% b. Perform a brief experiment to determine whether there was a better
% angle at which to hit the ball so that it could clear a 12 ft fence.
% the formulae required are:
% vertically,
% s = ut + 1/2 a t^2
% s, the distance traveled = 0
% u, the initial velocity is V sin(th)
% a, the acceleration, is -g
% t, the flight time is unknown
% horizontally,
% s = ut
% s is the distance traveled
% u is V cos(th)
% t is provided by the vertical equation
%
% given data:
th = 45 .* pi ./ 180
dist = 400 % ft
g = 32.2 % ft/sec^2
% transforming the vertical equation:
% u - 1/2 gt = 0
% so t = 2u/g = 2Vsin(th)/g
% so the horizontal equation becomes:
% dist = Vcos(th)*t = Vcos(th)*2Vsin(th)/g
% dist = 2V^2sin(th)cos(th)/g
% so V^2 = dist * g / sin(2th)
V = sqrt(dist * g / sin(2*th))
% Considering this equation, when th = 45, sin(2th) is 1, the most it can
% ever be, so this velocity is the least it can be when th is 45 deg.
clear
clc
% 3. If an ice cream cone is 6 inches tall, and its rim has a diameter of 2
% inches, write a script to determine the weight of the ice cream that
% can fit in the cone, assuming that the ice cream above the cone is a
% perfect hemisphere. You may neglect the thickness of the cone
% material. Assume that a gallon of ice cream weighs 8 lb and
% occupies 7.5 cubic feet.
%
% the volume of a cone is 1/3 pi r^2h
% the volume of a hemisphere is 1/2 pi r^r
%
% given:
r = 1 % inches
h = 6 % inches
volume = pi .* r.^2 .* h ./ 3 + pi .* r.^2 ./ 2 % cu in
vol_cu_ft = volume ./ 12.^3
gallons = vol_cu_ft ./ 7.5
weight = gallons * 8 % lb
weight_oz = weight .* 16
clear
clc
% 4. Write a script that validates the relationship between sin u, cos u,
% and tan u by evaluating these functions at suitably chosen values
% of u.
u = 0
diff = sin(u)./cos(u) - tan(u)
u = pi./3
diff = sin(u)./cos(u) - tan(u)
u = 2.*pi./3
diff = sin(u)./cos(u) - tan(u)
u = pi
diff = sin(u)./cos(u) - tan(u)
u = 4.*pi./3
diff = sin(u)./cos(u) - tan(u)
u = 5.*pi./3
diff = sin(u)./cos(u) - tan(u)
u = 2.*pi
diff = sin(u)./cos(u) - tan(u)
clear
clc
% 5. I like my shower to remain hot for hours at 100°F, but am too cheap
% to buy one of those on-demand hot water systems. I don’t care how
% slowly the water runs. The water supply is at 50°F, and the water
% heater is rated at 50,000 BTU/hour. Write a script to compute the
% maximum flow rate of my shower (in cubic feet per minute) that
% keeps the water temperature above 100°F.
%
% given a BTU is the energy necessary to raise a pound of water by 1°F
% and a cubic ft of water weighs 62.4 pounds
% If I am raising the water 50°F, 50,000 BTU per hour will heat 1,000 lb
% of water in an hour
weight = 1000 % lb per hour
volume = weight / 62.5 % cu ft / hour
flow = volume / 60 % cu ft / min
clear
clc
% 6. It takes an average of 45 horsepower to run an electric car at an
% average speed of 35 mph. Write a script to compute the electrical
% storage capacity of the battery system that would make this car
% practical for a 25-mile commute, recharging the batteries only at
% home at night when the electricity is cheap. How many D cell
% alkaline batteries would be needed for this?
% relevant information:
% D cel capacity is approx 15000 mAh at 1.5 volts
% one horsepower is 746 watts
% one watt is one joule per second
% a joule is one amp in a one ohm resistor for one sec
%
% given data:
hp = 45 % horsepower
speed = 35 % miles per hour
dist = 50 % miles
running_time = dist/speed % hours
running_time_sec = running_time * 3600 % sec
power = hp * 746 % watts = joule / sec
energy = power * running_time_sec % joules
recharge = energy / 3600000 % kwh
D_Cell_energy = 15 * 1.5 % watt_hours
D_Cells = ceil(1000 * D_Cell_energy / recharge)
clear
clc
% 7. You want to buy a $300,000 home with 20% down payment. The
% current compound interest rate is 4.5%.
% a. Write a script to determine:
% • the monthly payments for a 30-year loan,
% • the equivalent simple interest rate,
% • the total interest paid over the life of the loan.
% b. Now, repeat the computation for a 15-year loan at 5%. Is this a
% better deal?
%
% Formula:
% payment = r * PV / (1 - (1+r)^(-n))
% PV is the loan principal
% r is the rate per period
% n is the number of months
cost = 300000 % $
downpayment = cost .* 0.2
PV = cost - downpayment
interest_rate = 4.5 % %
n = 360
r = interest_rate ./ (12 * 100)
payment = r .* PV ./ (1 - (1+r).^(-n))
total_interest = payment .* n - PV
simple_interest = 100 .* total_interest ./ (PV * 30)
interest_rate = 5 % %
n = 180
r = interest_rate ./ (12 * 100)
payment = r .* PV ./ (1 - (1+r).^(-n))
total_interest = payment .* n - PV
simple_interest = 100 .* total_interest ./ (PV * 15)
clear
clc
% 7. You want to buy a $300,000 home with 20% down payment. The
% current compound interest rate is 4.5%.
% a. Write a script to determine:
% • the monthly payments for a 30-year loan,
% • the equivalent simple interest rate,
% • the total interest paid over the life of the loan.
% b. Now, repeat the computation for a 15-year loan at 5%. Is this a
% better deal?
%
% Formula:
% payment = r * PV / (1 - (1+r)^(-n))
% PV is the loan principal
% r is the rate per period
% n is the number of months
cost = 300000 % $
downpayment = cost .* 0.2
PV = cost - downpayment
interest_rate = 4.5 % %
n = 360
r = interest_rate ./ (12 * 100)
payment = r .* PV ./ (1 - (1+r).^(-n))
total_interest = payment .* n - PV
simple_interest = 100 .* total_interest ./ (PV * 30)
interest_rate = 5 % %
n = 180
r = interest_rate ./ (12 * 100)
payment = r .* PV ./ (1 - (1+r).^(-n))
total_interest = payment .* n - PV
simple_interest = 100 .* total_interest ./ (PV * 15)
clear
clc
% 9. A glass has the shape of a truncated cone of height 5 inches. Its top
% diameter is 3.5 inches, and its base diameter is 2 inches. If water is
% poured into the glass at 2 gallons per minute, write a script to
% calculate how long it takes to fill the glass to the brim. One gallon is
% 7.5 cubic feet.
%
% formula:
% cone volume = 1/3 pi r^2 h
% since the cone sides are straight, there is a linear relationship
% between radius and height
% taper = top_rad - bottom_rad / ht
% overall height of cone = taper * top_rad
% missing_cone_height = taper * bottom_rad
% given:
top_rad = 3.5 ./ 2 % inches
bot_rad = 2 ./ 2 % inches
h = 5 % inches
rate = 2 % galls / min
gall_per_cu_ft = 7.5
% computations
taper = (top_rad - bot_rad) / h
h1 = top_rad .* taper
h2 = bot_rad * taper
v1 = pi .* top_rad.^2 .* h1 ./ 3
v2 = pi .* bot_rad.^2 .* h2 ./ 3
volume = v1 - v2 % cu in
vol_cu_ft = volume ./ 12.^3 % cu ft
vol_galls = vol_cu_ft * gall_per_cu_ft % galls
time = vol_galls / rate % min
time_sec = time .* 60
clear
clc
% 10. You can calculate the aerodynamic drag on an object by the
% formula:
% Drag = 1/2 r V^2 CdS
% The air density, r, is 1.3 kg/m^3 and the value of the drag area, CdS,
% is a measure of the resistance of the object as it moves through the
% air. An object falling through air reaches terminal velocity when the
% aerodynamic drag equals the object’s weight.
% A sky diver weighing 80 kg has a CdS value of 0.7 when horizontal
% with arms and legs extended, and 0.15 when head down with
% arms and legs in line. One diver jumps from a plane at an
% altitude of 5,000 m in the horizontal position. After 20 sec,
% another diver jumps. Write a script to determine how much
% time the second diver must spend head down in order to catch
% up to the first diver. Also compute the height above the ground
% where they first meet. For simplicity, you may assume that the
% sky divers immediately reach their terminal velocity when
% jumping.
% analysis
% terminal vel ^2 = mass * acc * 2 / (r * CdS)
% units are kg * (m / sec^2) / ((kg/m^3) * m^2)
% = m^2 / sec^2
% once we have each velocity,
% first travels distance dh in t / tv1
% second travels the same distance in (t - 20) / tv2
% meeting time is found by equating these two:
% t*tv1 = (t-20)*tv2
% t = 20*tv2/(tv2-tv1)
% distance fallen = t * tv1
% given
r = 1.3 % kg/m^3
CdS_flat = 0.7 % m^2
CdS_tuck = 0.15 % m^2
h = 5000 % m
dt = 20 % sec
mass = 80 % kg
g = 9.81 % m / sec^2
terminal_vel_1 = sqrt(mass .* g .* 2 ./ (r .* CdS_flat))
terminal_vel_2 = sqrt(mass .* g .* 2 ./ (r .* CdS_tuck))
time = dt .* terminal_vel_2 ./ (terminal_vel_2 - terminal_vel_1)
drop = time * terminal_vel_1
altitude = h - drop
%
clear
clc
close all
% 11. You are given a circle with radius 5 centered at x = 1, y = 2.
% You want to calculate the intersection of some lines with that
% circle. Write a script to find the x and y coordinates of both
% points of intersection. You should test this code at least with
% these lines:
% y = 2 x - 1
% y = -2 x - 10
% y = x + 5.9054
%
% equations
% circle : (x-xc)^2 + (y-yc)^2 = r^2
% line : y = m*x + c
%
% solve for x and y:
% (x-xc)^2 + (m*x + c - yc)^2 - r^2 = 0
% quadratic of the form Ax^2 + Bx + C = 0
% where A = 1 + m^2
% B = -2xc + 2m(c - yc)
% C = xc^2 + (c - yc)^2 - r^2
% roots are x = (-B +/- sqrt(B^2 - 4AC)) / (2A)
xc = 1
yc = 2
r = 5
th = linspace(0, 2*pi);
plot(r*cos(th)+1, r*sin(th)+2)
axis equal
hold on
grid on
m = 1
c = 5.9054
A = 1+m.^2, B = 2.*(m.*c - xc - m.*yc), C = xc.^2 + (c-yc).^2 - r.^2
disc = sqrt(B.^2 - 4.*A.*C);
x1 = (-B + disc) ./ (2.*A), y1 = m*x1 + c
x2 = (-B - disc) ./ (2.*A), y2 = m*x2 + c
plot([x1 x2],[y1, y2], 'r+')
m = 2
c = -1
A = 1+m.^2, B = 2.*(m.*c - xc - m.*yc), C = xc.^2 + (c-yc).^2 - r.^2
disc = sqrt(B.^2 - 4.*A.*C);
x1 = (-B + disc) ./ (2.*A), y1 = m*x1 + c
x2 = (-B - disc) ./ (2.*A), y2 = m*x2 + c
plot([x1 x2],[y1, y2], 'g+')