You are on page 1of 5

Minia University Pof.

Mohammed Moness
Faculty of Engineering Programming. Language 2 (CSE121)
CSE Dept., 1st Year 2nd Semester, 9, May, 2017

Student Name: Grade:


Instructions:
You cannot use a calculator or computer or consult any notes or books.
No communication please.

Question (I): 20 Marks


Given:
3 4 5 1+
() = 1 + + + + ,
4 8 16 2
Design a MATLAB function to compute the sum of n-
terms of this function using,
(a)conditional loop, (b)unconditional loop, and
(c)vectorization.
The outputs are the correct sum, the execution
time in each case, and the name of fasted
technique.

Your Answer:

function [s,t1,t2,t3,tp] = prob1_2_2017(n)


% This fuction computes the sum: 1+ 3/4+..(1+n)/(2^n)
%
% Inputs
% ======
% n --> number of terms
%
% Outputs
% =======
% s --> sum of n-terms
% t1 --> execution time using uncondtional loop
% t2 --> execution time using condtional loop
% t1 --> execution time using vectorization
% tp --> the best execution time
%
% Record of revisions:
% Date Programmer Description of change
% ========== ============== =====================
% 09/05/2017 Prof M. Moness Original code
Professor Mohammed Moness Page 1 of 5
% CHECK n
if fix(n)~= n |n<= 0
error('n must be positive integer number')
end
%
% CHECK INPUT and OUTPUT ARGUMENTS
if nargin ~=1
error('number of input argumets must equal to 1')
end
%
if nargout ~= 5
error('number of output arguments must equal to 5')
end
%
% Conditional Loop
tic
k1 =1 ; s1 = 0 ;
while k1 <= n
s1 = s1 + (1+k1)/(2^k1);
k1 = k1 + 1;
end
t1 = toc;
%
% Unconditional Loop
tic
s2 = 0;
for k2 = 1 : n
s2 = s2 + (1+k2)/(2^k2);
end
t2 = toc ;
%
% Vectorization
tic
num = 2 : 1 : 1 + n ;
den = (2*ones(1,n)).^([1:n]) ;
s3 = sum(num./den);
t3 = toc;
%
% CHECK SUM
if s1 ~= s2 | s1~= s3
error('ERRORS of SUM')
else
s=s1;
end
%

Professor Mohammed Moness Page 2 of 5


% The best time
[tp k ]= min([ t1 t2 t3]);
switch k
case 1
disp('Unconditional Loop Yields the Best Time')
case 2
disp('Conditional Loop yields the Best Time')
case 3
disp('Vectorization yields the Best Time')
end

Question (II): 20 Marks


The sine function is an infinite series that has
the form:
3 5 7
( ) = + + ..
3! 5! 7!
Design a MATLAB function using computed n-terms to
achieve a given, error. This error is the
difference between the values computed by the
built-in function, sin(x), and that computed by
your user-defined function. The outputs also
include the computed value

Your Answer:

function [ sine,cae ] = Sine( x,ae )


% This program computes sine function tot an error ae
% Inputs:
% =======
% x value of angle in radian
% ae The required angle in radian
%
% Outputs
% =======
% sine : the compute value
% cae : the absolute value of actual error
%
% Record of revisions:
% Date Programmer Description of change
Professor Mohammed Moness Page 3 of 5
% ========== ============== =====================
% 09/05/2017 Prof M. Moness Original code
%
% CHECK n
% Check
if nargin ~= 2
eror('Number of input arguments must equal to 2')
end
if nragout ~= 2
error(' Number of input arguments must equal to 2')
end
sine = 0; k=1;cae=1;
while abs(cae) >= abs(ae)
sine = sine +((-1)^(k-1))*(x^(2*k-1))/factorial(2*k-
1);
k=k+1
cae = abs(sin(x)-sine);
end

Question (III): 10 Marks


Write a MATLAB segments to plot the following relations, as shown
below,
sin()
3.1) = , = 2 + 2 , 8 8 , =

3.2) The spiral of Archimedes relation which is described by the polar
coordinates (, r), where r = a, for 0 , with the parameter a = 2.
5
3.3) The function, f = 3 3 2 ( ) + 3 , 2 4
4 4
Your Answer:
clear
%
%
subplot(2,2,[1,3])
axis( [-10 10 -10 10 -0.5 1])
grid
x = -8: .5: 8; y = x;
[X,Y] = meshgrid(x,y);
R = sqrt(X.^2 + Y.^2) + eps; % add eps to prevent R=0
Z = sin(R)./R;
mesh(x, y, Z)
title('R=$$\sqrt{x^2+y^2} and z = \frac{sin(R)}{R}$$','Interpreter','Latex' )
Professor Mohammed Moness Page 4 of 5
%
%
subplot(2,2,2)
a=2;
theta=0:0.1:2*pi;
r=a*theta;
polar(theta,r)
title('r=a\theta')
%
%
subplot(2,2,4)
axis([-1 4 -15 5])
x = -2:0.1:4;
f = x.^3 - 3*(x.^2).*sin(pi*x/4-5*pi/4) + 3;
plot(x,f)
grid
title('f(x) vers x')
xlabel('x'),ylabel('f(x)')

Good Luck, Prof. Dr. M. Moness


Professor Mohammed Moness Page 5 of 5

You might also like