Chapter7

Click here to see book problems

Problem 1
Script file:

%problem 1
disp(‘Problem 1’)
disp(‘Part (a)’)
x=[-2 4];
y=math(x);
disp(‘The test values for y(x) are:’)
disp(y)
%
%part b
figure(1)
x=-4:.1:8;
plot(x,math(x));
title(‘y(x)=(0.6x^3)e^{-0.47x} + (1.5x^2)e^{-0.6x}’)
xlabel(‘x-axis’)
ylabel(‘y-axis’)



Function file problem 1:
function y = math(x)
y=(0.6*x.^3).*exp(-0.47*x)+ (1.5*x.^2).*exp(-0.6*x);
end

Problem 2
Script file:

%problem 2
disp(‘Problem 2’)
fprintf(‘Part (a)’)
th=[pi/6, 5*pi/6];
r=polarmath(th);
disp(‘The test values for r(theta) are:’)
disp(r)
%
%part b
figure(2)
th=linspace(0,4*pi,200);
polar(th,polarmath(th));
title(‘r(\theta)=4cos(4sin(\theta))’)

Function file problem 2:
function r = polarmath(theta)
%angles in radians
r=3*sin(3*cos(0.5*theta));
end

Problem 3
Script file:

%problem 3
disp(‘Problem 3’)
format shortg
disp(‘Part (a)’)
mpg=21;
Lkm = mpgToLpkm(mpg) ;
disp(‘The fuel efficiency in litters per 100 km of a car is:’)
disp(Lkm)
disp(‘Part (b)’)
mpg=36;
Lkm = mpgToLpkm(mpg);
disp(‘The fuel efficiency in litters per 100 km of a car is:’)
disp(Lkm)

Function file problem 3:
function Lkm = mpgToLpkm(mpg)
Lkm = mpg*4.40488/1.609347;
end

Problem 4
Script file:

%problem 4
disp(‘Problem 4’)
disp(‘Part (a)’)
psi = 120;
Pa = PsiToPa(psi);
fprintf(‘%d psi are equal to %d Pa.\n’, psi, round(Pa) )
disp(‘Part (b)’)
psi =3000;
Pa = PsiToPa(psi);
fprintf(‘%d psi are equal to %d Pa.\n’, psi, round(Pa))

Function file problem 4:
function Pa = PsiToPa(psi)
Pa = psi*6894.75729;
end

Problem 5
Script file:

%problem 5
disp(‘Problem 5’)
disp(‘Part (a)’)
den=8960;
sw = DenTOSw(den);
disp(‘The specific weight of cooper in lb/in^3 is:’)
disp(sw)
disp(‘Part (b)’)
den=2340;
sw = DenTOSw(den);
disp(‘The specific weight of concrete in lb/in^3 is:’)
disp(sw)



Function file problem 5:
function sw = DenTOSw(den)
sw=den/2.76799e4;
end

Problem 6
Script file:

%problem 6
disp(‘Problem 6’)
Nm = 2000;
lbft = NmTOlbft(Nm);
fprintf(‘%d N-m is equal to %d lb-ft.\n’,Nm, round(lbft))

Function file problem 6:
function lbft = NmTOlbft(Nm)
lbft = Nm*0.737562;
end

Problem 7
Script file:

%problem 7
disp(‘Problem 7’)
disp(‘Part (a)’)
w=170;
h=70;
BSA = BodySurA(w,h);
fprintf(‘The body surface area of a %.0f lb, %.2f in patient is %.3f m^2\n’,w,h,BSA)
disp(‘Part (b)’)
w=220;
h=77;
BSA = BodySurA(w,h);
fprintf(‘The body surface area of a %.0f lb, %.2f in patient is %.3f m^2\n’,w,h,BSA)

Function file problem 7:
function BSA = BodySurA(w,h)
BSA = sqrt(h*w/3131);
end

Problem 8
Script file:

%problem 8
disp(‘Problem 8′)
y1=0:.1:24;
VolOfFuel=Volfuel(y1);
figure(3)
plot(y1,VolOfFuel,’b’)
title(‘Volume of fuel as a function of height’)
xlabel(‘Height’)
ylabel(‘Volume of Fuel’)

