How can I solve this complex math equation using SymPy?

146 views
Skip to first unread message

Stephen Learmonth

unread,
Jun 29, 2025, 5:32:56 AMJun 29
to sympy

Screenshot 2025-06-29 at 10.31.04.png

Peter Stahlecker

unread,
Jun 29, 2025, 5:47:10 AMJun 29
to sy...@googlegroups.com
At first glance this looks nonlinear in w and in b.
Do you think there exists a closed form solution, or are you looking for a numerical solution given the x(i), y(i)?



On Sun 29. Jun 2025 at 11:33, Stephen Learmonth <stephen.j...@gmail.com> wrote:

Screenshot 2025-06-29 at 10.31.04.png

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/sympy/7229acb9-c0c1-4ed4-89a4-ee1136ad673bn%40googlegroups.com.

stephen.j...@gmail.com

unread,
Jun 29, 2025, 6:02:35 AMJun 29
to sy...@googlegroups.com
Yes, it is non-linear

Closed-form or numerical.

Thanks.

Sent from my iPhone.

On 29 Jun 2025, at 10:47, Peter Stahlecker <peter.st...@gmail.com> wrote:



stephen.j...@gmail.com

unread,
Jun 29, 2025, 6:11:37 AMJun 29
to sy...@googlegroups.com
If you can solve for $m = 3$ to start with that would be great.


Sent from my iPhone.

On 29 Jun 2025, at 11:02, stephen.j...@gmail.com wrote:

Yes, it is non-linear

Peter Stahlecker

unread,
Jun 29, 2025, 6:33:14 AMJun 29
to sy...@googlegroups.com

What woud be the range of x(i), y(i)?
What could be good initial guesses for w, b?
(Needed to solve numerically.)


peter.st...@gmail.com

unread,
Jun 29, 2025, 7:09:13 AMJun 29
to sy...@googlegroups.com

You give df/dwdb = 0 to solve for w and for b.

But this gives only one equation and two are needed.

peter.st...@gmail.com

unread,
Jun 29, 2025, 7:11:18 AMJun 29
to sy...@googlegroups.com

You give only one equation: df(w,b) / dwdb = 0

Two are needed to solve for w and for b.

What other equation is there?

 

From: sy...@googlegroups.com <sy...@googlegroups.com> On Behalf Of stephen.j...@gmail.com
Sent: Sunday, June 29, 2025 12:02 PM
To: sy...@googlegroups.com
Subject: Re: [sympy] How can I solve this complex math equation using SymPy?

 

Yes, it is non-linear

Stephen Learmonth

unread,
Jun 29, 2025, 7:19:30 AMJun 29
to sympy
x(i) can be 1.25, 2.0, 2.4 and y(i) can be 1.75, 1.65, 2.85 for i = 1 to 3.
initial guess for w is +1.043 and b = -0.29.


peter.st...@gmail.com

unread,
Jun 29, 2025, 7:25:54 AMJun 29
to sy...@googlegroups.com

Clear.

But you only give one equation: df(w,b; x, y) /dwdb = 0

Two equations are needed to try and solve for w, b.

 

From: sy...@googlegroups.com <sy...@googlegroups.com> On Behalf Of Stephen Learmonth
Sent: Sunday, June 29, 2025 1:20 PM
To: sympy <sy...@googlegroups.com>
Subject: Re: [sympy] How can I solve this complex math equation using SymPy?

 

x(i) can be 1.25, 2.0, 2.4 and y(i) can be 1.75, 1.65, 2.85 for i = 1 to 3.

Stephen Learmonth

unread,
Jun 29, 2025, 8:21:11 AMJun 29
to sympy
Let me post a reviewed version of the equations to solve:

Screenshot 2025-06-29 at 13.19.38.png

Peter Stahlecker

