Problems integrating in a for loop in Matlab

In summary, the speaker is having trouble with a "for" loop in Matlab where they are trying to integrate various eigenfunctions. The loop is not functioning as expected and is only creating a single-valued variable instead of populating an array. The speaker eventually realizes that they were using the wrong syntax and thanks the person for their help.
  • #1
ryan.j
Gold Member
9
0
Problems integrating in a "for" loop in Matlab

My problem strikes me as embarrassingly simple, so hopefully someone can set me straight with ease.

I'm writing a Matlab code in which I'll be wanting to do a good amount of integrating of products of various eigenfunctions. Presently, I'm just trying to get the code up and running and can't seem to get a basic "for" loop to function as I'd expect.

I'd expect that it would populate an array called "spatial" with seven elements, each of which is an integral of a cosine raised to a given power.

Instead, it creates a singly-valued variable called "spatial" and then stops. For what it's worth, the value that it assigns is what I'd expect for the first execution of that loop.

Here is the exact code giving me this grief:

moments = 7;
a = 11;
lambda = pi/a;

for C1 = 1,moments

integrand = @(x)(cos(lambda*x).^(C1+1))
spatial(C1) = quad(integrand,-a/2,a/2)

end


Any help would be greatly appreciated.
Thank you kindly.
-ryan
 
Physics news on Phys.org
  • #2


I am not a Matlab programmer, but would

Code:
for C1 = 1:moments

work better?
 
  • #3


I appreciate the help, but the syntax I've listed is the equivalent Matlab "translation" of the C++/FORTRAN loop syntax.
 
  • #5


Wow. I'm laughing at myself. Thank you kindly. A case of plain and simple space cadetery on my part. ". . .Embarrassingly simple. . ." doesn't do justice to this one.

thanks for taking the time.
-ryan
 

FAQ: Problems integrating in a for loop in Matlab

Why am I getting an "index out of bounds" error when using a for loop in Matlab?

This error occurs when you are trying to access an element in an array that does not exist. This could be due to incorrect indexing or the array being smaller than the specified index. Make sure to double check your indexing and the size of your array.

How can I avoid an infinite loop in my for loop in Matlab?

An infinite loop occurs when the condition for the loop to end is never met. To avoid this, make sure your loop condition is properly defined and that the loop variable is being incremented or decremented correctly within the loop.

Why is my for loop in Matlab not iterating through all the elements in my array?

This could be due to a logical error in your code, such as using the wrong loop variable or not updating the loop variable correctly. Double check your code to make sure the loop is properly iterating through all the elements in the array.

How can I optimize the performance of my for loop in Matlab?

One way to optimize the performance of your for loop is to preallocate memory for your arrays before the loop starts. This can help improve the speed of your loop, especially for large arrays. Additionally, try to minimize the number of operations within the loop and avoid unnecessary calculations.

Can I use a for loop in Matlab to iterate through a matrix?

Yes, a for loop can be used to iterate through a matrix in Matlab. However, it is generally more efficient to use vectorized operations instead of a for loop when working with matrices. This means performing operations on the entire matrix rather than looping through each element individually.

Similar threads

Replies
2
Views
3K
Replies
1
Views
6K
Replies
18
Views
11K
Replies
2
Views
6K
Replies
1
Views
2K
Back
Top