# Data storage in a for loop in matlab

• MATLAB

## Main Question or Discussion Point

Hi!

I'm trying to write a program, to show the vector field of a linear simple wave in shallow water. First, determining the position of a particle i run into a problem when trying to store the data in a vector/array. The equation for this is given below. In this particular case everything is kept constant except the water depth h

for h = 0.1: 0.1: 3
x(h)=xi+H/2*cosh((omega/(sqrt(g*h)))*(zeta+h))/(sinh((omega/(sqrt(g*h)))*h))*sin(omega*t-(omega/(sqrt(g*h)))*xi);
end

I get the following error:

??? Attempted to access x(0.1); index must be a positive integer or logical.

Help is much appreciated...

//Kbka

Related MATLAB, Maple, Mathematica, LaTeX News on Phys.org
I have realized that the index must be an integer. The increments need to be at least 0.1 so is there another way to store the x-values

You will need two vectors, one for x and one for h. This will work if all the other values are scalars:

h = 0.1:0.1:3;
x = xi + H/2 * cosh(omega./sqrt(g*h).*(zeta+h)) ./ sinh(omega./sqrt(g*h).*h) .* sin(omega*t - omega./sqrt(g*h).*xi);

As an example, in order to plot x versus h, you will just use plot(x,h).