# ExtraVariableSystemException while working with DiffEqOperators.jl

**URL:** <https://discourse.julialang.org/t/extravariablesystemexception-while-working-with-diffeqoperators-jl/72535>\
**Category:** Modelling & Simulations\
**Tags:** diffeq\
**Created:** [December 3, 2021, 6:16pm UTC](https://discourse.julialang.org/t/extravariablesystemexception-while-working-with-diffeqoperators-jl/72535 "2021-12-03T18:16:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![youainti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/youainti/32/30451_2.png) [@youainti](https://discourse.julialang.org/u/youainti)\
**Post date:** [December 3, 2021, 6:16pm UTC](https://discourse.julialang.org/t/extravariablesystemexception-while-working-with-diffeqoperators-jl/72535/1 "2021-12-03T18:16:36Z")

</div>

I’ve run into two issues while using DiffEqOperators.jl to solve the Feynman-Kac Formula through the method of lines.

Here is the setup to the problem:

```nohighlight
#setup symbols
@parameters τ y
@variables f(..)
Dτ = Differential(τ)
Dy = Differential(y)
Dyy = Differential(y)^2

#setup parameters

ρ = 0.025

#parameters
σ = 5.0
θ = 3.0
μ = 50.0

λ = 0.5

#bounds
T = 20
y_min = 1e4
y_max = 1e4 +20

#domains
dom = [ τ ∈ Interval(0.0,T),
		y ∈ Interval(y_min,y_max)]

#define the feynman-kac equation
equation = Dτ(f(τ,y)) + θ*(μ-y)*Dy(f(τ,y)) + (σ^2)/2 * Dyy(f(τ,y)) ~ - ρ*f(τ,y)

boundary_conditions = [
	f(T,y) ~ 0.6
	,f(τ,y_min) ~ 0 #no probability at minimum
	,f(τ,y_max) ~ 0 #No probability at the high point
]

fk = PDESystem(equation,boundary_conditions,dom,[τ,y],[f(τ,y)],name=:feynman_kac);

Δy = 1 #just to get the error to show up easily
discrete = MOLFiniteDifference([y=>Δy]
	,τ
	;centered_order=2
)

```

when I attempt to discretize the problem using

```julia
discretize(fk,discrete)

```

I get

```julia
ExtraVariablesSystemException: The system is unbalanced. There are 21 highest order derivative variables and 19 equations.
More variables than equations, here are the potential extra variable(s):
f[2](τ)
f[3](τ)
#etc

```

Depending on the specific choice of boundaries and \Delta y I’ll get either an off by one or off by two error on the derivative variables vs equations.

I’m understanding that there is a difference in the dimensions of the estimated derivatives and the equation approximation. Is that correct?

I suspect it is some oversight on my part as I’m quite new to solving PDEs and julia in general.

Thanks in advance for your help.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 4, 2021, 3:53pm UTC](https://discourse.julialang.org/t/extravariablesystemexception-while-working-with-diffeqoperators-jl/72535/2 "2021-12-04T15:53:01Z")

</div>

It’s not you, the discretizer is just a bit dumb right now. There’s a big warning sign on the top of the documentation that it’s still heavily being worked on, and that will be removed when it’s stable. Until then, there’s a lot of edge cases.

Here, the problem is that it currently doesn’t reorder variables. So you need to define the equation as:

```julia
equation = Dτ(f(τ,y)) ~ - ρ*f(τ,y) - θ*(μ-y)*Dy(f(τ,y)) - (σ^2)/2 * Dyy(f(τ,y))

```

so it picks up that it’s an ODE. But then you have to flip the signs because it doesn’t recognize the terminal condition at `T`, and wants an initial condition. So this works:

```julia
using DiffEqOperators, ModelingToolkit, DomainSets

#setup symbols
@parameters τ y
@variables f(..)
Dτ = Differential(τ)
Dy = Differential(y)
Dyy = Differential(y)^2

#setup parameters

ρ = 0.025

#parameters
σ = 5.0
θ = 3.0
μ = 50.0

λ = 0.5

#bounds
T = 20
y_min = 1e4
y_max = 1e4 +20

#domains
dom = [ τ ∈ Interval(0.0,T),
		y ∈ Interval(y_min,y_max)]

#define the feynman-kac equation
equation = Dτ(f(τ,y)) ~ ρ*f(τ,y) + θ*(μ-y)*Dy(f(τ,y)) + (σ^2)/2 * Dyy(f(τ,y))

boundary_conditions = [
	f(0.0,y) ~ 0.6
	,f(τ,y_min) ~ 0 #no probability at minimum
	,f(τ,y_max) ~ 0 #No probability at the high point
]

fk = PDESystem(equation,boundary_conditions,dom,[τ,y],[f(τ,y)],name=:feynman_kac);

Δy = 1 #just to get the error to show up easily
discrete = MOLFiniteDifference([y=>Δy]
	,τ
	;centered_order=2
)
discretize(fk,discrete)

```

and that explains the reason why the documentation said it’s still being developed so use at your own risk 😅 . It will be really cool in 2023 though.

---

<div class="post-metadata">

**Author:** ![youainti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/youainti/32/30451_2.png) [@youainti](https://discourse.julialang.org/u/youainti)\
**Post date:** [December 6, 2021, 11:39pm UTC](https://discourse.julialang.org/t/extravariablesystemexception-while-working-with-diffeqoperators-jl/72535/3 "2021-12-06T23:39:03Z")

</div>

Thanks for the follow up, I’m giving it a try.
