Code doesn't work on latest version of matlab.

I wrote a Newton solver in Matlab 2008 and it worked brilliantly. I now try it on an later version of matlab and it stops giving me the same results. I am at a loss as why this should be. Any suggestions?

2 Comments

Stephen23
Stephen23 on 19 Jul 2016
Edited: Stephen23 on 19 Jul 2016
@Mat Hunt: note that you should use consistent indentation and code formatting to make your code easier to follow. With MATLAB it is very simple, because the editor does it for you by default. If for some reason the indentation has been lost, simply select the code and the press ctrl + i.
Also you should never use error as a variable name, because this is the name of a very important inbuilt function. Likewise you should never use size, length, i, j, cell, etc, etc.

Sign in to comment.

 Accepted Answer

Stephen23
Stephen23 on 19 Jul 2016
Edited: Stephen23 on 19 Jul 2016
MATLAB function outputs can change between versions:
as can the availability of functions, and what arguments they accept. There is no guarantee that code written for one version should work on another. It is up to the user to make their code work on versions different than that it was written for. For this reason MATLAB provides documentation on changes between releases:

8 Comments

Mat Hunt's "Answer" moved here:
If you look at the code, I only use the solver for a linear set of equations. This is essentially the only in built matlab function I use. I can't believe that this would change so much as to make the program give wrong answers?
So I guess my next question is, can anyone suggest on how to make my code more robust?
Mat
Stephen23
Stephen23 on 19 Jul 2016
Edited: Stephen23 on 19 Jul 2016
"give wrong answers" is a totally different matter to "stops giving me the same results"
Please select 1, 2, or 3:
  1. The results are equivalent to numerical precision, or
  2. One of the results is totally wrong (how do you check?), or
  3. I don't know what the difference between 1. and 2. is.
The code no longer converges to an answer when previously it did, so 2.
I have the results in my PhD thesis and I reran the code to see if I could get the same results and it didn't even converge.
Stephen23
Stephen23 on 19 Jul 2016
Edited: Stephen23 on 19 Jul 2016
@Mat Hunt: which two MATLAB versions that you have tried this code on ?
I know it works in matlab 2008a and I have tried it on 2013b and it doesn't work.
Stephen23
Stephen23 on 19 Jul 2016
Edited: Stephen23 on 19 Jul 2016
@Mat Hunt: it is quite possible that Newton's method is susceptible to instability when it is used on this problem. This would mean small perturbations, e.g. changes in initial values, or changes in intermediate results (which could change due to different MATLAB versions) could have a significant effect on the behavior of Newton's method.
There may be a bug in your implementation, that only becomes significant for the same reasons.
At this point you should probably put on your debugging hat: run the code on both versions, and stop after the first convergence iterations: compare all intermediate numeric results: identify which operations are involved. Work backwards if differences are found, work forwards into the next iteration if none are found...
good luck!
The maths of the numerical solution is absolutely fine. It worked before and gave the answers expected. The code using matlab 2008a converged in around 3-4 steps showing that it was indeed a good method for the problem. The only issue is why doesn't it work in the 2013b version.
"The code using matlab 2008a converged in around 3-4 steps showing that it was indeed a good method for the problem"
Convergence in a few steps with one case does not prove anything about how suitable the method is for generally solving that problem: it might still be susceptible to slight changes in start values or due to the exact calculations performed. It could be a chaotic system when solved with that method.
In any case, you should start debugging to find out where the difference is.

Sign in to comment.

More Answers (0)

Categories

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

Asked:

on 19 Jul 2016

Commented:

on 19 Jul 2016

Community Treasure Hunt

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

Start Hunting!