You are on page 1of 5

% Name:

% Date:
% Test #2

%% Problem 1 - 5 points
% Prompt the user to enter a value of x, and then calculate the value of
% cos(x).
Solution
x=input('Enter a value of x: ');
cos(x)
Output:
Enter a value of x: 23
ans =
-0.5328

%% Problem 2 - 10 points
%Given the following matrix c, find the values that are greater than 15 and
%less than 35.
% c=[1 10 42 6
% 5 8 78 23
% 56 45 9 13
% 23 22 8 9];
Solution
c=[1 10
5

42
78

56

45

23

22

6
23
13
9];

a=find(c>15 & c<35);


c(a)
Outputs:
a=
4
8

14
c(a)
ans =
23
22
23

%% Problem 3 - 10 points
% Solve the following systems of equations, using both matrix left division
% and the inverse matrix method:
% 5x + 3y - z = 10
% 3x + 2y + 2z = 4
% 4x - y + 3z = 12
Solution:
%matrix inverse method
%AX = B
A = [5 3 -1
322
4 -1 3];
B = [10
4
12];
X = A^-1;
X = X* B;
X
%matrix left division
A = [5 3 -1
322
4 -1 3];
B = [10
4
12];

Y= A\B

%% Problem 4 - 20 points
%Create a program that prompts the user to enter his or her number of
%credit hours. Use the if/elseif structure to determine when registration
%for fall semester begins for each group. Use the fprintf function to
%create the output to the command window.
% Credit Hours Registration Begins
% 0-3 April 16
% 4-23 April 15
% 24-44 April 14
% 45+ April 13
Solution
c_hr = input('Enter credit hours');
if c_hr >=0 && c_hr <4
fprintf('April 16');
elseif c_hr >=4 && c_hr <24
fprintf('April 15');
elseif c_hr >=24 && c_hr <45
fprintf('April 14');
elseif c_hr > 45
fprintf('April 13');
else
fprintf('Invalid input')
end

%% Problem 5 - 10 points
% Create a table that converts degrees to radians using a for loop.
Solution:
fprintf('Degree to Radians\n');
fprintf('Degree
Radians\n');
for degrees = 0:5:300
radians = (pi./180).*degrees;
fprintf('%6.0f %6.2f \n',degrees,radians);
end

%% Problem 6 - 15 points
% Near the end of a concrete design project, the total weight of
% reinforcing steel is required. This information is typically summarized
% in a reinforcing steel schedule as shown below.

% The final weight of steel is calculated by multiplying each bar's weight


% by its length, and add them all up. For example, bar #3 would have a
% weight of:
%
% bar3_weight=0.376*11 lbs
%
% For the following data, calculate the total weight of steel needed
% for this reinforced concrete project. To make sure you have the correct
% value, use two different methods.
% Bar Weight Length
% Size (lbs/ft) (ft)
% 3 0.376 11
% 4 0.668 20
% 5 1.043 22
% 6 1.502 45
% 7 2.044 64
% 8 2.670 57
% 9 3.400 60
% 10 4.303 20
% 11 5.313 10
% 14 7.650 0
% 18 13.600 0
Solution
S = [3 0.376 11
4 0.668 20
5 1.043 22
6 1.502 45
7 2.044 64
8 2.670 57
9 3.400 60
10 4.303 20
11 5.313 10
14 7.650 0
18 13.600 0];
sum = 0;
%method 1
for i=1:1:11
sum = sum + (S(i,2)*S(i,3));
end
TotalWeight_method1 = sum
%method 2

TotalWeight_method2 = (S(1,2)*S(1,3))+(S(2,2)*S(2,3))+(S(3,2)*S(3,3))+
(S(4,2)*S(4,3))+(S(5,2)*S(5,3))+(S(6,2)*S(6,3))+(S(7,2)*S(7,3))+
(S(8,2)*S(8,3))+(S(9,2)*S(9,3))+(S(10,2)*S(10,3))+(S(11,2)*S(11,3))

%% Problem 7 - 5 points
% Prompt the user to enter his or her favorite color. If for example,
% the user enters blue when prompted for his or her favorite color,
% your display should read: Your favorite color is blue

Solution:
str = input('Enter your favourite color','s');
fprintf('your favourite color is %s',str);

%% Problem 8 - 20 points
% The value of e^x can be approximated as:
%
% e^x = 1 + x + x^2/2! + x^3/3! + x^4/4! + ...
%
% Using a while loop, determine the number of iterations (k) necessary to
% calculate e^x to within 0.0001 of the "true" value of e^x for x = 5.
% Also, display the estimate of e^x calculated, and display the
% "true" value of e^x (or exp(x)).
% Hint: The factorial ("!") function in Matlab is "factorial(n)"
Solution
x=5; %value to compute exponent
calc_exp = 0;
real_exp = exp(x);
k=0; %store iteration
while (real_exp-calc_exp > 0.0001 )
calc_exp = calc_exp + ((x^k)/factorial(k));
k = k+1;
end
fprintf('Real exponent of %f = %8.8f \n',x,real_exp);
fprintf('Calculated exponent of %f = %8.8f \n',x,calc_exp);
fprintf('# of iterations =%f\n',k);

You might also like