Q:

How do I plot the frequency response of a digital filter in MATLAB?

I am trying to graph the frequency response. I was asked to use filter in MATLAB, but since I've read the manual, I still don't get how it performs a Z transform.
I have the impulse response of the digital filter written below:
for i=1:22;
   y(i)= 0;
end
x(22) = 1;
for k=23:2523
    x(k) = 0;
end
for n = 22:2522;
    y(n) = ((1/21)*x(n))+((20/21)*y(n-21));
end
plot(y);

It's just a feedback system of y[n] = 1/21*x[n] + 20/21*y[n-21]
Below are my calculations to compute the Z transform of the above system, which ultimately determines the impulse response:
Z(y) = Z((1/21)*x(n)+(20/21)*y(n-21))

Y(Z) = (1/21)X(Z)+(20/21)*Z.^-21Y(Z)

Z(Z)-(20/21)*Z.^-21Y(Z) = (1/21)X(Z)

Y(Z)(1-(20/21)*Z.^-21) = (1/21)X(Z)  // divide by X(Z)*(1-(20/21)*Z.^-21)

Y(Z)/X(Z) = (1/21)/(1-(20/21)*Z.^-21)

H(Z) = (1/21)/(1-(20/21)*Z.^-21) // B = 1/21, A = 20/21

H(Z) = (B*Z.^21)/(Z.^21-A)

How can I plot the frequency response of H(Z)?  Should I use filter?

A:

If you just need to plot the impulse response it's easy. The impulse response is the response of the digital filter to a Dirac pulse. You already have the difference equation, so you're already in 'z' and you don't care about the 's', you don't have to perform the 's' to 'z' transform (which is a topic in itself!).
So just generate a signal x(n) consisting of zeros everywhere except the first sample x(1) being 1. Pass it through the filter (yes, your difference equation). The y(n) you get is your impulse response h(n). That's basically what you did.
And of course if you FFT this h(n) you get the phase and magnitude response.

A:

Use freqz from the signal processing toolbox (hope you have it).  You first need to find the Z-transform, which you have already done here:
Y(Z)/X(Z) = (1/21)/(1-(20/21)*Z.^-21)

freqz takes in a vector of coefficients which correspond to the numerator and denominator of your transfer function.  It's called like so:
freqz(b, a);

b and a are the numerator and denominator coefficients of your transfer function.  This will produce a figure that shows the magnitude and phase response (hence frequency response) of the above system.
Therefore, all you need is to do this:
b = 1/21;
a = [1 zeros(1,20) -(20/21)];
freqz(b, a)

Take special note of the a vector.  It has 1, followed by 20 zeros, then followed by -(20/21).  Because you have a coefficient to the power of -21 and nothing else other than the 1 before it, that means that those coefficients between -1 to -20 are zero, and there are 20 of these coefficients that are zero in total, which is why we need to fill in the vector with zeroes between the 1 and -(20/21) term.
We get:

If you want to plot the poles and zeroes of your filter, use a combination of tf and pzmap:
sys = tf(b, a, -1);
pzmap(sys);

tf creates a transfer function by specifying the numerator and denominator coefficients of your filter, and -1 implies it's a discrete-time filter, but we don't know what the sampling time is.  pzmap plots the poles and zeroes in the z-domain with the unit-circle overlaid.
We get this:

This makes sense as you have no zeroes in your system, and 21 poles, as you have 21 delay elements when examining the discrete-time sequence in your example.

A:

From Matlab's filter documentation:

filters the input data, x, using a rational transfer function defined by the numerator and denominator coefficients b and a, respectively.

As you might know, filtering an impulse input would give you the impulse response. It then remains to obtain those b and a coefficients.
You could either obtain those directly from the difference equation
y[n] = 1/21*x[n] + 20/21*y[n-21];

(as indicated in the rational transfer function link above) or equivalently from the rational transfer function you have derived:
%H(Z) = (B*Z.^21)/(Z.^21-A)

In either case, you should get the following a and b coefficients:
a = [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -20/21];
% or equivalently: a=zeros(22,1); a(1)=1; a(22)=-20/21;
b = [1/21];

Thus,
% setup the impulse input
x = zeros(2500,1);
x(1) = 1;

% compute the impulse response
y = filter(b, a, x);

This impulse response can be plotted as you have done:
plot(y);

As far as how this related to the transform H(Z), the closed form expression you obtained can be evaluated in terms of a Laurent series expansion, which then has the coefficients of the time-domain y (impulse response) series.
However, H(z) is an analytic function defined in the |z| > R (where R=power(20/21,1/21) in your case) region of convergence in the complex plane. It is more typical to plot the frequency response, which corresponds to the H(z) evaluated on the unit-circle (i.e. for complex number satisfying |z|=1 or equivalently z = exp(j * theta) with theta in the [0-2pi] range). An efficient method to compute values of H(z) at regularly spaced points on that unit-circle is to take the FFT of the impulse response:
FrequencyResponse = fft(y);
figure(1);
plot(abs(FrequencyResponse));
figure(2);
plot(phase(FrequencyResponse));

P.S.: the computation of filter and fft can be done in the single call freqz if you have the signal processing toolbox (although you were specifically asked to use filter).

