MATLAB Answers

## Help needed displaying multiple plots (and maybe preassigning too)

Asked by Tom

### Tom (view profile)

on 10 Dec 2011
Accepted Answer by Walter Roberson

### Walter Roberson (view profile)

Help needed displaying multiple plots (and maybe preassigning too) I've got this script, but when I try add x and y labels it returns error codes. How would I go about putting two plots on one graph? I want to make three graphs, all with f on the x axis and then with z, R and AbsCoef as the three different y axis values. On each of these I want to have two plots - one regular and one with the 'nogap' tag. There's also something happening where Matlab is suggesting that I preallocate - and I don't know what that is.
Any help would be greatly appreciated.
iter=1;
for f=50:1000
rho_a=1.2041;
sigma=11000;
phi=0.99;
alpha=1;
c=343.26;
i=sqrt(-1);
L=0.1;
a=0.03;
k_a=(2*pi*f)/c;
k_2=((2*pi*f)/c)*sqrt(alpha-((i*sigma*phi)/(2*pi*f*rho_a)));
z_c_2=((rho_a*c)/phi)*sqrt(alpha-((i*sigma*phi)/(2*pi*f*rho_a)));
z_1=(z_c_2*cot(k_2*L)*(cos(k_a*a)+(i/(rho_a*c))*sin(k_a*a)))/(i*cos(k_a*a)-(1/(rho_a*c))*sin(k_a*a));
z_1rec(iter)=z_1;
z_1nogap=-i*z_c_2*cot(k_2*L);
z_1nogaprec(iter)=z_1nogap;
R=((z_1/(rho_a*c))-1)/(1+(z_1/(rho_a*c)));
Rrec(iter)=R;
AbsCoef=1-(real(R))^2;
AbsCoefrec(iter)=AbsCoef;
Rnogap=((z_1nogap/(rho_a*c))-1)/(1+(z_1nogap/(rho_a*c)));
Rnogaprec(iter)=Rnogap;
AbsCoefnogap=1-(real(Rnogap))^2;
AbsCoefnogaprec(iter)=AbsCoefnogap;
iter=iter+1;
end
plot(50:1000,z_1rec)

Walter Roberson

### Walter Roberson (view profile)

on 10 Dec 2011
Urk. Could we get some indentation and blank lines and spaces in the middle of lines? Spacing is important punctuation in the reading of code.

Sign in to comment.

## 2 Answers ### Walter Roberson (view profile)

Answer by Walter Roberson

### Walter Roberson (view profile)

on 10 Dec 2011
Accepted Answer

For the preallocation:
Before the loop, use
niter = 1000-50+1;
z_1rec = zeros(niter,1);
Rnogaprec = zeros(niter,1);

#### 0 Comments

Sign in to comment.

Answer by Paulo Silva

### Paulo Silva (view profile)

on 10 Dec 2011

doc hold %allows you to have more than one plot on a axes
doc subplot %if you want more than one axes on a figure
Please read the documentation I supplied and try to improve your code with it, any further question can be answered here, good luck with it.

#### 0 Comments

Sign in to comment.