1 - Cornell Computer Science

CS100M 2001 Fall
Project 2 Solutions
General Comments








Output should correspond to the code submitted! It is a violation of academic integrity to turn in output from
programs not submitted by you.
Comments should be concise. Comments describe what is the task to be done by the subsequent code; comments do
not explain the code statement by statement!
Do not declare/initialize variables, constants that are not subsequently used.
Use loops when necessary—we saw quite a few unnecessary nested loops.
Use named constants for values that are frequently used and remain constant during the execution of the program.
Break lines that are too long to fit horizontally on the printed page. Programs with text wrapped to the next line are
very hard to read.
Read the question statement and the general instructions on CS100MHomeworkProject Format.
Read the textbook and follow the guidelines, usually given at the end of the chapters.
1.) Distance between 2 points
% Question 1 of Project 2
% Calculate distance between 2 points and
% print result to screen
% Prompt the user
x1 = input('Enter
x2 = input('Enter
y1 = input('Enter
y2 = input('Enter
to enter the co-ordinates
the Co-ordinate x1:');
the Co-ordinate x2:');
the Co-ordinate y1:');
the Co-ordinate y2:');
% Compute the distance and assign to variable 'dist'
dist = sqrt((x1-x2)^2+(y1-y2)^2);
% Display the distance just computed
disp([' The Distance between the points is '
num2str(dist) ]);
% Alternately fprintf can be used
% fprintf('The Distance between the Points is %f',dist);
%===========================================
% Enter the Co-ordinate x1: 2
% Enter the Co-ordinate x2: 8
% Enter the Co-ordinate y1: 3
% Enter the Co-ordinate y2: -5
% The Distance between the Points is 10.0000
2.) Function Evaluation
% Question 2 of Project-2
% Evaluate expression given on page 111, Problem 3.5
% Prompt user to enter the input value
x = input('Enter the Value of x:');
% Evaluate expression
if (x < 1)
% valid input
y = log(1/(1-x));
fprintf('The value of log(1/(1-x)), for an x value of %f is %f\n',x,y);
else
% x is less than 1, invalid input
disp('The value of x must be less than 1. Please Re-run');
end
%================================================================
% Enter the Value of x:0.5
% The value of log(1/(1-x)), for an x value of 0.500000 is 0.693147
% Enter the Value of x:3
% The value of x must be less than 1. Please Re-run
3a.) Position and Velocity of Ball (with a loop)
% Question 3(a) of Project-2
% This problem demonstrates the free fall example
% Part (a): use loop
% Prompt user to enter the initial height and velocity
h_init = input('Enter the intial Height:');
v_init = input('Enter the initial Velocity:');
% Declare constants
g = -9.81;
% Acceleration due to gravity
timeFinal = 10; % Solution of quadratic eqn 2.10 gives time of 9.995.
% Or use trial and error to find this result.
% Compute the height and velocities at different time intervals
for i = 0:timeFinal
time(i+1) = i; % Index should be strictly positive
h(i+1) = 0.5*g*i*i+v_init*i+h_init;
v(i+1) = g*i+v_init;
end
% plot the Height
figure(1)
plot(time,h);
title('Plot of Height with Time');
xlabel('Time (s)');
ylabel('Height (m)');
% Plot the Velocity
figure(2)
plot(time,v);
title('Plot of Velocity with Time');
xlabel('Time (s)');
ylabel('Velocity (m/s)');
% =================================================================
% The ball takes around 10 seconds to reach the ground
3b.) Position and Velocity of Ball (without a loop)
% Question 3(b) of Project-2
% This code demonstrates the free fall example
% Part (b): No loops (vectorized code)
% prompt the user to enter the initial height and velocity
h_init = input('Enter the intial Height:');
v_init = input('Enter the initial Velocity:');
% Declare constants
g = -9.81;
% Acceleration due to gravity
timeFinal = 10; % Solution of quadratic eqn 2.10 gives time of 9.995.
% Or use trial and error to find this result.
% Compute height and velocity
time = 0:1:timeFinal; % Times at which we calculate velocity and height
h = (0.5*g).*time.*time+v_init.*time+h_init.*ones(1,timeFinal+1);
v = g.*time+v_init.*ones(1,timeFinal+1);
% plot the Height
figure(1)
plot(time,h);
title('Plot of Height with Time');
xlabel('Time (s)');
ylabel('Height (m)');
% Plot the Velocity
figure(2)
plot(time,v);
title('Plot of Velocity with Time');
xlabel('Time (s)');
ylabel('Velocity( m/s)');
% =================================================================
% The ball takes around 10 seconds to reach the ground
4.) Eeeeeeeeee!
% Question 4 of Project-2
% This illustrates approximations and errors for a series sum
% First decide for what value of x, we should compute the errors
% and choose a tolerance
x = 0.5;
tol = 0.000001;
% tolerance, named constant can be easily modified
exact_val = exp(x);
% Exact value of the Exponential, a named constant
fprintf('True value is %10.8f\n', exact_val);
disp('No. of Terms
sum = 0;
i = 0;
Approximation
Error');
diff = 1;
% initialize to diff>tol in order to enter loop
% Repeat calculation until tolerance is satisfied
while (diff > tol)
sum = sum + (x^(i))/factorial(i);
i = i+1;
diff = exact_val-sum;
fprintf('%8d %17.8f
%12.8f \n',i,sum,diff);
end
fprintf('No. of terms needed to achieve a tol of %10.8f is %d\n',tol,i);
%=================================================================
%Part (a)
%No. of Terms
Approximation
Error
%
1
1.00000000
0.64872127
%
2
1.50000000
0.14872127
%
3
1.62500000
0.02372127
%
4
1.64583333
0.00288794
%
5
1.64843750
0.00028377
%No. of terms needed to achieve a tol of 0.00100000 is 5
Part (b)
% Tolerance
%
0.1
% 0.01
% 0.001
% 0.0001
% 0.00001
% 0.000001
-
# of Terms
3 Terms
4 Terms
5 Terms
6 Terms
7 Terms
8 Terms
In order to achieve higer accuracy (small tolerance), more terms need to be used. One can see that the number of terms
needed to achieve a relatively high accuracy is not many for this case. Repeated use of the program for part (b) also shows
that the use of named constants make the work simpler.