# Newton's Method

Can someone here provide a Python script that allows the use of Newton’s Method, where I guess the equation to solve can be either hard-coded in the routine or input through a text box?

Hi @akilli There are some code samples here for newtons method.
This thread discusses (and solves) realtime input calculation in grasshopper.

Putting these two bits of information together should get you started.

2 Likes

Thanks for your suggestions! My goal was to solve another question here. Divide curve ramp - where you can see my definition - by dividing a curve into n geometrically scaled segments so that their sum equaled the original curve. I ended up using a different example for the Python code because I found one that actually approximated the derivative on its own. As for importing the equation to be solved into the Python module via a text box, that still remains a mystery to me, I just hard coded it… If by ‘text box’ you mean a panel, respectively a string as an input.

I am not a big expert but I learnt that equations in python are better represented as a list of the parameters. Same approach is used in MATLAB.

for 2x^2 + 3x -1
You simply use list(2,3,-1)

If you find a way to process the string “2x^2+3x-1” in all possible variations removing spaces, counting the number of “x” reading the signs and the powers, and take into account that the members might be scrambled. Then it makes sense to use the string.

In my opinion it doesn’t.

That’s fine for polynomials but what about anything more exotic?

Sounds like a job for Scipy, but this is not compatible with the Ironpython interpreter in Rhino and GrassHopper. You would need to find a way to interact with an external interpreter…

https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.newton.html

Fourier?
Taylor?

Could you give an example of a fancy/exotic equation?

There’s no point in trying to dazzle you with a complex equation. I was initially hoping there was some kind of parser available in GH Python that I could just feed from a panel but apparently not. @Dancergraham mentioned Scipy, and although I’m clearly not a Python programmer, for years I’ve seen threads addressing this subject and how difficult it is to use Numpy and Scipy with Grasshopper, despite a number of workarounds. you know @akilli, although I could not manage to do it (no time to spend on it) subprocess is a good workaround to getting scipy, numpy or whatever, to parse the string perform the calculation in cpython3 and return the result in IronPython, respectively Rhino/GH