Problems integrating in a for loop in Matlab

In summary, the person is having trouble with integrating in a "for" loop in Matlab and is seeking help. They have realized that their syntax for the "for" loop was incorrect and have corrected it with the help of others.
  • #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


Your syntax for 'for' isn't right - it should be:

Code:
for C1 = 1:moments

to do what you want. Writing it the way you did only executes the loop once (with C1 = 1).
 
  • #3


Thank you kindly for taking the time to help me out. Member Joffan just pointed it out to me as well.

Unbelievably simple mistake, and even more unbelievable failure to see it. Especially given that I've a dozen other functioning Matlab scripts with heavy use of for loops.

Thanks again. Hopefully I'll be less ridiculous in the future.
-ryan
 

Related to Problems integrating in a for loop in Matlab

1. How do I use a for loop in Matlab to solve a specific problem?

To use a for loop in Matlab, you will need to first define the initial value, the final value, and the increment or decrement value for the loop. Then, you can include the code that you want to be executed for each iteration of the loop within the loop body. Finally, you can use the loop index variable to track the progress of the loop.

2. How can I iterate through a specific range of values in a for loop in Matlab?

You can use the linspace function in Matlab to generate a vector of evenly spaced values within a specified range. Then, you can use this vector as the loop index variable and access each value in the loop body to perform your desired operations.

3. What is the difference between a for loop and a while loop in Matlab?

In a for loop, the number of iterations is predetermined and the loop index variable is automatically incremented or decremented after each iteration. In a while loop, the loop will continue to run as long as the specified condition is true. Additionally, the loop index variable must be manually updated within the loop body.

4. How do I avoid infinite loops when using a for loop in Matlab?

To avoid infinite loops, make sure that your loop condition will eventually evaluate to false. This can be achieved by using a counter variable or a logical expression as your loop condition. Additionally, it is important to properly increment or decrement the loop index variable within the loop body to ensure that the loop will eventually terminate.

5. Can I use a for loop to solve problems involving matrices in Matlab?

Yes, you can use a for loop to iterate through the rows and columns of a matrix in Matlab. By accessing each element of the matrix using its row and column index, you can perform operations on the elements and update them accordingly. However, for large matrices, it may be more efficient to use built-in functions instead of a for loop.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
Back
Top