Function file problem 8:
function [ V ] = Volfuel( y )
V=((pi/3).*(y.^2).*((3.*(0.6096.*24))-(y)).*(264.172));
end

Problem 9
Script file:

%problem 9
disp(‘Problem 9’)
disp(‘Part (a)’)
r1=2;
R1=3.5;
h1=4.25;
[V1,S1]= VolSArea(r1,R1,h1);
fprintf(‘The Volume of a cup with a lower radius of %.0f inches and upper radius of %.1f inches and a height of %.2f inches is %.2f fl oz\n’,r1,R1,h1,V1)
fprintf(‘The Surface Area of a cup with a lower radius of %.0f inches and upper radius of %.1f inches and a height of %.2f inches is %.2f inches^2\n’,r1,R1,h1,S1)

disp(‘Part (b)’)
r2=2.5;
R2=3.5;
h2=4.5;
[V2,S2]= VolSArea(r2,R2,h2);
fprintf(‘The Volume of a cup with a lower radius of %.0f inches and upper radius of %.1f inches and a height of %.1f inches is %.2f fl oz\n’,r2,R2,h2,V2)
fprintf(‘The Surface Area of a cup with a lower radius of %.0f inches and upper radius of %.1f inches and a height of %.1f inches is %.2f inches^2\n’,r2,R2,h2,S2)

Function file problem 9:
function [V,S] = VolSArea(r,R,h)

S = pi.*(R+r).*sqrt(((R-r).^2)+(h.^2));

V=((pi/3).*(h).*((R.^2)+(r.^2)+(R*r)))/(0.554113);

end

Problem 10
Script file:

%problem 10
disp(‘Problem 10’)
disp(‘Part (a)’)
TdbA=75;
TwbA=69;
RHA=RelHum(TdbA,TwbA);
fprintf(‘The Relative humidity is when the dry bulb is %.0f degrees fahrenheit and the wet bulb is %.0f degrees fahrenheit is %.0f%%\n’,TdbA,TwbA,RHA)

disp(‘Part (b)’)
TdbB=93;
TwbB=90;
RHB=RelHum(TdbB,TwbB);
fprintf(‘The Relative humidity is when the dry bulb is %.0f degrees fahrenheit and the wet bulb is %.0f degrees fahrenheit is %.0f%%\n’,TdbB,TwbB,RHB)



Function file problem 10:
function [ RH ] = RelHum( Tdb,Twb )
VP=exp(((16.78.*Twb)-(116.9))/(Twb+237.3))-0.066858.*(1+0.00115.*Twb).*(Tdb-Twb);
SVP=exp(((16.78.*Tdb)-(116.9))/(Tdb+237.3));
RH=((VP)./(SVP)).*100;
end

Problem 11
Script file:

%problem 11
disp(‘Problem 11’)
G = [5 4 0 3 4 5 2 5];
C = [4 3 3 2 3 4 3 3];
gpa = round(GradePtAve(G,C),1);
fprintf(‘The GPA for the student is %.1f\n\n’,gpa)

Function file problem 11:
function [GPA] = GradePtAve(G,C)
GPA = sum(G.*C)/sum(C);
end

Problem 12
Script file:

%problem 12
disp(‘Part (a)’)
A=[-5 -1 6];
B=[2.5 1.5 -3.5];
C=[-2.3 8 1];
th = anglines(A,B,C);
fprintf(‘The angle between the points is %.1f degrees\n\n’,th)
disp(‘Part (b)’)
A=[-5.5 0];
B=[3.5,-6.5];
C=[0,7];
th = anglines(A,B,C);
fprintf(‘The angle between the points is %.1f degrees\n\n’,th)

Function file problem 12:
function [th] = anglines(A,B,C)
BA = A-B; BC = C-B;
th=acosd(dot(BA,BC)/(sqrt(sum(BA.^2))*sqrt(sum(BC.^2))));
end

Problem 13
Script file:

%problem 13
disp(‘Part (a)’)
A = [5 37];
a1 = 0;%0 for AM
B = [2 51];
b2 = 1;%1 for PM
timeElapsed = timediff(A, a1, B, b2);
fprintf(‘Time elapsed is %d hr and %d min.\n\n’,timeElapsed(1), timeElapsed(2))

