# Solving 1+u''(x) = b(x) with DiffEqOperators

**URL:** <https://discourse.julialang.org/t/solving-1-u-x-b-x-with-diffeqoperators/78653>\
**Category:** General Usage\
**Tags:** diffeq\
**Created:** [March 29, 2022, 4:49am UTC](https://discourse.julialang.org/t/solving-1-u-x-b-x-with-diffeqoperators/78653 "2022-03-29T04:49:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruvi\_Lecamwasam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ruvi_lecamwasam/32/28374_2.png) [@Ruvi\_Lecamwasam](https://discourse.julialang.org/u/Ruvi_Lecamwasam)\
**Post date:** [March 29, 2022, 4:49am UTC](https://discourse.julialang.org/t/solving-1-u-x-b-x-with-diffeqoperators/78653/1 "2022-03-29T04:49:48Z")

</div>

Suppose b(x) is a function of a variable x, and let Dxx represent the second derivative d^2x/dx^2. Then the differential equation  
1+Dxx u = b  
has solution  
u = (1+Dxx)\b.  
I can’t figure out how to do this with `DiffEqOperators.jl`.

Let’s first consider the simpler equation: Dxx u = b. This can be solved as:

```julia
using DiffEqOperators

L = 2*pi
nx = 100
dx = L/(nx+1)

xpoints = range(dx, step=dx, length=nx)
b = sin.(xpoints)

Dxx = CenteredDifference(2, 2, dx, nx)
bc = Dirichlet0BC(Float64)

sol = (Dxx*bc)\b

```

Based on this I want to do something like `(1+Dzz*bc)\b`. However I can’t see how to make this work. I’ve tried using `I` from the `LinearAlgebra` package, the identity matrix, and a `CenteredDifference` of order zero, but all gave an error. Does anyone know what the solution is?

---

<div class="post-metadata">

**Author:** ![Ruvi\_Lecamwasam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ruvi_lecamwasam/32/28374_2.png) [@Ruvi\_Lecamwasam](https://discourse.julialang.org/u/Ruvi_Lecamwasam)\
**Post date:** [March 29, 2022, 5:31am UTC](https://discourse.julialang.org/t/solving-1-u-x-b-x-with-diffeqoperators/78653/2 "2022-03-29T05:31:11Z")

</div>

Based on [this question](https://discourse.julialang.org/t/solving-linear-system-with-summed-diffeqoperators/60998/5), I think I got it to work via:

```julia
using SparseArrays, LinearAlgebra
Axx,bxx = sparse(Dxx*bc)
Dh = I+Axx

```

and then doing `Dh\b`. Is this the best way to go about it?

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [March 29, 2022, 9:02am UTC](https://discourse.julialang.org/t/solving-1-u-x-b-x-with-diffeqoperators/78653/3 "2022-03-29T09:02:47Z")

</div>

The derivative operators are internally converted to sparse matrices anyway, so your approach is in principle good.

Beware that one needs to solve `(I + Dxx*bc) \ (b-bxx)` though. See [https://github.com/JuliaDiffEq/DiffEqOperators.jl/files/3267835/ghost\_node.pdf](https://github.com/JuliaDiffEq/DiffEqOperators.jl/files/3267835/ghost_node.pdf) for a discussion of ghost nodes and boundary conditions, and take a look at how `DiffEqOperators` implements `\`.
