Troubleshooting Matlab Loops: Discovering a Simple Solution | Get Help Now

In summary, there is a problem with the index of the array V not being an integer when running a for loop in Matlab. This is due to the way binary fractions are represented in computers and the accumulated error that occurs when adding 1/10 multiple times. This can be confirmed by checking s-fix(s) which will show a number in the range of 1*10^-16.
  • #1
mikaili
3
0
I have recently found a silly problem with Matlab loop
try the following simple program :
>> clear all
>> for s=0:0.1:1
>> V(10*s+1)=s;
>> end
it will get into trouble when s reaches 0.6

would anybody please check and let me know the result
 
Physics news on Phys.org
  • #2
I don't have Matlab, so I can't check to see what happens. I suspect that you run into a problem with the index of the array V not being an integer.

If this is the problem you're having, the reason it's happening is that certain numbers that have a nice, neat representation in decimal fractions aren't represented exactly as binary real numbers. One decimal fraction in particular that has this problem is 0.1.

A binary fraction as used in many programming languages uses powers of 1/2. For example, you could think of 3/4 as being represented as .11, meaning 1 * 1/2 + 1 * (1/2)^2. The actual representation is a bit more complicated, but this is the idea.

Just as 1/3 requires an infinite number of decimal places to represent, so does 1/10 as a binary fraction. Since computers don't have an infinite amount of storage available, what you get for the representation of 1/10 in binary is a number that is close, but not exactly correct. The problem gets worse if you add 1/10 to itself a few times, such as what happens in your loop. Apparently, by the time s is .6, the accumulated error is enough so that 10*s + 1 comes out to something like 6.99 or so, instead of 7, and then Matlab complains because you are trying to access an array with an index that isn't an integer.
 
  • #3
Thanks Mark44 , Your answer was convincing . Actually when I checked s-fix(s) it was a number in the range of 1*10^-16
 

Related to Troubleshooting Matlab Loops: Discovering a Simple Solution | Get Help Now

What is the purpose of troubleshooting Matlab loops?

The purpose of troubleshooting Matlab loops is to identify and solve any errors or bugs that may be causing the loop to malfunction. This ensures that the loop will run smoothly and produce the desired results.

What are some common issues that can cause Matlab loops to fail?

Some common issues that can cause Matlab loops to fail include syntax errors, logical errors, and infinite loops. These can occur due to typos, incorrect use of loop syntax, or incorrect logic in the loop code.

How can I debug my Matlab loop?

To debug your Matlab loop, you can use the built-in debugging tools such as the "dbstop" function or the "debug" button in the Matlab editor. These allow you to step through your code line by line and check the values of variables at each step to identify where the error is occurring.

What is the best approach to troubleshooting Matlab loops?

The best approach to troubleshooting Matlab loops is to break down the loop into smaller parts and test each part individually. This helps to isolate the source of the error and makes it easier to find a solution. Additionally, using debugging tools and carefully reviewing any error messages can also be helpful.

Where can I get help if I am unable to troubleshoot my Matlab loop?

If you are unable to troubleshoot your Matlab loop on your own, you can seek help from online forums, tutorial websites, or Matlab's official support team. You can also consult with other Matlab users or colleagues who may have experience with similar issues.

Similar threads

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