Sheet 2
Sheet 2
Computer
Programming
Mechanical engineering department 3rd year (Power)
Assignment (2)
1- Identify the error in each of the following statements and correct
the wrong ones
a) n + 1 = n;
Fahrenheit temp = 9*C/5 + 32;
2 = x;
b) z = 2 + 3 i
z=6+j5
c) x = 0.01 : .001; .1;
plot (x, sin(1/x))
d) x = 0 : 0.1 : 10;
z = sin x;
plot (x,y), grid
e) x = 0 : pi/20 : 6 * pi;
plot(x, exp(-0.2*x) * sin(x), ’r’), grid
f) ploty(x,sin(x), x, 10*cos(x)) % independent left and right y-axis labels
g) t = (0:.1:2*pi)’;
subplot(2,2,1), plot(t sin(t))
subplot(2,2,2), plot(t,cos(t))
subplot(2,2,3), plot(t,exp(t)
subplot(2,2,5), plot(t,1./(1+t. ^2))
h) x = -1:0.001:1;
y1 = acos(x);
y2 = asin(x);
y3 = atan(x);
plot(y1,x,y2,x,y3 x), grid,
legend(’asin(x)’, acos(x)’, ’atan(x)’)
xlabel(’\theta in radian’), ylabel(’x, the argument of the function’)
6- Draw a flowchart to plot the function y = 3x3–26x + 10, and its first and
second derivatives in the same plot during the interval -2≤ x ≤ 4.
User Matlab command to add x-axis title (x) and diagram title (A plot of the
function y = 3x^3 - 26x + 10 and its first and second derivatives)
Display also the legend on the diagram as follows; first legend ( y = 3x^3 -
26x + 10), Second legend (yd = 9x^2 – 26), third legend (ydd = 18x)
7- Run the following MATLAB program and copy the resulting curves in your
answer sheet
A)
% program indicating plot options
x = -pi:pi/10:pi;
y = tan(sin(x)) - sin(tan(x));
plot(x,y,'--rs','LineWidth',2, 'MarkerEdgeColor','k',...
'MarkerFaceColor','g', 'MarkerSize',10)
B)
x = 0:pi/40:2*pi;
polar(x, sin(2*x)),grid
C)
t = (0:.1:2*pi);
subplot(2,2,1), plot(t,sin(t))
subplot(2,2,4), plot(t,1./(1+t.^2))
subplot(2,2,3), plot(t,exp(t))
subplot(2,2,2), plot(t,cos(t))
F)
t = -1:0.001:1;
y1 = acos(x)*180/pi; y2 = asin(x) *180/pi; y3 = atan(x) *180/pi;
plot(y1,t,y2,t,y3, t),grid,
legend('asin(x)', 'acos(x)', 'atan(x)')
xlabel('\theta in degrees'),ylabel('x, the argument of the function')