MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

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

Asked by Stan on 6 Apr 2013

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;
Ac = pi*(rc^2);
z0 = rho*c/Ac;
plot(abs(gamma))
end```

Have a great day!

Tobias on 8 Apr 2013

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]
```
Jan Simon on 8 Apr 2013

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.

## Products

No products are associated with this question.

Answer by Jan Simon on 8 Apr 2013

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 * .