Problems integrating in a for loop in Matlab

Click For Summary
SUMMARY

The discussion centers on a common issue encountered when using "for" loops in Matlab, specifically in the context of integrating functions. The user, Ryan, initially wrote the loop incorrectly as "for C1 = 1,moments" instead of the correct syntax "for C1 = 1:moments". This mistake resulted in the loop executing only once, leading to the unexpected behavior of the variable "spatial". After receiving clarification from another forum member, Ryan corrected the syntax and successfully populated the array as intended.

PREREQUISITES
  • Understanding of Matlab programming syntax
  • Familiarity with anonymous functions in Matlab
  • Knowledge of numerical integration techniques, specifically the quad function
  • Basic concepts of eigenfunctions and their applications
NEXT STEPS
  • Review Matlab documentation on "for" loop syntax and usage
  • Explore the use of anonymous functions in Matlab for mathematical operations
  • Learn about numerical integration methods in Matlab, focusing on the quad function
  • Investigate eigenfunctions and their significance in mathematical modeling
USEFUL FOR

Matlab programmers, students learning numerical methods, and researchers working with eigenfunctions will benefit from this discussion, particularly those looking to enhance their understanding of loop structures and numerical integration in Matlab.

ryan.j
Gold Member
Messages
8
Reaction score
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


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).
 


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
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 1 ·
Replies
1
Views
13K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K