![]() The downside is that the memory requirements of doing it my way go up with the number of terms pretty steeply doing 100 million terms takes about 800 MB of RAM, although only for that 1 second that the code is actually running. I can actually do the first 100 million terms of the sequence in a single line of code that takes about 1 second to execute, giving me an error of around 9.56e-9. or at least, not unless you want to get down to pretty small tolerances. Now, if you want to get really smart about it, you actually don't need the while loop either. Storing a value in an already-big-enough matrix is WAY faster in Matlab than extending the size of a matrix by a single row in order to add a new value. And it will also save a lot of computation time if you pre-allocate A to be at least as big as it eventually needs to be (e.g., A = zeros(10000000, 2) ) so Matlab isn't constantly having to resize it. And if you NEED to save all those values, at least you can clear up the bug that's in your loop. ![]() I'd still get rid of it until you figure out some of the other stuff. So just cut out that entire for loop and you will speed things up quite a bit.Įdit: Whoops, just re-read and remembered that you need what's in A eventually for something else. Partly because there is a bug in your for loop so it isn't working the way it should anyway. Partly because increasing the size of a matrix is another super-slow operation in Matlab. Partly because you don't seem to use the values in A anyway. Similarly, stop storing values in A every run through. You could potentially speed up your code by a factor of hundreds just by doing this. Printing stuff out to the screen is a zillion times slower than doing the actual calculations. Stop doing disp every run through your loop. If you are looking to speed up your code in general, though, here are some tips: ![]() for 10,000 terms, the error is about 9.55e-5 for 100,000 terms, the error is about 9.55e-6 etc.) (In fact, for a 10x increase in # of terms, you get almost exactly a 10x increase in precision. Well, to get a certain tolerance, you have to go out a certain number of terms in the infinite series.
0 Comments
Leave a Reply. |