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

In summary: It seems like you may be 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.In summary, the for loop in MATLAB is not working properly and the problem may be with the if statement.
  • #1
elecz
17
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
  • #2


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!
 
  • #3


I wish my attached file may help you to compare with your m file...:D

View attachment NNFL_HW2.m
 
  • #4
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.
 
  • #5
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 .
 
  • #6
Yeah, it's not coming up with 0 for sin(n*pi) even when I just do the calculation separately in MATLAB.
 

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

1. What is a for loop in MATLAB?

A for loop in MATLAB is a programming structure that allows you to execute a set of commands repeatedly based on a specified number of iterations or a condition. It is useful for automating repetitive tasks and for processing large amounts of data.

2. How do I solve a for loop error in MATLAB?

The most common for loop error in MATLAB is an indexing error, where the loop index goes out of bounds. To solve this, you can check your indexing logic and make sure it is within the bounds of your data. You can also use the built-in debugging tools in MATLAB to identify and fix the error.

3. Why does my for loop only run once?

This could be due to a mistake in your loop logic, such as setting the loop index to a constant value instead of incrementing it. It could also be caused by an error in your code that causes the loop to terminate early. Double check your code and make sure all necessary variables are being updated correctly.

4. How do I count from 1 to 11 in a for loop in MATLAB?

You can create a for loop that counts from 1 to 11 by setting the loop index to 1 and the loop condition to be less than or equal to 11. Inside the loop, you can print out the value of the loop index using the fprintf function or perform any other desired operations.

5. Can I use a for loop to iterate through a matrix in MATLAB?

Yes, you can use a for loop to iterate through a matrix in MATLAB. You can use nested loops to iterate through each row and column or use the built-in functions such as "for each" or "for eachrow" to iterate through the matrix elements. It is important to be careful with indexing and make sure it is within the bounds of the matrix.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
629
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
172
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top