Mathematica: speeding up NSolve

  • Context: Mathematica 
  • Thread starter Thread starter Sosi
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary

Discussion Overview

The discussion revolves around optimizing the use of Mathematica's NSolve function for solving a system of equations with multiple dependent variables. Participants explore strategies to speed up the computation process, particularly when searching for real positive solutions across a range of independent variable values.

Discussion Character

  • Technical explanation
  • Exploratory
  • Mathematical reasoning

Main Points Raised

  • One participant describes a slow process using NSolve to find solutions for 13 dependent variables, seeking methods to improve efficiency.
  • Another participant suggests using ParallelTable to speed up the process when varying independent variables in a loop.
  • A method is proposed to discretize the range of an independent variable and use previous solutions as starting points for FindRoot, potentially improving speed without losing roots.
  • A participant expresses concern about not knowing the operating points for dependent variables when varying the independent variable but considers trying the suggested approach.
  • Subsequent replies emphasize the importance of using results from previous evaluations to inform initial guesses for subsequent points, which could lead to faster computations.
  • A later post reports significant improvements in computation time after implementing a function that uses FindRoot, reducing the time from hours to minutes for a larger set of values.

Areas of Agreement / Disagreement

Participants generally agree on the potential benefits of using FindRoot and leveraging previous results to improve efficiency. However, there is uncertainty regarding the initial operating points for dependent variables across different values of the independent variable, indicating that the discussion remains exploratory.

Contextual Notes

Participants mention the need for real positive solutions and the challenges of varying multiple dependent variables, highlighting the complexity of the system and the computational demands involved.

Who May Find This Useful

This discussion may be useful for Mathematica users dealing with complex systems of equations, particularly those looking to optimize their computational strategies for solving such systems efficiently.

Sosi
Messages
13
Reaction score
1
Hello everyone!

I have a system of equations which I am trying to solve in order to 13 dependent variables. Yet, I am only interested in the answers which are real positives. In my code I have implemented a way to select only the answer which satisfies this constraint:

Code:
Cases[
 NSolve[
{equations},{variables}],
 {Rule[_, _?NonNegative] ..},
 1 ]

Yet, this is a very slow process, taking about 14 seconds for each value of an independent value that I am considering (and I want to take into consideration several hundreds).

I know that FindRoot is able to speed up this process, but I want to search for a global (not local) solution. Is there any way to speed up this process?

Thanks so much
 
Physics news on Phys.org
So are you varying the independent variables in some loop for example using Table? If so the easy answer to your question is that you can speed up the process a bit by using ParallelTable.

Another thing that you could try is the following. Say there is just 1 independent parameter x (this could be modified for more but just to illustrate my point) and say you want to vary it over a range from 0 to 1, by discretizing this interval and checking the solution at each discrete point. Schematically you could do it like this:

0 | ... | ... | ...| ...|1

Each . represents a solution using FindRoot, and each | represents a solution using NSolve. Every time you're trying to do a FindRoot, for the initial guess you use the solution from the point to the left (this could be a FindRoot solution, or an NSolve solution). Because x changes only slightly from each point to the next, and presumably the solutions will maybe depend smoothly on x you shouldn't lose any roots this way. If you do lose some roots, this should be apparent the next time you get to a | point.
 
kai_sikorski: thanks for your reply!

My objective is just to get the numerical solution to the system of equations by manually assigning values for an independent variable. Say that the independent variable x varies between 1*^-10 and 1*^-5. In this range, I'm considering ~200 values for x , and then finding the solution to the system of equations.

Your suggestion is a very interesting idea! The issue is that, for each of the 200 values that I attribute to the independent variable, I will have to give a different operating point to the 13 dependent variables. And although I have an idea of the operating points of those dependent variables for the first x value, I have no idea what those operating points may be when I consider other values of x.

Nevertheless, I will try to do what you suggested and see if what I get makes sense :)
 
Last edited:
Sosi said:
And although I have an idea of the operating points of those dependent variables for the first x value, I have no idea what those operating points may be when I consider other values of x.

Well the idea is that when you evaluate the second point, you use the results of the first point. When you evaluate the third point you use the results of the second point. If the system varies relatively smoothly with x, you should always have a pretty good guess for the starting values.
 
kai_sikorski said:
Well the idea is that when you evaluate the second point, you use the results of the first point. When you evaluate the third point you use the results of the second point. If the system varies relatively smoothly with x, you should always have a pretty good guess for the starting values.

Of course.. I wasn't seeing that. Yes, it should indeed work! I'll try it out and hopefully it runs much faster than NSolve. Thanks!
 
Ok, so I setup a function that uses FindRoot to solve the system. I also added a few checkpoints along the run, where the operating points are results taken from a NDSolve command.Results:
Before: System had 10 dependent variables. It took around 5 hours to find solutions to those variables for <100 values of an independent variable.

Now: System has ~15 dependent variables. Takes less than 1 minute to find solution for about 1000 values of an independent variable.

Thanks again for the help, it was very valuable!
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 52 ·
2
Replies
52
Views
13K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 6 ·
Replies
6
Views
8K