Beruflich Dokumente
Kultur Dokumente
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
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 .
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 .
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);
=
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);