Data storage in a for loop in matlab

  • MATLAB
  • Thread starter kbka
  • Start date
  • #1
25
0

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
 

Answers and Replies

  • #2
25
0
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
 
  • #3
166
0
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).
 

Related Threads on Data storage in a for loop in matlab

Replies
5
Views
5K
Replies
3
Views
6K
  • Last Post
Replies
3
Views
3K
Replies
1
Views
2K
Replies
2
Views
2K
Replies
4
Views
6K
Replies
1
Views
6K
Replies
1
Views
2K
  • Last Post
Replies
1
Views
13K
Replies
4
Views
10K
Top