disp(‘Part (b)’)
A = [12 53];
a1 = 1;%1 for PM
B = [6 12];
b2 = 1;%1 for PM
timeElapsed = timediff(A, a1, B, b2);
fprintf(‘Time elapsed is %d hr and %d min.\n\n’,timeElapsed(1), timeElapsed(2))
disp(‘Part (c)’)
A = [11 32];
a1 = 1;%1 for PM`
B = [3 18];
b2 = 1;%1 for PM
timeElapsed = timediff(A, a1, B, b2);
fprintf(‘\nTime elapsed is %d hr and %d min.\n\n’,timeElapsed(1), timeElapsed(2)



Function file problem 13:
function [dt] = timediff(TA,ap1,TB,ap2)

if ap2 == 0 && ap1 == 0 %if they are in the morning just substract vectos
if TA(2)<=TB(2)% event A 3:23, event B 6:30
dt = TB – TA;
end
if TA(1)>TB(1)
min = (60 – TA(2)) + TB(2);
hr = TB(1)-(TA(1)+1);
dt = [hr min];
end

end
if ap2 == 1 && ap1 == 0
min1 = 60 – TA(2);
hr1 = 12-(TA(1)+1);
min2 = min1 + TB(2);
if min2 > 60
min2 = min2-60;
hr1 = hr1 +1;
hr1 = hr1 + TB(1);
end
dt = [hr1 min2];
end
if ap2 == 1 && ap1 == 1
if TA(2)<=TB(2)
dt = TB – TA;
end
if TA(2)>TB(2)
min = (60 – TA(2)) + TB(2);
hr = TB(1)-(0+1);
dt = [hr min];
end
end
if ap1 == 0 && ap2 == 0
if TA(1)>TB(1)
fprintf(‘Error situation’)
end
end
if ap1 == 1 && ap2 == 1
if TA(1)>TB(1) && (TA(1)~=12)
fprintf(‘Error situation’)
end
end
end

Problem 14
Script file:

%problem 14
disp(‘problem 14’)
disp(‘Part (a)’)
A=[-0.7 2.1];
B=[9 18];
n=unitvec(A,B);
disp(‘The unit vector is:’)
disp(n)
disp(‘Part (b)’)
A=[10 -3.5 -2.5];
B=[-11 6.5 5.9];
n=unitvec(A,B);
disp(‘The unit vector is:’)
disp(n)

Function file problem 14:

%Function file problem 14:
function n=unitvec(A,B)
n=(B-A)/sqrt(sum((B-A).^2));
end

Problem 15
Script file:

disp(‘problem 15’)
disp(‘Part (a)’)
a=[3 11]; b=[14 -7.3];
r=crosspro(a,b);
disp(‘The cross product vector is:’)
disp(r)
disp(‘Part (b)’)
c=[-6 14.2 3]; d=[6.3 -8 -5.6];
s=crosspro(c,d);
disp(‘The cross product vector is:’)
disp(s)

Function file problem 15:

function w = crosspro(u,v)
n=length(u);
if n == 2
u(3)=0;
v(3)=0;
end
w(1)=u(2)*v(3)-u(3)*v(2);
w(2)=u(3)*v(1)-u(1)*v(3);
w(3)=u(1)*v(2)-u(2)*v(1);
end

Problem 16
Script file:

%problem 16
disp(‘problem 16’)
disp(‘Part (a)’)
A=[1,2]; B=[10,3]; C=[6,11];
Area = TriArea(A,B,C);
fprintf(‘The area of the triangle is %.1f\n\n’,Area)
disp(‘Part (b)’)
A=[-1.5, -4.2, -3]; B=[-5.1, 6.3, 2]; C=[12.1, 0, -1.5];
Area = TriArea(A,B,C);
fprintf(‘The area of the triangle is %.1f\n\n’,Area)

Function file problem 16:

function Area = TriArea(A,B,C)
[AB AC] = sides(A,B,C);
Area = sqrt(sum(crosspro(AB,AC).^2))/2;
end
function [AB AC] = sides(A,B,C)
AB = B-A;
AC = C-A;
end

function w = crosspro(u,v)
n=length(u);
if n == 2
u(3)=0;
v(3)=0;
end
w(1)=u(2)*v(3)-u(3)*v(2);
w(2)=u(3)*v(1)-u(1)*v(3);
w(3)=u(1)*v(2)-u(2)*v(1);
end

Problem 17
Script file:

disp(‘problem 17’)
A = [1,1
7,2
10,5
9,11
6,12
1,12
-3,8
-4,4];

Area = APolygon(A);
fprintf(‘The Area of the Polygon is: %.0f\n’,Area)

Function file problem 17:

function [ A ] = APolygon( Crd )

[M N]= size(Crd);
n = 0;
TotalPolyArea = 0;
while n<M-2
n=n+1;
TotalPolyArea = TotalPolyArea + TriArea(Crd(n,1:2),Crd(n+1,1:2),Crd(n+2,1:2));
end
A = TotalPolyArea;
end

Problem 18
Script file:

disp(‘problem 18’)
A = [7,1.2]; B= [0.5, 2.6]; C=[-2.4,-1.4];
circle = Circle3Pts(A,B,C)

Function file problem 18:

function [Ct R] = Circle3Pts(A,B,C)
a = [-2*A(1) -2*A(2) 1; -2*B(1) -2*B(2) 1; -2*C(1) -2*C(2) 1];
b = [-(A(1)^2+A(2)^2); -(B(1)^2+B(2)^2); (C(1)^2+C(2)^2)];
x=a\b;
R=sqrt(x(1)^2+x(2)^2-x(3));
R=round(R*100)/100;
Ct = [x(1),x(2)];
Ct = round(Ct*100)/100;
t = linspace(0,pi,100);
xp = Ct(1)+R*cos(t);
ypt=Ct(2)+R*sin(t);
ypb=Ct(2)-R*sin(t);
figure(4)
plot(xp,ypt,’k’,xp,ypb,’k’,A(1), A(2),’*’, B(1),B(2),’*’,C(1),C(2),’*’)
axis equal

end

Problem 19
Script file:

disp(‘problem 19’)
disp(‘Part (a)’)
d=100;
b = Bina(d);
if b>=0
disp(‘The binary decomposition is:’)
disp(b)
end
disp(‘Part (b)’)
d=1002;
b = Bina(d);
if b>=0
disp(‘The binary decomposition is:’)
disp(b)
end
disp(‘Part (c)’)
d=52601;
b = Bina(d);
if b>=0
disp(‘The binary decomposition is:’)
disp(b)
end

disp(‘Part (d)’)
d=2000090;
b = Bina(d);
if b>=0
disp(‘The binary decomposition is:’)
disp(b)
end

Function file problem 19:

function b = Bina(d)
if d>=2^16
b=-1;
fprintf(‘The integer is too large for this routine\n’)
else
n=floor(log(d)/log(2));
b=[];
for k=n:-1:0
p=floor(d/2^k);
b=[b p];
d=d-p*2^k;
end
end
end

Problem 20
Script file:

disp(‘problem 20’)
A=[2.6, 3.2];
B=[11,14.5];
C=[-2,2.8];
TriCirc(A,B,C)

Function file problem 20:

function TriCirc(A,B,C)
midAB=(A+B)/2;
abisectorAB=-(A(1)-B(1))/(A(2)-B(2));
bbisectorAB=midAB(2)-abisectorAB*midAB(1);
midBC=(B+C)/2;
abisectorBC=-(B(1)-C(1))/(B(2)-C(2));
bbisectorBC=midBC(2)-abisectorBC*midBC(1);
mat=[-abisectorAB 1; -abisectorBC 1]; col=[bbisectorAB; bbisectorBC];
center=mat\col; r=sqrt((A(1)-center(1))^2 + (A(2)-center(2))^2)
x=center(1)-r:.01:center(1)+r;
y1=center(2)+sqrt(r^2 – (x-center(1)).^2);
y2=center(2)-sqrt(r^2 – (x-center(1)).^2);
plot(x,y1,x,y2,[A(1) B(1)],[A(2) B(2)],[A(1) C(1)],[A(2) C(2)],[B(1) C(1)],[B(2) C(2)])
axis equal
title(‘Circumscribed Triangle’)
xlabel(‘x–>’)
ylabel(‘y–>’)
end