A bug with vector lengths?

Hey everyone.
Without going into too much unnecessary detail, I'll try and explain this strange problem I'm having.
I've been writing some optimization code, in which I have a set of starting values (importantly, containing a value N which specifies vector length), which are repeatedly adjusted and rated for suitability using an objective function. All of this takes place in a loop.
The objective function always (purposely) outputs a vector of N+1 values, which is then used as the standard length for the rest of what I'm doing in the loop.
This all usually works absolutely fine, but recently I have come across a set of values where N doesn't increase to N+1 as it usually should, and hence it causes errors in the rest of the code. This set of values is nothing out of the ordinary, and furthermore, when I break the code down and run it in sections with these values, N increases to N+1 as it should - so the error only happens while running in a loop.
Therefore, there doesn't seem to be any problem with my code - so I'm just wondering if anyone has experienced something similar or knows of any bugs that produce this behaviour in Matlab?
Hope that wasn't too complicated. Thanks in advance if you have any advice!

6 Comments

I am afraid that without a snippet that reproduces the issue it's gonna be hard to give some feedback.
What if I told you that "while driving on a road I suddenly crushed my car but usually it doesn't happen, would you happen to know what might have caused it?"
In that case I'll do some more searching myself and get back to you - the code I have is written as many several function scripts, and the trail involves looking at several of them at once so it may be pretty hard to explain clearly on here!
  1. Is there an error message?
  2. Does it happens in a static workspace?
1. Yes, I get the 'Index exceeds matrix dimensions' error. This is because the script is meant to start creating vectors of length N+1, but in this case only creates vectors of length N. Hence somewhere further along the line the code tries to access the N+1 value of a vector, which of course isn't there.
2. I'm not sure what you mean by static workspace? Enlighten me and I'll give you a better answer!
have you set it to go into debug mode when the error occurs? Write
dbstop if error
Ok, I think this is something like floating point error, which means I'm an eejit as I thought I'd already checked for it. I got it working now, just want to confirm that my thinking is right. Two bits lines of code:
N=N*d/L;
x=linspace(0,d,N+1);
In the case I'm working on, d should always be equal to L. In earlier functions in my code, L is simply being put as an input for d. But when I execute the linspace, its length stays as N instead of N+1. When I specifically make d=L in the command window, then x will become the correct length.
Because of this I'm concluding that some sort of error is creeping in somewhere such that d/L is a fraction below 1, which affects the linspace output. I simply put N = round(N*d/L) and this has fixed it.
Have i got the right idea here?

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 26 Jul 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!