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.stahlec...@gmail.com> wrote:

```
# %%
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 schrieb am Sonntag, 29. Juni 2025 um 14:21:11 UTC+2:
Let me post a reviewed version of the equations to solve:

Screenshot 2025-06-29 at 13.19.38.png

On Sunday, 29 June 2025 at 12:25:54 UTC+1 peter.st...@gmail.com wrote:

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.

initial guess for w is +1.043 and b = -0.29.

 

On Sunday, 29 June 2025 at 11:33:14 UTC+1 peter.st...@gmail.com wrote:

 

What woud be the range of x(i), y(i)?

What could be good initial guesses for w, b?

(Needed to solve numerically.)

 

 

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

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

 

Closed-form or numerical.

 

Thanks.

 

Sent from my iPhone.



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



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.

--
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/CABKqA0YHe5GCWx8C6EiYA%3DuA1dCq1ij7dvrV%2Bfi22Pbe8L-W0A%40mail.gmail.com.

--
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.

--
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.

--
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+unsubscr...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/sympy/ee36cabb-1a69-4984-bc51-3539cdcb030fn%40googlegroups.com.

--
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+unsubscr...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/sympy/BD8C0F3D-FDE8-44D1-96E8-C77CE3905CAD%40gmail.com.

Reply via email to