unread,
Jun 29, 2025, 8:59:47 AMJun 29
to sympy
```
# %%
import numpy as np
import sympy as sm
from scipy.optimize import root

m = 3

w, b = sm.symbols('w, b')
x = [sm.symbols(f'x_{i}') for i in range(m)]
y = [sm.symbols(f'y_{i}') for i in range(m)]


def f(w, b, x, y):
    """Stephen's function"""
    m = len(x)
    if m != len(y):
        raise ValueError("x and y must have the same length")

    summe = sum([sm.sqrt((w**2 * x[i] - w * (y[i] - b))**2) /
                    sm.cos(sm.atan((2 * w**2 * b - w * x[i] + y[i] + b) /
                                    (w**2 * x[i] - w * (y[i] - b))))
                    for i in range(m)])
    return summe / m

dfdw = f(w, b, x, y).diff(w)
dfdb = f(w, b, x, y).diff(b)

gleichung = sm.Matrix([[dfdw, dfdb]])
gleichung_lam = sm.lambdify((w, b, *x, *y), gleichung, cse=True)

# %%
# Set values
x = [1.25, 2.0, 2.4]
y = [1.75, 1.65, 2.85]
if len(x) != m or len(y) != m:
    raise ValueError(f"x and y must have length {m}")


def func(xx, args):
    w, b = xx[0], xx[1]
    return gleichung_lam(w, b, *args).squeeze()

# Initial guess
x0 = [1.043, -0.29]
args = [*x, *y]
# Iterate. This sometimes improves the result
for _ in range(3):
    loesung = root(func, x0, args=args)
    x0 = loesung.x
print("message from root:", loesung.message)
print('error:', loesung.fun )
print(f"w: {loesung.x[0]}, b: {loesung.x[1]}")
````

This seems to work.
I tried it for up to m = 75
NOTE: I replaced abs(a) with sqrt(a**2) as this 'behaves' better numerically.

stephen.j...@gmail.com

unread,
Jun 29, 2025, 10:08:15 AMJun 29
to sy...@googlegroups.com
Thanks.

Can you give me a figure for execution time, when m = 30 say?



Sent from my iPhone.

On 29 Jun 2025, at 13:59, Peter Stahlecker <peter.st...@gmail.com> wrote:

```

Peter Stahlecker

unread,
Jun 29, 2025, 10:28:42 AMJun 29
to sympy
m = 30       0..2 sec
m = 40       1.0 sec
m = 50       1.2 sec
m = 60       1.6 sec
m = 70       2.0 sec

This is on my PC, which is a good, average PC
Message has been deleted

Stephen Learmonth

unread,
Jun 30, 2025, 7:39:01 AMJun 30
to sympy
I have tried executing your code in PyCharm Community Edition but i get no output in the console. Nothing is printed to the console window.

Peter Stahlecker

unread,
Jun 30, 2025, 9:08:56 AMJun 30
to sympy
I do not the the answer to your problem, I do not know what ``PyCharm Community Edition`` is.
I get this output with your numbers:

```
time: 0.024225711822509766 message from root: The solution converged. error: [-4.44089210e-16 2.22044605e-16] w: 0.4474948009099584, b: -0.6156041430138662
```

Jonathan Gutow

unread,
Jun 30, 2025, 9:31:41 AMJun 30
to sy...@googlegroups.com
I use PyCharm for coding packages. I would not use it for this task. As has been suggested before, I encourage you to install Jupyter, numpy, sympy and scipy. Then paste Peter’s code into a cell and run it. You will get cleaner output and it will be easier to keep track of what code has been run in a notebook.

Dr. Jonathan Gutow
Halsey Science Room 412
Chemistry Department
UW Oshkosh
web: https://cms.gutow.uwosh.edu/Gutow
e-mail: gu...@uwosh.edu
Ph: 920-424-1326

Stephen Learmonth

unread,
Jun 30, 2025, 9:45:44 AMJun 30
to sympy
What versoin of python interpreter, sympy and numpy are you using to run this code?

Stephen Learmonth

unread,
Jun 30, 2025, 9:47:50 AMJun 30
to sympy
I'm not getting any output at all.

It shouldn't matter if I use PyCharm or Jupyter Notebook. The only thing that should matter is Python interpreter version, SymPy version, Numpy version  and SciPy version.

Peter Stahlecker

unread,
Jun 30, 2025, 9:57:55 AMJun 30
to sympy
No idea whether what you are stating is correct, I doubt it: why should visibility of the output depend on the numpy version?

This is what I use:
SymPy version: 1.13.3 NumPy version: 2.2.2 SciPy version: 1.15.1 Python version: 3.13.1 | packaged by conda-forge | (main, Jan 13 2025, 09:37:28) [MSC v.1942 64 bit (AMD64)]

Jonathan Gutow

unread,
Jun 30, 2025, 9:58:07 AMJun 30
to sy...@googlegroups.com
Where output goes when you run things within PyCharm depends on exactly how you run it and what options you have set for the virtual environment your code is running in. Your issue is likely to be difficult to diagnose from afar. Again, I suggest you try Jupyter, where the code apparently works.

Dr. Jonathan Gutow
Halsey Science Room 412
Chemistry Department
UW Oshkosh
web: https://cms.gutow.uwosh.edu/Gutow
e-mail: gu...@uwosh.edu
Ph: 920-424-1326

Peter Stahlecker

unread,
Jul 1, 2025, 4:04:37 PMJul 1
to sympy
Your Guide to the Python print() Function – Real Python
Maybe this is of use for your printing issue
Reply all
Reply to author
Forward
0 new messages