Pavel.D
Pavel.D

Reputation: 581

Scipy fsolve solving an equation with specific demand

Below a sample of code consists an equation, it uses scipy.fsolve to find zero with x-axis,

1- How to run fsolve and ask to search within a specific interval f.ex. -75 and 75?

2- Sometimes the demand is to find x value when result becomes 2000, how that can be achieve by fsolve?

from scipy.optimize import fsolve

def func(x, a, b, c):
    result = a * x ** 2 + b * x + c
    return  result

result = fsolve(func, 0.01, args = (1, 2, 3))
print(result)


import matplotlib.pyplot as plt
import numpy as np

# Data for plotting
x = np.arange(-100.0, 100.0, 0.1)
fig, ax = plt.subplots()
ax.plot(x, func(x, 1, 2, 3))

ax.set(xlabel='x', ylabel='result',
       title='Just for illustration')
ax.grid()
plt.show()

Showing graphically.

enter image description here

Upvotes: 0

Views: 283

Answers (1)

ev-br
ev-br

Reputation: 26040

  1. fsolve does not support bounds. Prefer brentq if you have a localization interval.

  2. Just find a root of lambda x, a, b, c: func(x, a, b c) -2000

Upvotes: 1

Related Questions