4
$\begingroup$

I need to solve a boundary value problem (BVP) of second order, where the equation depends on several know parameters, which are geometric parameters and material constants.

I would like to solve this equation for several combinations of values of the known parameters. Is it possible to do this using solve_bvp()? I am thinking along the lines of the args parameter in solve_ivp().

In case my question is unclear, imagine I want to solve the Bratu equation from the solve_bvp() documentation for several values of k, without having to each time change the value of k in the function fun(x,y). How can this be accomplished?

$\endgroup$
3
  • $\begingroup$ The documentation says you can write a function of the form f(x,y,p) where p will be an array of additional parameters. So you could just pass k in as an element of p. $\endgroup$
    – Tyberius
    Commented Dec 12, 2021 at 1:12
  • $\begingroup$ Thanks @Tyberius for your comment but the documentation says "p is a k-D vector of unknown parameters ". In my case, the parameters are known $\endgroup$
    – Ken Grimes
    Commented Dec 12, 2021 at 11:30
  • $\begingroup$ Sorry, I had glossed over that in the docs. I think I may have found an alternative solution that I added as an answer. $\endgroup$
    – Tyberius
    Commented Dec 12, 2021 at 16:25

1 Answer 1

2
$\begingroup$

It looks like solve_ivp also didn't have args until fairly recently, see the issue on GitHub.

The workaround they suggest there is to use a lambda expression around your function, which will have the other arguments set as keywords.

For the Bratu equation, reworking the example from the documentation, I believe this would look like:

def fun(x, y,k=0):
    return np.vstack((y[1], -k*np.exp(y[0])))

def bc(ya, yb):
    return np.array([ya[0], yb[0]])

x = np.linspace(0, 1, 5)
y_a = np.zeros((2, x.size))

res_a = solve_bvp(lambda x,y: fun(x,y,k=1), bc, x, y_a)
$\endgroup$
2
  • $\begingroup$ why do you define k=0 in the definition of fun? $\endgroup$ Commented Jun 6, 2023 at 18:25
  • $\begingroup$ @RonShvartsman this was just to make clear that k is a keyword argument, rather than a positional one. I could also have written def fun(x,y,*,k) to make it a keyword without setting a default value. $\endgroup$
    – Tyberius
    Commented Jun 6, 2023 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.