Problems integrating in a for loop in Matlab

Click For Summary

Discussion Overview

The discussion revolves around a problem encountered while integrating functions within a "for" loop in Matlab. The focus is on troubleshooting code that is intended to populate an array with integral values but is not functioning as expected.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant describes their attempt to use a "for" loop in Matlab to compute integrals of cosine functions raised to various powers, but encounters an issue where the expected array is not being populated correctly.
  • Another participant suggests that using a colon instead of a comma in the "for" loop syntax might resolve the issue.
  • A later reply indicates that the original syntax was intended to mimic C++/FORTRAN loop syntax, implying a misunderstanding of Matlab's syntax requirements.
  • One participant references Matlab documentation to clarify that a comma is not an acceptable substitute for a colon in the "for" loop syntax.
  • The original poster expresses relief and humor about the situation, acknowledging the simplicity of the mistake.

Areas of Agreement / Disagreement

Participants do not appear to have significant disagreement, as the discussion centers around clarifying syntax rather than competing views. The original poster acknowledges the error and expresses gratitude for the assistance.

Contextual Notes

The discussion highlights a specific syntax issue in Matlab that may depend on the participant's prior programming experience in other languages.

Who May Find This Useful

Individuals learning Matlab, particularly those with backgrounds in other programming languages, may find this discussion helpful for understanding syntax differences.

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


I am not a Matlab programmer, but would

Code:
for C1 = 1:moments

work better?
 


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


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
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 18 ·
Replies
18
Views
11K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
9K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
7K