# ApproxFun with Robin boundary conditions

**URL:** <https://discourse.julialang.org/t/approxfun-with-robin-boundary-conditions/113938>\
**Category:** New to Julia\
**Created:** [May 7, 2024, 9:35am UTC](https://discourse.julialang.org/t/approxfun-with-robin-boundary-conditions/113938 "2024-05-07T09:35:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![HankelGretel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hankelgretel/32/209068_2.png) [@HankelGretel](https://discourse.julialang.org/u/HankelGretel)\
**Post date:** [May 7, 2024, 9:35am UTC](https://discourse.julialang.org/t/approxfun-with-robin-boundary-conditions/113938/1 "2024-05-07T09:35:10Z")

</div>

Hi,

I am new to Julia and am interested in using the ApproxFun package to solve 1D boundary value problems. I found interesting examples here : [ApproxFunExamples/PDEs/Manipulate ODEs and PDEs.ipynb at master · JuliaApproximation/ApproxFunExamples · GitHub](https://github.com/JuliaApproximation/ApproxFunExamples/blob/master/PDEs/Manipulate%20ODEs%20and%20PDEs.ipynb) but couldn’t find examples with Robin boundary conditions for instance. Let’s say I want to solve the following:

-u’‘+u=0  
u’(0)=0, u’(1)+u(1)=1

How to implement that using ApproxFun?

Many thanks in advance.

---

<div class="post-metadata">

**Author:** ![jipolanco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jipolanco/32/12129_2.png) [@jipolanco](https://discourse.julialang.org/u/jipolanco)\
**Post date:** [May 7, 2024, 2:08pm UTC](https://discourse.julialang.org/t/approxfun-with-robin-boundary-conditions/113938/2 "2024-05-07T14:08:07Z")

</div>

Hi,

I know this doesn’t directly answer your question, but [BSplineKit.jl](https://github.com/jipolanco/BSplineKit.jl) allows Robin-type boundary conditions.

The first thing you would need to do is to convert your boundary conditions to _homogeneous_ BCs, for instance via the change of variables v = u - 1. Your equation then becomes:

\begin{align\*} -v'' + v &= -1 \\ v'(0) &= 0 \\ v'(1) + v(1) &= 0 \end{align\*}

The interface for solving boundary value problems with BSplineKit is currently not exactly high level. I can explain later how this works, but right now one would do it as follows:

```julia
using BSplineKit
using LinearAlgebra

ts = range(0, 1; length = 101)
B = BSplineBasis(BSplineOrder(4), ts)

# Homogeneous boundary conditions
bc_left = Derivative(1)
bc_right = Derivative(0) + Derivative(1) # Robin-type BC

# Create recombined B-spline basis, such that each element of the basis individually satisfies
# the specified homogeneous BCs
R = RecombinedBSplineBasis(B, bc_left, bc_right)

M″ = galerkin_matrix(R, (Derivative(0), Derivative(2)))
M = galerkin_matrix(R, (Derivative(0), Derivative(0)))

A = M - M″
y = galerkin_projection(x -> -one(x), R) # project right-hand side

vcoefs = A \ y # solve linear system for basis coefficients

v = Spline(R, vcoefs) # finally, create spline with the solution (can be evaluated, plotted, differentiated, ...)

## Verification

v′ = Derivative() * v
v″ = Derivative() * v′

v′(0) # = 0
v(1) + v′(1) # ≈ 0

# Verify graphically that v″ = v + 1

using CairoMakie

fig = Figure()
ax = Axis(fig[1, 1]; xlabel = L"x")
lines!(ax, 0..1, x -> v(x) + 1; label = L"v(x) + 1")
lines!(ax, 0..1, x -> v″(x); linestyle = :dash, label = L"v″(x)")
axislegend(ax; position = (0, 1), labelsize = 20)
fig

```

 ![solution](https://global.discourse-cdn.com/julialang/original/3X/9/5/95fbea08c2da84b4fc91acdcf53dcd2ec4f852af.png)

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [May 7, 2024, 2:23pm UTC](https://discourse.julialang.org/t/approxfun-with-robin-boundary-conditions/113938/3 "2024-05-07T14:23:21Z")

</div>

There is an example of solving a (nonlinear) boundary value problem with mixed boundary conditions here:

> **[Nonlinear Boundary Value Problem · ApproxFun.jl](https://juliaapproximation.github.io/ApproxFun.jl/latest/generated/NonlinearBVP/)**
>
> Documentation for ApproxFun.jl.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [May 7, 2024, 4:05pm UTC](https://discourse.julialang.org/t/approxfun-with-robin-boundary-conditions/113938/4 "2024-05-07T16:05:06Z")

</div>

Have you tried a sum of a `Dirichlet` and a `Neumann` operator, or a sum of `Evaluation` operators? I think this should work.

---

<div class="post-metadata">

**Author:** ![HankelGretel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hankelgretel/32/209068_2.png) [@HankelGretel](https://discourse.julialang.org/u/HankelGretel)\
**Post date:** [May 8, 2024, 8:18am UTC](https://discourse.julialang.org/t/approxfun-with-robin-boundary-conditions/113938/5 "2024-05-08T08:18:29Z")

</div>

Hi,

Thank you everyone for your answers.

@jipolanco: I will see if I can use that for my actual problem, although I would like to use ApproxFun in priority.

To Sevi: My problem is a linear one, thus I don’t want to use Newton, but I will give it a try.

@jishnub: Actually, I tried something like this, but It didn’t work (I am new to Julia so I don’t know the intricacies of these operators yet). I tried for instance

d = Domain(0…1)  
L = -D^2-I  
u = [Neumann(d)+(x\>0.5)\*Dirichlet(d);L] \ [[0,1],0]

but that didn’t give the right solution. How would you impose Robin boundary conditions using a sum of Dirichlet and Neumann operators?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [May 8, 2024, 9:48am UTC](https://discourse.julialang.org/t/approxfun-with-robin-boundary-conditions/113938/6 "2024-05-08T09:48:39Z")

</div>

```julia
julia> d = Domain(0..1);

julia> D = Derivative(Chebyshev(d));

julia> L = -D^2-I;

julia> f = [ldirichlet(); rdirichlet() + rneumann(); L] \ [0, 1, 0];

julia> f(0)
0.0

julia> f(1) + f'(1)
1.0

```

---

<div class="post-metadata">

**Author:** ![HankelGretel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hankelgretel/32/209068_2.png) [@HankelGretel](https://discourse.julialang.org/u/HankelGretel)\
**Post date:** [May 8, 2024, 3:34pm UTC](https://discourse.julialang.org/t/approxfun-with-robin-boundary-conditions/113938/7 "2024-05-08T15:34:43Z")

</div>

Thank you very much @jishnub. Is there an option to add somewhere as I’m interested in a high accuracy approximation? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [May 8, 2024, 4:28pm UTC](https://discourse.julialang.org/t/approxfun-with-robin-boundary-conditions/113938/8 "2024-05-08T16:28:15Z")

</div>

The operator `\` accepts a tolerance, so instead of `A \ b`, you may call it as `(A, b, tolerance=1e-16)` (or whatever tolerance you want).

If you want higher precision, perhaps you could try using a `BigFloat` or a `BigInt` domain? This should also automatically bump the tolerances used. Something like `d = Domain(big(0)..big(1))`.

---

<div class="post-metadata">

**Author:** ![MingjiaYan](https://avatars.discourse-cdn.com/v4/letter/m/ecd19e/32.png) [@MingjiaYan](https://discourse.julialang.org/u/MingjiaYan)\
**Post date:** [May 26, 2025, 10:33pm UTC](https://discourse.julialang.org/t/approxfun-with-robin-boundary-conditions/113938/9 "2025-05-26T22:33:51Z")

</div>

> [@HankelGretel](#):
>
> -u’‘+u=0  
> u’(0)=0, u’(1)+u(1)=1

I’m also working with Robin boundary conditions in my PDE system and had the same confusion. But I think the `Evaluation()` operator works well for most boundary conditions (Dirichlet, Neumann, Robin). And writing the boundary condition operators together with the differential operator into a column vector/matrix solves the problem. Here’s my attempt:

```julia
using ApproxFun, Plots

d = Interval(0,1)
S = Chebyshev(d)
x = Fun(S)
D = Derivative(S)

BC1 = Evaluation(S, 0, 1) # u'(0) = 0
BC2 = Evaluation(S, 1, 1) + Evaluation(S, 1, 0) # u'(1) + u(1) = 1
L = D^2 - I # differential operator: u''- u = 0

sol = [BC1; BC2; L] \ [0; 1; 0*x]

# analytical solutio: u(x) = A(e^x + e^(-x)), where A = 1/(2e)
A = 1/(2*exp(1))
u_analytical = A * (exp(x) + exp(-x))

plot(sol)
plot!(u_analytical, lw=2, linestyle=:dash)

```

 ![Screenshot 2025-05-26 at 23.33.13](https://global.discourse-cdn.com/julialang/original/3X/3/7/37c3be105e541d264a49dd440c69f735c40e0c48.png)
