Problem with plotting a basic function with a domain specified by an array or vector...

Hi, im new to matlab. Im having trouble with the following code. I have to do a plot of "gamma" in the end, but I get an error message saying that the matrix dimension must agree.
%universal constants
c = 345;
rho = 407 / c;
L = 1;
%frequency here
N = 5; % number of sections
dx = L / N;
T = (L / c) / N;
Fs = 1 / T; % sample rate required
npoints = 2^12;
f = linspace(0, Fs, npoints); % frequency vector
%equations here
w = 2*pi*f;
s = i*w;
rc = 0.00375;
rrad = c / w;
Ac = pi*(rc^2);
Arad = 2*pi*(rrad^2);
z0 = rho*c/Ac;
Yrad = (Arad/(s*rc*rho))+(Arad/(rho*c));
Zrad = 1 / Yrad;
gamma = (Zrad-z0)/(Zrad+z0)
plot(abs(gamma))
end
Have a great day!

2 commentaires

I'm just about to go to the university, so I don't have time to do an in-depth look at your code, but one thing I see is your mathematical interaction between constants and vectors. I am pretty sure you need to do ./ or .^ etc, if the calculation is be be done on each element of a vector. For example everything that has f included in calculations.
A = 5
B = [5 2]
B .* A = [25 10]
Please post a copy of the error message. Most of all it contains information about the line, where the problem occurs. Then we do not have to guess this line.

Connectez-vous pour commenter.

Réponses (1)

Do you want rrad to be a scalar or a vector of the same size as c and w? For the later case, you need the elementwies division:
rrad = c ./ w;
The same concerns Arad = 2*pi*(rrad^2) also, where you'd need rrad .^ 2, and the same for the following vector operations with .* instead of * .

Catégories

En savoir plus sur Mathematics dans Centre d'aide et File Exchange

Question posée :

le 6 Avr 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by