Data storage in a for loop in matlab

In summary, the conversation is about creating a program to display the vector field of a linear simple wave in shallow water. The person is having trouble storing data in a vector or array, specifically with the equation for a constant water depth. They receive an error message and ask for help, and it is suggested to use two vectors for x and h. The person also mentions using the plot function to display the data.
  • #1
kbka
25
0
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
 
Physics news on Phys.org
  • #2
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
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).
 

1. What is a for loop in Matlab?

A for loop in Matlab is a programming construct that allows you to repeatedly execute a block of code a specified number of times. It is commonly used for tasks such as iterating through arrays or performing calculations.

2. How do I store data in a for loop in Matlab?

To store data in a for loop in Matlab, you can use an indexing variable to assign values to an array. For example, you could create an empty array before the loop and then use the indexing variable to assign values to the array during each iteration of the loop.

3. Can I store different types of data in a for loop in Matlab?

Yes, you can store different types of data in a for loop in Matlab. The data can be stored in an array or in separate variables, depending on your specific needs.

4. How can I access the stored data in a for loop in Matlab?

To access the stored data in a for loop in Matlab, you can use the indexing variable to retrieve specific values from an array or use the variables that were used to store the data.

5. Is there a limit to the amount of data that can be stored in a for loop in Matlab?

No, there is no specific limit to the amount of data that can be stored in a for loop in Matlab. However, the amount of data that can be stored may be limited by the available memory on your computer.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
811
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
7K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
29
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top