















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The solutions to Matlab Sheet 4 from the Mechanical Engineering Department at the Faculty of Engineering, Spring 2017. The sheet covers topics such as conditional statements, loops, and the Pythagorean theorem. Students can use this document to check their understanding of these concepts and to verify the correctness of their solutions.
Typology: Lecture notes
1 / 23
This page cannot be seen from the preview
Don't miss anything!
















3
2
Write and run the program to calculate the sum.
The following M-file gives the correct sum:
M-File:
total = 0;
for k = 1:
total(k+1) =total(k)+ 14k^3 - 20k^2 +5*k;
end
disp('The sum for 10 terms is: ')
disp(total(k+1))
Command Window:
The sum for 10 terms is:
The following M-file would not give the correct sum (why?).
M-File:
total = 0;
for k = 1:
total = 14k^3 - 20k^2 +5*k;
end
disp('The sum for 10 terms is: ')
disp(total)
Command Window:
The sum for 10 terms is:
check your results.
a. 𝑧 = 6 > 3 + 8
b. 𝑧 = 6 + 3 > 8
c. 𝑧 = 4 >
d. 𝑧 =
e. 𝑧 = 4 < 7 + 3
f. 𝑧 = ( 4 < 7 ) − 5
g. 𝑧 = 4 < ( 7 ∗ 5 )
h. 𝑧 = 2 ⁄ 5 >= 5
The answers are
(a) z = 0, (b) z = 1, (c) z = 0,
(d) z = 4, (e) z = 1, (f) z = 5,
(g) z = 1, (h) z = 0
using MATLAB. Check the answers with MATLAB.
(a) 𝑦 = 𝑎 − 𝑏 > 𝑎 − 𝑐 < 𝑏
(b) 𝑦 = − 4 < 𝑎 < 0
(c) 𝑦 = 𝑎 − 𝑐 < = 𝑏 > 𝑎 + 𝑐
M-File:
clear, clc
a=-2; b=3; c=5;
disp('Part (a)')
y=a-b>a-c<b
disp('Part (b)')
y=-4<a<
disp('Part (c)')
y=a-c<=b>a+c
disp('Part (d)')
y=3*(c+a~=a/b-b)==(a+c)~=b
Command Window:
Part (a)
y =
Part (b)
y =
the following operations by hand and use MATLAB to check your results.
a. 𝑧 = 𝑦 < ~𝑥
b. 𝑧 = 𝑥 & 𝑦
c. 𝑧 = 𝑥 | 𝑦
M-File:
The answers are
(a) z = [1,1,1,0,0,0], (b) z = [1,0,0,1,1,1]
(c) z = [1,1,0,1,1,1]
with MATLAB
a) - 3&
b) ~5<4&~0> - 3
c) - 2&2>3|8/
M-File:
clear, clc
disp('Part (a)')
disp('Part (b)')
disp('Part (c)')
disp('Part (d)')
Command Window:
Part (a)
ans =
Part (b)
ans =
Part (c)
ans =
Part (d)
ans =
a speed of at an angle A to the horizontal are given by
0
𝑡 sin 𝐴 − 0. 5 g𝑡
2
0
2
0
g𝑡 sin 𝐴 + g
2
2
where g is the acceleration due to gravity. The projectile will strike the ground
when ℎ
= 0 , which gives the time to hit 𝑡
ℎ𝑖𝑡
0
/g
sin 𝐴. Suppose that 𝐴
0
= 40 m/s, and g =9.8 m/s
2
. Use the MATLAB relational and logical
operators to find the times when
a. The height is no less than 15 m.
b. The height is no less than 15 m and the speed is simultaneously no greater
than 36 m/s.
c. The height is less than 5 m or the speed is greater than 35 m/s.
It is helpful to plot the height and speed versus time before answering the
questions, especially part (c). The plot is shown in the figure.
Part C
time (sec) h<5 v>35 h<5|v>35 time (sec) h<5 v>35 h<5|v>35 time (sec) h<5 v>35 h<5|v>
0 TRUE TRUE TRUE 1.508665 FALSE TRUE TRUE 3.017329 FALSE TRUE TRUE
0.040775 TRUE TRUE TRUE 1.549439 FALSE FALSE FALSE 3.058104 FALSE TRUE TRUE
0.081549 TRUE TRUE TRUE 1.590214 FALSE FALSE FALSE 3.098879 FALSE TRUE TRUE
0.122324 TRUE TRUE TRUE 1.630989 FALSE FALSE FALSE 3.139653 FALSE TRUE TRUE
0.163099 TRUE TRUE TRUE 1.671764 FALSE FALSE FALSE 3.180428 FALSE TRUE TRUE
0.203874 TRUE TRUE TRUE 1.712538 FALSE FALSE FALSE 3.221203 FALSE TRUE TRUE
0.244648 TRUE TRUE TRUE 1.753313 FALSE FALSE FALSE 3.261978 FALSE TRUE TRUE
0.285423 FALSE TRUE TRUE 1.794088 FALSE FALSE FALSE 3.302752 FALSE TRUE TRUE
0.32619 8 FALSE TRUE TRUE 1.834862 FALSE FALSE FALSE 3.343527 FALSE TRUE TRUE
0.366972 FALSE TRUE TRUE 1.875637 FALSE FALSE FALSE 3.384302 FALSE TRUE TRUE
0.407747 FALSE TRUE TRUE 1.916412 FALSE FALSE FALSE 3.425076 FALSE TRUE TRUE
0.448522 FALSE TRUE TRUE 1.95 7187 FALSE FALSE FALSE 3.465851 FALSE TRUE TRUE
0.489297 FALSE TRUE TRUE 1.997961 FALSE FALSE FALSE 3.506626 FALSE TRUE TRUE
0.530071 FALSE TRUE TRUE 2.038736 FALSE FALSE FALSE 3.547401 FALSE TRUE TRUE
0.570846 FALSE TRUE TRUE 2.079511 FALSE FALSE FALSE 3.588175 FALSE TRUE TRUE
0.611621 FALSE TRUE TRUE 2.120285 FALSE FALSE FALSE 3.62895 FALSE TRUE TRUE
0.652396 FALSE TRUE TRUE 2.16106 FALSE FALSE FALSE 3.669725 FALSE TRUE TRUE
0.69317 FALSE TRUE TRUE 2.201835 FALSE FALSE FALSE 3.710499 FALSE TRUE TRUE
0.733945 FALSE TRUE TRUE 2.24261 FALSE FALSE FALSE 3.751274 FALSE TRUE TRUE
0.77472 FALSE TRUE TRUE 2.283384 FALSE FALSE FALSE 3.792049 FALSE TRUE TRUE
0.815494 FALSE TRUE TRUE 2.324159 FALSE FALSE FALSE 3.832824 TRUE TRUE TRUE
0.856269 FALSE TRUE TRUE 2.364934 FALSE FALSE FALSE 3.873598 TRUE TRUE TRUE
0.897044 FALSE TRUE TRUE 2.405708 FALSE FALSE FALSE 3.914373 TRUE TRUE TRUE
0.937819 FALSE TRUE TRUE 2.446483 FALSE FALSE FALSE 3.955148 TRUE TRUE TRUE
0.978593 FALSE TRUE TRUE 2.487258 FALSE FALSE FALSE 3.995923 TRUE TRUE TRUE
1.019368 FALSE TRUE TRUE 2.528033 FALSE FALSE FALSE 4.036697 TRUE TRUE TRUE
1.060143 FALSE TRUE TRUE 2.568807 FALSE TRUE TRUE 4.077472 TRUE TRUE TRUE
1.100917 FALSE TRUE TRUE 2.609582 FALSE TRUE TRUE
1.141692 FALSE TRUE TRUE 2.650357 FALSE TRUE TRUE
1.182467 FALSE TRUE TRUE 2.691131 FALSE TRUE TRUE
1.223242 FALSE TRUE TRUE 2.731906 FALSE TRUE TRUE
1.264016 FALSE TRUE TRUE 2.772681 FALSE TRUE TRUE
1.304791 FALSE TRUE TRUE 2.813456 FALSE TRUE TRUE
1 .345566 FALSE TRUE TRUE 2.85423 FALSE TRUE TRUE
1.38634 FALSE TRUE TRUE 2.895005 FALSE TRUE TRUE
1.427115 FALSE TRUE TRUE 2.93578 FALSE TRUE TRUE
1.46789 FALSE TRUE TRUE 2.976555 FALSE TRUE TRUE
by 13 and by 16 whose square root is greater than 120. Use a loop in the
program. The loop should start from 1 and stop when the number is found. The
program prints the message “The required number is:” and then prints the
number.
M-File:
clear, clc
i=0;
s=0;
while s<=
i=i+1;
if rem(i,2)==0 && rem(i,13)==0 && rem(i,16)==
s=sqrt(i);
end
end
fprintf('The required number is: %i\n',i)
Command Window:
The required number is: 14560
Command Window:
For the equation ax^2+bx+c
Enter a: 3
Enter b: 6
Enter c: 3
The equation has one root,
For the equation ax^2+bx+c
Enter a: - 3
Enter b: 4
Enter c: - 6
The equation has no real roots.
For the equation ax^2+bx+c
Enter a: - 3
Enter b: 7
Enter c: 5
The equation has two roots,
2
2
2
. Write a MATLAB program
in a script file that finds all the combinations of triples a, b, and c that are positive
integers all smaller or equal to 50 that satisfy the Pythagorean theorem. Display
the results in a three-column table in which every row corresponds to one triple.
The first three rows of the table are:
M-File:
clear, clc
id=1;
for k=1:
for j=k+1:
for i=j+1:
if i^2==k^2+j^
a(id)=k;
b(id)=j;
c(id)=i;
id=id+1;
end
end
end
end
table=[a' b' c']
Command Window:
table =
systems and vehicle suspensions, for example. The springs exert a force that is
proportional to their compression, and the proportionality constant is the
spring constant k. The two side springs provide additional resistance if the
weight W is too heavy for the center spring. When the weight W is gently placed,
it moves through a distance x before coming to rest. From statics, the weight
force must balance the spring forces at this new position. Thus
1
1
2
Figure 1.
These relations can be used to generate the plot of 𝑥 versus 𝑊.
a. Create a function file that computes the distance x, using the input
parameters 𝑊, 𝑘
1
2
, and d. Test your function for the following two
cases, using the values 𝑘
1
4
N/m; 𝑘
2
4
N/m; 𝑑 = 0.1 m.
b. Use your program to plot 𝑥 versus 𝑊 for 0 ≤ 𝑊 ≤ 3000 N for the values
of 𝑘
1
2
and 𝑑 given in part a.
M-File:
(a) The function file is
function package(W,k1,k2,d);
if W < k1*d
x = W/k1;
else
x = (W+2k2d)/(k1+2*k2);
end
disp(‘The distance traveled is:’)
disp(x)
The session is
package(500,10000,15000,0.1)
‘The distance traveled is:’
package(2000,10000,15000,0.1)
‘The distance traveled is:’
(b) The function file in part (a) must be modified to provide an output
variable x and to eliminate the display statements. It is
function x = package(W,k1,k2,d);
if W < k1*d
x = W/k1;
else
x = (W+2k2d)/(k1+2*k2);
end
Note that the inputs must be scalars because of the if statement. Thus the
following script file would not give the correct plot. See the text for a
discussion of this pitfall.
plot(W,package(W,10000,15000,0.1)
The correct script file to obtain the plot is the
following.
k1 = 10000;k2 = 15000;d = 0.1;
if k1*d <= 3000
W = [0,k1*d,3000];
x = [package(W(1),k1,k2,d),package(W(2),k1,k2,d),...
package(W(3),k1,k2,d)];plot(W,x,0- 0),...
xlabel(‘Weight (N)’),ylabel(‘Distance (m)’),...
title(‘k1 = 10,000, k2 = 15,000, d = 0.1’)
else
x = [package(W(1),k1,k2,d),package(W(2),k1,k2,d)];
plot(W,x,0-0),xlabel(‘Weight (N)’),...
ylabel(‘Distance (m)’),title(‘k1 = 10,000, k2 =
15,000, d = 0.1’)
end
machinery, which consists of lathes, grinders, and milling machines. The
number of hours on each machine required to produce a product is given in the
following table, along with the number of hours available per week on each type
of machine. Assume that the company can sell everything it produces. The profit
per item for each product appears in the last line of the table.
M-File:
unit_profit = [100, 150, 90, 120];
hours = [1,2,0.5,3;0,2,4,1;3,1,5,2];
p = [0,0,0,0];
max_profit = 0;
another = [];
for p1 = 0:
for p2 = 0:
for p3 = 0:
for p4 = 0:
p = [p1,p2,p3,p4];
if hours*p0 <= [40, 30, 45]
profit = unit_profit*p0;
else
profit = 0;
end
if profit > max_profit
max_profit = profit;
production = [p1,p2,p3,p4];
elseif (max_profit - profit)<=
another = [another;p];
P = [P,profit];
end
end
end
end
end
disp(0Optimal production is:0)
disp(production)
disp(0The profit is:0)
disp(max_profit)
[m,n] = size(another);
if m>
disp(0The number of answers giving the same profit
is:0)
disp(m-1)
disp(0The other possible answers are:0)
disp(another(2:m,:))
disp(0Other profit:0)
disp(P(2:m)0)
else
disp(0There is only one solution.0)
end
The results are that the optimal production is given by the vector [10 15 0 0].
Thus we should manufacture 10 units of product 1, 15 units of product 2, and
no units of products 3 and 4. The profit is $3250. There is only one solution.
(b) If we produce 9, 14, 0, and 0 units of each product, the profit would be
9(100) + 14(150) = $3000. If we produce 11, 16, 1, and 1 units of each
product, the profit would be 11(100) + 16(150) + 1(90) + 1(120) = $3710, but
this would require 46.5 hours on the lathe, 37 hours on the grinder, and 56
hours on the milling machine, which is more than the available hours.
3
, is in
iterations. The process starts by choosing a value 𝑥
1
as a first estimate of the
solution. Using this value, a second, more accurate value 𝑥
2
can be calculated
with 𝑥
2
1
2
1
/ 3 , which is then used for calculating a third, still more
accurate value 𝑥
3
, and so on. The general equation for calculating the value of
from the value of 𝑥
𝑖
is 𝑥
𝑖+ 1
𝑖
2
𝑖
)/ 3. Write a MATLAB program that
calculates the cubic root of a number. In the program use 𝑥
1
= 𝑃 for the first
estimate of the solution. Then, by using the general equation in a loop, calculate
new, more accurate values. Stop the looping when the estimated relative error
E defined by 𝐸 = |
𝑥
𝑖+ 1
−𝑥
𝑖
𝑥
𝑖
| is smaller than 0.00001. Use the program to calculate:
a. √ 100
3
b. √ 53701
3
c. √ 19. 35
3
M-File:
clear, clc
n=[100 53701 19.35];
for j=1:
P=n(j);
x=P;
while E>.
x_old=x;
x=(P/x^2+2*x)/3;
E=abs((x-x_old)/x_old);
end
fprintf('The cube root of %.0f is %.1f\n',P,x)
end
Command Window:
The cube root of 100 is 4.
The cube root of 53701 is 37.
The cube root of 19 is 2.
midterms, and a final exam, using the following scheme: Quizzes: Quizzes are
graded on a scale from 0 to 10. The grade of the lowest quiz is dropped and the
average of the 5 quizzes with the higher grades constitutes 30% of the course
grade. Midterms and final exam: Midterms and final exams are graded on a scale
from 0 to 100. If the average of the midterm scores is higher than the score on
the final exam, the average of the midterms constitutes 50% of the course grade
and the grade of the final exam constitutes 20% of the course grade. If the final
grade is higher than the average of the midterms, the average of the midterms
constitutes 20% of the course grade and the grade of the final exam constitutes
50% of the course grade.
Write a computer program in a script file that determines the course grade for
a student. The program first asks the user to enter the six quiz grades (in a
vector), the three midterm grades (in a vector), and the grade of the final. Then
the program calculates a numerical course grade (a number between 0 and
100). Finally, the program assigns a letter grade according to the following key:
A for 𝐺𝑟𝑎𝑑𝑒 ≥ 90 , B for 80 ≤ 𝐺𝑟𝑎𝑑𝑒 < 90 , C for 70 ≤ 𝐺𝑟𝑎𝑑𝑒 < 80 , D for 60 ≤
𝐺𝑟𝑎𝑑𝑒 < 70 , and E for a grade lower than 60. Execute the program for the
following cases:
(a) Quiz grades: 6, 10, 6, 8, 7, 8. Midterm grades: 82, 95, 89. Final exam: 81.
(b) Quiz grades: 9, 5, 8, 8, 7, 6. Midterm grades: 78, 82, 75. Final exam: 81.
M-File:
clear, clc
for j=1:
quiz=input('Please enter the quiz grades as a vector [x
x x x x x]: ');
mid=input('Please enter the midterm grades as a vector
[x x x]: ');
final=input('Please enter the final exam grade: ');
q_c=(sum(quiz)-min(quiz))/5;
if mean(mid)>final
grade=3q_c + 0.5mean(mid) + 0.2*final;
else
grade=3q_c + 0.2mean(mid) + 0.5*final;