1

I am trying to find the zero's of several straight lines solving one of them at a time with fsolve function. I can't manage to write a decent code that will do this, this below is my best attempt so far, any help is very much appreciated. I think the best way to do it is to define a class (the class being the line with its two properties i.e the slope and the y intercept) but I do not know how to do this.

import numpy as np
from scipy.optimize import fsolve


def straight_line(parameters):
    m = parameters[0]             # This is the first parameter of the line i.e the slope
    n = parameters[1]             # This is the second parameter of the line i.e. the y-axis intercept
    x = parameters[3]             # This is the variable of the function, I want to find x such that m * x + n = 0
    return m * x + n


for m in range(-10,10):
    for n in range(-10,10):
        guess = 1
        zero = fsolve(straight_line([m, n]), guess)   # This is not correct
        print([m, n, zero])
Miguel Garcia
  • 97
  • 1
  • 8
  • What does "This is not correct" mean? What is the output? What do you expect it to be instead? – Code-Apprentice Jan 11 '21 at 15:49
  • Hello @Code-Apprentice I mean that the line is not correct (the interpreter will not understand that line). I just wrote it because I think it reflects well what I want the program to do, so it will be useful to explain myself. Inside the nested loops I wanna find the zero of the straight line but in each loop iteration the line parameters (m,n) should change i.e. each time is a different equation I am solving – Miguel Garcia Jan 11 '21 at 15:57
  • "the line is not correct" and "the interpreter will not understand that line" still doesn't tell us what happens when you run your code. Please [edit] your question to show the exact output when you run your program. – Code-Apprentice Jan 11 '21 at 16:03
  • The problem here is that you are not using `fsolve()` correctly. I suggest you find some tutorials and read the documentation to understand how to correctly call it to do what you want. – Code-Apprentice Jan 11 '21 at 16:04

1 Answers1

1
zero = fsolve(straight_line([m, n]), guess)

The problem is that you call straight_line() and send the calculated value to fsolve. If you read the documentation, you will see that the first parameter to fsolve, must be a "callable". In other words, you need to pass the function itself:

zero = fsolve(straight_line, guess)

You also need to pass the args to define the line, in this case the slope and y-intercept:

zero = fsolve(straight_line, guess, args=(m, n))

Also, you have to make sure the x value is the first parameter to straight_line():

def straight_line(x, m, b):
    return m*x + b

I haven't tested this, so it might not be exactly correct. It might not fix all the problems. I suggest you read more tutorials and examples to learn about how this works. Refer to the documentation to be sure you are using fsolve() correctly.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268