Array Operations Problem Set 02
Rebecca Arevalo 09/14/2023
clear, clc, close all
Problem 2.13
Initialize variables
clear, clc, close all
A=[9 6; 2 7];
B=[8 9; 6 2];
Solve
ABsum=A+B %sum of A and B
ABsum = 2×2
17 15
8 9
w=A*B %product of A and B
w = 2×2
108 93
58 32
z=B*A %product of B and A
z = 2×2
90 111
58 50
Results
fprintf('The product of AB and BA are not the same.')
The product of AB and BA are not the same.
Problem 2.16
clear, clc, close all
Initialize variables
A=[5 9;6 2];
B=[4 7;2 8];
Solve
C=A./B %array quotient
C = 2×2
1.2500 1.2857
1
3.0000 0.2500
D= B./A %array quotient
D = 2×2
0.8000 0.7778
0.3333 4.0000
E=A.\B %array quotient
E = 2×2
0.8000 0.7778
0.3333 4.0000
F= B.\A %array quotient
F = 2×2
1.2500 1.2857
3.0000 0.2500
Results
fprintf('The quotient of D&E are the same and the quotient of C&F are the same.')
The quotient of D&E are the same and the quotient of C&F are the same.
Problem 2.19
clear, clc, close all
Initialize variables
x=0:1:16;
y=4*cos(x)./x+exp(-0.75*x);
Plot function
plot(y)
2
Problem 2.26
clear, clc, close all
Two divers start at the surface and establish a coordinate system:
x is to the west
y is to the north
z is down.
Diver 1 swims 100 ft east, then 30ft south, then dives 40ft. At the same time, diver 2 dives 30ft, swims east 40
ft, and then south 60ft.
a.compute the distance between diver 1 and the straight point.
b. how far in each direction must diver 1 swim ot reach diver 2?
c.How far in a straight line must diver 1 swim to reach diver 2?
initialize variables
r=[-100,-30, 40]; %diver 1
w=[-40,-60,30]; %diver 2
a. Diver distance 1 from straight point.
3
dist1=norm(r); %this is the magnitude, the distance from the origin
b. how far in each direction must diver 1 swim ot reach diver 2?
v=w-r; %vector of diver 1 to diver 2
c.How far in a straight line must diver 1 swim to reach diver 2?
dist2=norm(v); %straight line distance between the divers
Display results
fprintf('The distance of diver 1 from the origin is %f feet',dist1)
The distance of diver 1 from the origin is 111.803399 feet
fprintf('Diver 1 must swim %.1f west, %.1f south, and %.1f down',v)
Diver 1 must swim 60.0 west, -30.0 south, and -10.0 down
fprintf('Diver 1 must swim %.1f feet to reach diver 2.',dist2)
Diver 1 must swim 67.8 feet to reach diver 2.
Problem 2.30
clc, clear, close all
A water tank consists of a cylinderical part of radius r and height h, and a hemispherical top. The tank is to be
contructed to hold 500m^3 of fluid when filled. The surface area of a cylinderical part is 2pirh, and it's volume
is given by 2pir^3/3. The cost to construct the cylinderical part of the tank is $300m^2 of surface are; the
hemisherical part costs $400m^2.
-Plot the cost versus r for 2<=r<=10m.
-determine the radius that results in the least cost.
-Compute the corresponding height h.
Initialize variables
r=2:0.01:10;
Compute total cost
cost_total=600*pi.*r.*((500-2*pi.*r.^3/3)/pi.*r.^2)+800*pi.*r.^2;
plot total cost versus r
plot(r,cost_total)
title("Total Cost vs. Radius"),xlabel('radius'),ylabel('cost total'),grid on
4
radius that results in the least cost
[least_cost, radius]=min(cost_total);
Corresponding height for the least cost and radius
h=(500-2*pi*radius.^3./3)*pi*radius.^2;
Display results
fprintf('The lest cost is %.01f and the coresponding radius is %.01f and the height
is%f',least_cost,radius,h)
The lest cost is -956385734.0 and the coresponding radius is 801.0 and the height is-2169556002536760.750000
5