# ODE solution in Symbolics/SymbolicNumericIntegration vs. ModelingToolkit

**URL:** https://discourse.julialang.org/t/ode-solution-in-symbolics-symbolicnumericintegration-vs-modelingtoolkit/100893
**Category:** Modelling & Simulations
**Created:** [June 27, 2023, 2:32pm UTC](https://discourse.julialang.org/t/ode-solution-in-symbolics-symbolicnumericintegration-vs-modelingtoolkit/100893 "2023-06-27T14:32:45Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [June 27, 2023, 2:32pm UTC](https://discourse.julialang.org/t/ode-solution-in-symbolics-symbolicnumericintegration-vs-modelingtoolkit/100893/1 "2023-06-27T14:32:45Z")

</div>

I’m comparing the analytic and numeric solutions of a linear, second order ODE, and get some weird (arbitrary) result with the analytic solution found by using SymbolicNumericIntegration… I _assume_ I’m using the tools incorrectly…

Here is the ODE I consider:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/4/d4ec9f88ddfdcd52e4944c9467fa2ca2316eda39.png)

With x\_1(0) = 1 and x\_2(0) = -1, and a unit step input for u(t) at t=0, the solutions I find are:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/f/2fe03cdf2e501bdc3aaf8c1f792dd1d3e9366d27.png)  
where superscript “a” indicates analytic solution. NOTE that the analytic (solid) and numeric (dashed) solutions seem to diverge at the end of the time period.

The system is _unstable_, so a divergence might be possible. However, every time I _plot_ the _analytic_ solution, I get a different result. I’m not talking about _re-solving_ the solution analytically; I’m talking about re-_plotting_ the found solution. So this indicates that there is something wrong with the way I represent the analytic solution.

- What is it I do that is wrong?

Consider:

\frac{dx}{dt} = Ax+Bu

leading to:

x(t) = \Phi(t)\cdot \left( x(0) + \int\_0^t \Phi(-\tau)Bu(\tau)d\tau \right)

where

\Phi(t) = \exp(At)

My code is as follows:

```julia
using SymbolicNumericIntegration
using Plots

@variables τ t

Φ(t) = [1//4*exp(-10t) + 3//4*exp(t) -3//4*exp(-10t) + 3//4*exp(t);
        -1//4*exp(-10t) + 1//4*exp(t) 3//4*exp(-10t) + 1//4*exp(t)]
B = [1;0]
x0 = [1; -1]

# constant input
intg(τ) = [i[1] for i in integrate.(Φ(-τ)*B,τ)] # stripping off errors, etc.

intg_def(t) = intg(t) .- substitute.(intg(τ),(Dict(τ=>0),))

x(t) = simplify.(expand.(Φ(t)*x0 .+ Φ(t)*intg_def(t)))

Tf = 3.8
#
plot(x(t)[1], xlim=(0,Tf),lw=2.5, lc=:blue,la=0.3,label=L"x_1^\mathrm{a}")
plot!(x(t)[2], xlim=(0,Tf),lw=2.5, lc=:red,la=0.3,label=L"x_2^\mathrm{a}")
plot!(title=L"Diagonalizable system with input $u(t)=1$")
plot!(xlabel=L"t")

```

---

<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: [June 27, 2023, 3:09pm UTC](https://discourse.julialang.org/t/ode-solution-in-symbolics-symbolicnumericintegration-vs-modelingtoolkit/100893/2 "2023-06-27T15:09:45Z")

</div>

Always check the analytical result by using differentiation.

(We should do that automatically and throw a warning)

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [June 27, 2023, 3:33pm UTC](https://discourse.julialang.org/t/ode-solution-in-symbolics-symbolicnumericintegration-vs-modelingtoolkit/100893/5 "2023-06-27T15:33:30Z")

</div>

It is not completely clear to me how to do the differentiation… my solution `x(t)` is a function of symbolic variable `t`. Doing `Differentiate(x)` of `x(t)` produces `0` (zero), while `Differentiate(x(t))` gives an error message…

Because the analytic result is almost identical to the numeric result over most of the time span, and because if I re-do the plotting of the analytic results, there is only a slight “wiggling” at the largest values of the time span, I think the solution is correct… so I’m suspecting something wrong in the way I have created the functions – or possibly that the plotting recipe doesn’t understand the function properly.

---

<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: [June 27, 2023, 6:07pm UTC](https://discourse.julialang.org/t/ode-solution-in-symbolics-symbolicnumericintegration-vs-modelingtoolkit/100893/6 "2023-06-27T18:07:40Z")

</div>

[https://symbolics.juliasymbolics.org/stable/manual/derivatives/](https://symbolics.juliasymbolics.org/stable/manual/derivatives/)
