Sie sind auf Seite 1von 6

CSC 113, Fall 2009

Midterm Exam + Solutions


The exam has 4 problems worth the same amount. Do all four problems and where possible
show your work. Please write clearly and comment all code you write.

1. Write a M ATLAB function file that builds the following N N matrix:

A=
3.
.
.
N

2
3
3
4
4
5
..
..
.
.
N +1 N +2

...

N
N +1

N +2

..

.
2N 1

The function should accept as input the integer N and return the matrix A as its output.
Solution: A simple way is to populate successive rows of A using the colon : operator.
Using this approach, the matrix can be constructed as follows:
function A = prob1(N)
%
% function A = prob1(N)
%
% returns to matrix A an N x N Hankel matrix (constant along
% antidiagonals) with 1 in the northwest corner, and 2N-1
% in the southeast corner.
A = zeros(N,N); % allocate the array first
for ii=1:N
A(ii,:) = [ii:ii+N-1]; % populate sucessive rows
end
end

Another method is to recognize that the matrix is defined elementwise as


Aij = i + j 1
which suggests the following version:

function A = prob1(N)
%
% function A = prob1(N)
%
% returns to matrix A an N x N Hankel matrix (constant along
% antidiagonals) with 1 in the northwest corner, and 2N-1
% in the southeast corner.
A = zeros(N,N); % allocate the array first
for ii=1:N
for jj=1:N
A(ii,jj) = ii + jj - 1; % entry (i,j)
end
end
end

This version requires two for loops, however, and so will be slower than the first version
for sizable N . An alterate approach is to create an N N array whose ith row has all entries
equal to i, and another array whose j th column has all entries equal to j, and then add them:
function A = prob1(N)
%
% function A = prob1(N)
%
% returns to matrix A an N x N Hankel matrix (constant along
% antidiagonals) with 1 in the northwest corner, and 2N-1
% in the southeast corner.
% Direct formulation:
A = [1:N] * ones(1,N) + ones(N,1) * [1:N] - 1;
end

This last version requires no loops, and should be fastest for sizeable N .

2. Write a M ATLAB script that calculates the following sum:


TN =

N
X

1
cos(n + )
n = 0 2n + 1

Here N (an integer) and (an angle in radians) are to be set at the top of the script. (Set them
to any convenient values; use N = 10 and = 0.2 if unsure).
Solution: This can be done using either a for loop (slower), or the sum( ) function (faster).
In the first version the script might look like this:
NN = 10; % This can be set to any other value for a later run
theta = 0.2 * pi;% This can be set to any other value later
TN = 0;
for nn=0:NN
TN = TN + cos(nn * pi + theta)/(nn + nn + 1);
end

A more efficient procedure is to create a vector whose n + 1st entry is [cos(n + )]/(2n + 1)
and then sum the entries:
NN = 10; % This can be set to any other value for a later run
phi = 0.2 * pi;% This can be set to any other value later
nn = [0:NN]; % create vector [0 1 2 ... N]
TN = sum( (cos(nn * pi + theta) ) ./ (2 * nn + 1));

Note that this version avoids the for loop and therefore should execute faster for large N .

3. Consider the following M ATLAB function file:


function [A,B]=problem3(x,y,z)
W = -17;
A = -5;
B = -5;
while W<=0
if (y<x)|(-z<=x)
A=A+2*abs(x-y);
B=B+x-y+3*z;
W=W+B
elseif (x<-y)&(z<10*x)
A=A-x+y;
B=B+5*y-2*z+x;
W=W+2*A+B;
else
W=W+8;
A=A+abs(x-y+z);
B=B+A;
end
end

If I now type
>> [A, B] = problem3(-4,-1,-2)

at the M ATLAB prompt, what will the return values of A and B be?
Answer:
A = 10
B = 10

4. The M ATLAB function file below is intended to compute and plot the electrostatic force F
between two charged particles, as a function of the distance r between them.
According to Coulombs law,
q1 q2
Newtons
r2
1
=
40
= 8.854 1012

F = kc
kc
0

where q1 and q2 are the charges on the two particles. If either particle is an electron, then
q1 = q2 = 1.602 1019 Coulombs.
The input to the function is a vector of distances r, and the output is to be a vector of the
same size, containing the corresponding forces F .
Here is some code that has FIVE errors in it. Find those errors and propose a solution. The
errors could be either syntax errors or logical errors.
function F
% function
% Input:
% Output:
%

=
F
r
F

problem4(r)
= problem4(r)
= vector of distances between two electrons
= vector of forces between the electrons as a
function of distance

%
% some constants
%
kc = 1/(4pi * eo);
eo = 8.854e-12;
q = 1.602e-19;
%
% loop through all the r values
%
for ii=1:length(F)
F(ii) = kc*q*q./r;
end
%
% plot the result
%
plot(F,r);
title(Force as a function of distance);

Solution: The five errors are as follows:


i. Line 10: 4pi should be 4 * pi;
ii. Line 11: the value of eo should be set before it is used in the preceding line. Thus lines
10 and 11 should be swapped.
iii. In the for loop, the final value for ii should be the length of the input vector, not the
length of F. Thus the line should be changed to for ii=1:length(r) .
iv. The right-hand side of the expression for F(ii) performs the element-by-element division (./) of a scalar by a vector, yielding a vector. It should instead divide a scalar
by the scalar r(ii)2.
v. The plot command has inverted the order of the arguments, plotting thus r as a function of F, rather than F as a function of r.
The corrected program thus appears as follows:
function F
% function
% Input:
% Output:
%

=
F
r
F

problem4(r)
= problem4(r)
= vector of distances between two electrons
= vector of forces between the electrons as a
function of distance

%
% some constants
%
eo = 8.854e-12;
kc = 1/(4 * pi * eo);
q = 1.602e-19;
%
% loop through all the r values
%
for ii=1:length(r)
F(ii) = kc*q*q/r(ii)2;
end
%
% plot the result
%
plot(r,F);
title(Force as a function of distance);

Das könnte Ihnen auch gefallen