Solving MATLAB For Loop Error: Counting 'c' to 11

  • Context: MATLAB 
  • Thread starter Thread starter elecz
  • Start date Start date
  • Tags Tags
    Loop Matlab
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB for loop error where a participant is trying to count occurrences of a condition involving the sine function. The focus is on debugging the code related to floating-point comparisons and the behavior of the sine function within the loop.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant suggests that comparing a floating-point number directly to zero may lead to issues due to precision errors, recommending the use of a tolerance value instead.
  • Another participant points out that the if statement only evaluates true for the first iteration (i = 0) and proposes that the logic should be modified to evaluate points continuously across the range.
  • A different participant mentions that round-off errors could make it impossible for the sine function to equal zero exactly, suggesting a comparison with a small epsilon value.
  • One participant notes that even when calculating sin(n*pi) separately in MATLAB, it does not yield zero, indicating potential issues with the function's output.

Areas of Agreement / Disagreement

Participants express varying opinions on the cause of the issue, with no consensus on the exact solution. Multiple competing views regarding the handling of floating-point comparisons and the logic of the loop remain unresolved.

Contextual Notes

Participants highlight limitations related to floating-point arithmetic and the specific behavior of the sine function in MATLAB, but do not resolve these issues.

elecz
Messages
17
Reaction score
0
For loop in Matlab

I have written following code in MATLAB but the if condition inside the for loop isn't working properly. Can anyone tell what's the possible bug??

f=1000;
T=0.001;
g=0:0.00005:5;
w=2*f*T*pi;
s=sin(g*w);
plot(g,s);
grid on;
title('SINE FN');
xlabel(' x10^-3 TIME axis(ms)');
ylabel('sin(x)');
diary off
c=0;
for i=0:0.25:5
if(sin(w.*i)==0)
c=c+1;
end
end
display(c);

counter 'c' shows the value one instead of 11..
 
Physics news on Phys.org


I don't have my copy of MATLAB in front of me, but my first thought is that you may be breaking a cardinal rule of programming when you're trying to compare a FLOATING number (for instance, the sin of 1.999999999999*pi) to the exact value of 0. You may have better results by using a comparison with epsilon, or, better yet, an arbitrarily-defined tolerance value.
http://www.mathworks.com/help/techdoc/ref/eps.html
http://www.mathworks.com/support/tech-notes/1100/1108.html
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

For future reference, please put your code inside of
Code:
[ /code] tags (obviously, with the space removed from the second tag).  This even allows you to preserve white space!
 
There is a forum for this question under Computing & Technology > Math & Science Software.

I'm still working on this part of MATLAB programming myself, but I think the problem is your if statement. It runs once at i = 0 and your logical statement is true, therefore c = 0+1.

The second time it runs, it evaluates to false and that is the end.

I think the problem is that what you want it to do is continuously evaluate every point even though it's swapping between true and false through the entire range.

My suggestion is to write it so that you're only evaluating points where sin(bleh)==0 up through 10*pi, which should be a pretty simple modification. I don't have MATLAB on this comp so I'm not going to check it, but I would imagine that should work.
 
First off I know nothing of matlab, but... Due to round off errors your contition of sin(..) = 0 maybe impossible. You might want to try sin(,,)< e where e is some small number say .00001 .
 
Yeah, it's not coming up with 0 for sin(n*pi) even when I just do the calculation separately in MATLAB.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K