Problems integrating in a for loop in Matlab

  • Thread starter ryan.j
  • Start date
  • #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
 

Answers and Replies

  • #2
473
13


I am not a Matlab programmer, but would

Code:
for C1 = 1:moments
work better?
 
  • #3
ryan.j
Gold Member
9
0


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


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.

Sincerely, thanks for taking the time.
-ryan
 

Related Threads on Problems integrating in a for loop in Matlab

Replies
10
Views
11K
  • Last Post
Replies
22
Views
5K
Replies
1
Views
5K
  • Last Post
Replies
1
Views
1K
Replies
3
Views
1K
  • Last Post
Replies
3
Views
21K
  • Last Post
Replies
8
Views
1K
  • Last Post
Replies
1
Views
2K
Replies
1
Views
3K
Replies
2
Views
2K
Top