EGR1302
Matlab Homework #1 Solution
Problem 1-1
Script file:
% HW1_problem_1.m: Chapter 1 homework problem 1
% Homework #ML1 EGR 1302
% Programmer: Carolyn Skurla
% Date: 03/16/2009
% Last Modified: 03/16/2009
% This program is used to solve problem 1 on p61.
%
% Clear workspace and command window
clear
clc
% Problem 1-1
disp('Problem 1-1');
% Given values
x=10;
y=3;
% Functions
u=x+y
v=x*y
w=x/y
z=sin(x)
r=8*sin(y)
s=5*sin(2*y)
MATLAB Command Log:
Problem 1-1
u =
13
v =
30
w =
3.3333
z =
-0.5440
r =
1.1290
s =
-1.3971
Problem 1-5
Script file:
% HW1_problem_5.m: Chapter 1 homework problem 5
% Homework #ML1 EGR 1302
% Programmer: Carolyn Skurla
% Date: 03/16/2009
% Last Modified: 03/16/2009
% This program is used to solve problem 5 on p62.
%
% Clear workspace and command window
clear
clc
% Problem 1-5
disp('Problem 1-5');
% Given values
a=1.12;
b=2.34;
c=0.72;
d=0.81;
f=19.83;
% Functions
x=1+a/b+c/f^2
s=(b-a)/(d-c)
r=1/(1/a + 1/b + 1/c + 1/d)
y=a*b/c*((f^2)/2)
MATLAB Command Log:
Problem 1-5
x =
1.4805
s =
13.5556
r =
0.2536
y =
715.6766
Problem 1-7
Script file:
% HW1_problem_7.m: Chapter 1 homework problem 7
% Homework #ML1 EGR 1302
% Programmer: Carolyn Skurla
% Date: 03/16/2009
% Last Modified: 03/16/2009
% This program is used to solve problem 7 on p62.
%
% Clear workspace and command window
clear
clc
% Problem 1-7
disp('Problem 1-7');
% Given - radius of a sphere
r=5;
% Calculate volume of a sphere with radius r
V=(pi*4*r^3)/3;
% Increase volume of sphere by 30%
V=1.3*V;
% Calculate radius of larger sphere
r=((3*V)/(pi*4))^(1/3)
MATLAB Command Log:
Problem 1-7
r =
5.4570
Problem 1-12
Script file:
% HW1_problem_12.m: Chapter 1 homework problem 12
% Homework #ML1 EGR 1302
% Programmer: Carolyn Skurla
% Date: 03/16/2009
% Last Modified: 03/16/2009
% This program is used to solve problem 12 on p63.
%
% Clear workspace and command window
clear
clc
% Problem 1-12
disp('Problem 1-12');
% December conditions
T1=273.2-15; %Temperature in Kelvin
V1=28500; % Volume of storage tank in cubic feet
% July temperature
T2=273.2+31; % Temperature in Kelvin
% Volume of storage tank at T2
V2=(T2*V1)/T1
MATLAB Command Log:
Problem 1-12
V2 =
3.3577e+004
Problem 1-13
Script file:
% HW1_problem_13.m: Chapter 1 homework problem 13
% Homework #ML1 EGR 1302
% Programmer: Carolyn Skurla
% Date: 03/16/2009
% Last Modified: 03/16/2009
% This program is used to solve problem 13 on p63.
%
% Clear workspace and command window
clear
clc
%Problem 1-13
disp('Problem 1-13');
% Given values of x
x=[1:0.2:5];
% Calculate function y(x)
y=7*sin(4*x);
% Calculate number of elements in y
length(y)
% Display value of 3rd element in y
y(3)
MATLAB Command Log:
Problem 1-13
ans =
21
ans =
-4.4189
Problem 1-19
Script File:
% HW1_problem_19.m: Chapter 1 homework problem 19
% Homework #ML1 EGR 1302
% Programmer: Carolyn Skurla
% Date: 03/16/2009
% Last Modified: 03/16/2009
% This program is used to solve problem 19 on p64.
%
% Clear workspace and command window
clear
clc
%Problem 1-19
disp('Problem 1-19');
% Coeffients of polynomial from highest power of x
a=[36,12,-5,10];
% Calculate roots of polynomial
roots(a)
MATLAB Command Log:
Problem 1-19
ans =
-0.8651
0.2659 + 0.5004i
0.2659 - 0.5004i
Problem 1-27
Script File:
% HW1_problem_27.m: Chapter 1 homework problem 27
% Homework #ML1 EGR 1302
% Programmer: Carolyn Skurla
% Date: 03/16/2009
% Last Modified: 03/16/2009
% This program is used to solve problem 27 on p65.
%
% Clear workspace and command window
clear
clc
%Problem 1-27
disp('Problem 1-27');
% Given
a=95; b=-50; c=145;
% Coefficients of 3 equations
A=[7,14,-6; 12,-5,9; -5,7,15];
% Place given values in column vector
d=[a;b;c];
% Solve for x,y,z
A\d
MATLAB Command Log:
Problem 1-27
ans =
-3
10
4
Problem 1-30
Script File:
% HW1_problem_30.m: Chapter 1 homework problem 30
% Homework #ML1 EGR 1302
% Programmer: Carolyn Skurla
% Date: 03/16/2009
% Last Modified: 03/16/2009
% This program is used to solve problem 30 on p65.
%
% Clear workspace and command window
clear
clc
%Problem 1-30
disp('Problem 1-30');
% Area of enclosure (square feet)
A=1600;
% Initialize array address variable
k=0;
% Calculate radius R, length L, and cost C for a range of x values
% Increment x by 0.01 feet to get required resolution
% In this case, I selected to calculate cost from R=5 to R=30 feet
for x=5:0.01:30
% increment array address by 1
k=k+1;
% set radius of curved section of fence to x
R(k)=x;
% calculate length of rectanglular part of fence
L(k)=(A-(pi*R(k)^2)/2)/(2*R(k));
% calculate cost of fence ($30/ft for straight sides & $40/ft for
% curves
C(k)=(2*L(k)+2*R(k))*30 + (pi*R(k))*40;
end
% Find element of cost function with minimum value
mincost=min(C)
% Initiate array address variables for next logic loop
g=0; h=0;
% Logic loop to find array address of minimum cost. Find radius that
% corresponds to the minimum cost fence.
while g==0
h=h+1;
if C(h)==mincost
minr=R(h);
g=1;
elseif h==length(C)
g=1;
end
end
% Display radius of minimum cost fence
minr
% Calculate length of rectangular part of minimum cost fence
minL=(A-(pi*minr^2)/2)/(2*minr)
% Calculate dimensions and cost of fence -20% change in R
r1=0.8*minr
L1=(A-(pi*r1^2)/2)/(2*r1)
C1=(2*L1+2*r1)*30 + (pi*r1)*40
Diff1=((C1-mincost)/mincost)*100
% Calculate dimensions and cost of fence with +20% change in R
r2=1.2*minr
L2=(A-(pi*r2^2)/2)/(2*r2)
C2=(2*L2+2*r2)*30 + (pi*r2)*40
Diff1=((C2-mincost)/mincost)*100
% Plot radius vs cost function
plot(R,C),xlabel('Radius (ft)'),ylabel('Cost ($)'),title('Problem 1-30: Cost of Fence')
MATLAB Command Log:
Problem 1-30
mincost =
5.1575e+003
minr =
18.6100
minL =
28.3714
r1 =
14.8880
L1 =
42.0415
C1 =
5.2867e+003
Diff1 =
2.5045
r2 =
22.3320
L2 =
18.2835
C2 =
5.2433e+003
Diff1 =
1.6630
MATLAB Plot:
Problem 1-38
Script File:
% HW1_problem_38.m: Chapter 1 homework problem 38
% Homework #ML1 EGR 1302
% Programmer: Carolyn Skurla
% Date: 03/16/2009
% Last Modified: 03/16/2009
% This program is used to solve problem 38 on p65.
%
% Clear workspace and command window
clear
clc
%Problem 1-38
disp('Problem 1-38');
% Initialize array address
z=1;
% Set the first element of x array to 0. Calculate y.
x(z)=0; %seconds
y(z)=10*(1-exp(-1*x(z)/4)); %Newtons
% Increment x by 0.01 and calculate y until y>=9.8
while y(z)< 9.8
z=z+1;
x(z)=x(z-1)+0.01;
y(z)=10*(1-exp(-1*x(z)/4));
end
% Display final values of x and y
x(z)
y(z)
% Plot x versus y
plot(x,y),xlabel('Time (s)'),ylabel('Force (N)'),title('Problem 1-38')
MATLAB Command Log:
Problem 1-38
ans =
15.6500
ans =
9.8001
MATLAB Plot: