Python curves intersection with fsolve() and function arguments using numpy

2024/10/18 15:03:33

I am trying to use fsolve as quoted here : http://glowingpython.blogspot.gr/2011/05/hot-to-find-intersection-of-two.html,

On order to find the intersection between two curves. Both curves basically are two arrays of floats.

The first of them is a one dimension array Pmech ( Pmech(x) ) and the second is a two dimension array Pair ( Pair(x,y) )

The x - axis is common for both arrays ,so what i want to do is for every y to see where Pair and Pmech intersect.

I am aware of the fact that fsolve() take as arguments functions, not arrays so I wrote two basic functions to implement this feature:

def Pmix(x):return Pmech[x]def Paera(x,y):return Pair[x,y]

So as demonstrated in the above link I implemented the findIntersection function :

def findIntersection(fun1,fun2,x0): return fsolve(lambda x: (fun1(x) - fun2(x,y) for y in range(1,100)),x0)

but I get the following error :

TypeError: float() argument must be a string or a number
Traceback (most recent call last):File "batteries.py", line 261, in <module>findIntersection(Pmix,Paera,0)File "batteries.py", line 238, in findIntersectionfsolve(lambda x: (fun1(x) - fun2(x,y) for y in range(1,100) ),x0)File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 125, in fsolvemaxfev, ml, mu, epsfcn, factor, diag)
minpack.error: Result from function call is not a proper array of floats.
Answer
from scipy.optimize import fsolvedef pmix(x):return xdef paera(x, y):return x**2 - y**2def findIntersection(fun1, fun2, x0):return [fsolve(lambda x:fun1(x)-fun2(x, y), x0) for y in range(1, 10)]print findIntersection(pmix, paera, 0)
https://en.xdnf.cn/q/72802.html

Related Q&A

What is the Python freeze process?

The Python Doc states:Frozen modules are modules written in Python whose compiled byte-codeobject is incorporated into a custom-built Python interpreter byPython’s freeze utility. See Tools/freeze/ fo…

Is there a way to get the top k values per row of a numpy array (Python)?

Given a numpy array of the form below:x = [[4.,3.,2.,1.,8.],[1.2,3.1,0.,9.2,5.5],[0.2,7.0,4.4,0.2,1.3]]is there a way to retain the top-3 values in each row and set others to zero in python (without an…

Python with tcpdump in a subprocess: how to close subprocess properly?

I have a Python script to capture network traffic with tcpdump in a subprocess:p = subprocess.Popen([tcpdump, -I, -i, en1,-w, cap.pcap], stdout=subprocess.PIPE) time.sleep(10) p.kill()When this script …

How to install GDB with Python support on Windows 7

I need to debug cython code. Official documentation says, I need to install "gdb 7.2 or higher, built with Python support". Unfortunately I didnt find any step-by-step guide how to install it…

Pip3 is unable to install requirements.txt during docker build

I am using docker tutorial (https://docs.docker.com/language/python/build-images/) to build a simple python app. Using freeze command I made requirements.txt file which consists a lot of packages. When…

__del__ at program end

Suppose there is a program with a couple of objects living in it at runtime.Is the __del__ method of each object called when the programs ends?If yes I could for example do something like this:class C…

PySpark groupby and max value selection

I have a PySpark dataframe likename city datesatya Mumbai 13/10/2016satya Pune 02/11/2016satya Mumbai 22/11/2016satya Pune 29/11/2016satya Delhi 30/11/2016panda Delhi 29/11/2016…

Nesting descriptors/decorators in python

Im having a hard time understanding what happens when I try to nest descriptors/decorators. Im using python 2.7.For example, lets take the following simplified versions of property and classmethod:clas…

Retrieve definition for parenthesized abbreviation, based on letter count

I need to retrieve the definition of an acronym based on the number of letters enclosed in parentheses. For the data Im dealing with, the number of letters in parentheses corresponds to the number of w…

Python (Watchdog) - Waiting for file to be created correctly

Im new to Python and Im trying to implement a good "file creation" detection. If I do not put a time.sleep(x) my files are elaborated in a wrong way since they are still being "created&q…