0% found this document useful (0 votes)
7 views

Sheet2 Solution

Uploaded by

mahmoud amin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Sheet2 Solution

Uploaded by

mahmoud amin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

BENHA UNIVERSITY Faculty of engineering

Computer
Programming
Mechanical engineering department 3rd year (Power) 2012/2113

Assignment (2) Solution


1- Identify the error in each of the following statements and
correct the wrong ones
a) n + 1 = n; variable must be on the right n =n + 1;
Fahrenheit temp = 9*C/5 + 32;
There is a space in the variable name Fahrenheit_temp = 9*C/5 + 32;
2 = x;
It is not allowed to start variable name with a number and variable must
be on the right x = 2;
b) z = 2 + 3*I missing *
z = 6 + j*5 missing *

c) x = 0.01:.001:.1; colon must be used


plot(x, sin(1./x)) Point to point division to get correct answer
d) x = 0 : 0.1 : 10;
z = sin(x); Parentheses is missing
plot(x,z), grid correct is z
e) x = 0 : pi/20 : 6 * pi;
plot(x, exp(-0.2*x) .* sin(x), ’r’),grid
Point to point multiplication to get correct answer
f) plotyy(x,sin(x), x, 10*cos(x))
y is missing in plotyy
g) t = (0:.1:2*pi); Apostrophe must deleted
subplot(2,2,1), plot(t,sin(t)) coma is missing
subplot(2,2,2), plot(t,cos(t))
subplot(2,2,3), plot(t,exp(t)) bracket is missing to close plot function
subplot(2,2,4), plot(t,1./(1+t.ˆ2)) index 5 is wrong, maximum 4
h) x = -1:0.001:1;
y1 = acos(x);
y2 = asin(x);
y3 = atan(x);
Dr. Mohamed Saber Sokar
BENHA UNIVERSITY Faculty of engineering

Computer
Programming
Mechanical engineering department 3rd year (Power) 2012/2113

plot(y1,x,y2,x,y3,x),grid missing comma


legend('asin(x)', 'acos(x)', 'atan(x)')
xlabel('\theta in radian),ylabel('x, the argument of the function')
2-

Start

Enter
P =1000
i = 0.09

A = P (1+n *i)

Print A

Start

% Matlab program to find the final amount of one year investment


P_balance = 1000;
i_rate = 0.09;
interest = i_rate * P_balance;
A_balance = P_balance + interest;
disp( ’Final amount of balance:’ );
disp( A_balance );
RUN
Final amount of balance:
1090.00
Dr. Mohamed Saber Sokar
BENHA UNIVERSITY Faculty of engineering

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)

0 (32), 100 (212), −40 (−40!), 37 (98.6).

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.

Dr. Mohamed Saber Sokar


BENHA UNIVERSITY Faculty of engineering

Computer
Programming
Mechanical engineering department 3rd year (Power) 2012/2113

exp(x) Exponential function ceil(x) The smallest integer ≥ x


ceil(-3.9) returns -3, ceil(3.9)
returns 4.
cot(x) cotangent of x. csc(x) cosecant of x.

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)

Heat Transfer Problem


100

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

Dr. Mohamed Saber Sokar


BENHA UNIVERSITY Faculty of engineering

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

Dr. Mohamed Saber Sokar


BENHA UNIVERSITY Faculty of engineering

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

Dr. Mohamed Saber Sokar


BENHA UNIVERSITY Faculty of engineering

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

Dr. Mohamed Saber Sokar

You might also like