Sheet2 Solution
Sheet2 Solution
Computer
Programming
Mechanical engineering department 3rd year (Power) 2012/2113
Computer
Programming
Mechanical engineering department 3rd year (Power) 2012/2113
Start
Enter
P =1000
i = 0.09
A = P (1+n *i)
Print A
Start
Computer
Programming
Mechanical engineering department 3rd year (Power) 2012/2113
3-
c = input(’Enter Celsius Fahrenheit temperature: ’);
F = 9* c / 5 * + 32;
disp( ’The Fahrenheit temperature is: ’), disp(F)
4-
Function Remark Function Remark
Cos (x) Cosine of x log(x) Natural logarithm base (e)
sin(x) Sine of x log10(x) Common logarithm base (10)
tan(x) Tangent of x=Sin x/cos x abs(x) Absolute value of x
acos(x) Inverse (arc) cosine of x real(x) Real part of a complex number
between 0 and π.
cos−1(x)
asin(x) Inverse (arc) sine of x imag(x) Imaginary part of a complex
between −π/2 and π/2. number
sin−1(x)
atan(x) arc tangent of x between angle(x) Phase of a complex number[rad]
−π/2 and π/2.
−π/2 ≤ tan−1(x) ≤ π/2
atan2(y,x) arc tangent of y/x conj(x) Complex conjugate
between −π and π.
−π ≤ tan−1(y, x) ≤ π
cosh(x) hyperbolic cosine round(x) The nearest integer (round-off)
(ex + e−x) / 2
sinh(x) hyperbolic cosine sqrt(x) Square root
(ex − e−x) / 2
tanh(x) hyperbolic tangent sign(x) 1(if positive) / 0 / -1( if negative)
(ex − e−x) / (ex + e−x )
acosh(x) inverse hyperbolic sine of Clock Present time and date
x
cosh−1(x)
asinh(x) inverse hyperbolic sine of mod(y,x) Remainder of y/x
x
sinh−1(x)
atanh(x) inverse hyperbolic floor(x) The greatest integer ≤ x
tangent of x floor(-3.9) returns -4, floor(3.9)
tanh−1(x) returns 3.
Computer
Programming
Mechanical engineering department 3rd year (Power) 2012/2113
5-
Ts = 4; To = 100; k=0.0004;
t=0 : 3*3600;
T = Ts + (To - Ts)*exp(-k*t);
plot (t,T)
xlabel(' Time [sec] ');
ylabel('Temperature [oC]');
title('Heat Transfer Problem');
grid on;
% Temperature after 3hr, using the formula given
t=2*3600;
T = Ts + (To - Ts)*exp(-k*t);
T=round(T); % Round the answer to the nearest integer
disp( ' The temperature after 2 hours = '); disp(T)
90
80
70
Temperature [oC]
60
50
40
30
20
10
0
0 2000 4000 6000 8000 10000 12000
Time [sec]
RUN
The temperature after 2 hours =
5
Computer
Programming
Mechanical engineering department 3rd year (Power) 2012/2113
6-
x = [-2:0.01:4];
y = 3*x.^3 - 26*x + 6;
yd = 9*x.^2 - 26;
ydd = 18*x;
plot(x, y, '- b', x, yd, '-- r',x, ydd, ': k' )
grid on;
title('A plot of the function y = 3x^3 - 26x + 10 and its first and second
derivatives');
xlabel('x');
legend('y = 3x^3 - 26x + 10', 'yd = 9x^2 - 26', 'ydd = 18x');
3
A plot of the function y = 3x - 26x + 10 and its first and second derivatives
120
y = 3x3 - 26x + 10
100 yd = 9x2 - 26
ydd = 18x
80
60
40
20
-20
-40
-2 -1 0 1 2 3 4
x
7-
A)
3
-1
-2
-3
-4 -3 -2 -1 0 1 2 3 4
Computer
Programming
Mechanical engineering department 3rd year (Power) 2012/2113
B)
90 1
120 60
150 0.5 30
180 0
210 330
240 300
270
C)
3
Two Y Axes 3
2.5 2.5
2 2
Plot Y Axis
1.5 1.5
1 1
0.5 0.5
00 0
0 11 22 33 44 55 66 77
X Axis
D)
10
4
value of q
-2
-4
-6
-8
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5
value of t
Computer
Programming
Mechanical engineering department 3rd year (Power) 2012/2113
E)
1 1
0.5 0.5
0 0
-0.5 -0.5
-1 -1
0 2 4 6 8 0 2 4 6 8
500 1
400 0.8
300 0.6
200 0.4
100 0.2
0 0
0 2 4 6 8 0 2 4 6 8
F)
1
asin(x)
0.8 acos(x)
atan(x)
0.6
x, the argument of the function
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
-100 -50 0 50 100 150 200
in degrees