# Dimension mismatch error

**URL:** <https://discourse.julialang.org/t/dimension-mismatch-error/122590>\
**Category:** Performance\
**Tags:** plotting\
**Created:** [November 13, 2024, 2:55pm UTC](https://discourse.julialang.org/t/dimension-mismatch-error/122590 "2024-11-13T14:55:22Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 13, 2024, 6:00pm UTC](https://discourse.julialang.org/t/dimension-mismatch-error/122590/7 "2024-11-13T18:00:05Z")

</div>

> [@Vegetasan](#):
>
> The problem arises because I’m using the `diff` function to calculate the reaction rate (i.e., the change in mass over time).

On a separate note: using a finite difference to calculate the reaction rate is not ideal — you are throwing away a lot of the accuracy of the solution.

You can get the “exact” time derivative (or at least, as accurate as your solution) just by plugging the solution into your right-hand-side function `crnn!`. But even more simply you can do e.g.

```julia
dmass_r1_dt = getindex.(sol1.(time, Val{1}), 1) # Val{1} = first derivative

```

or replace the `getindex` call with

```julia
dmass_r1_dt = sol1.(time, Val{1}, idxs=1)

```

or

```julia
dmass_r1_dt = sol1(time, Val{1}, idxs=1).u

```

See also [DifferentialEquations and derivatives of solutions?](https://discourse.julialang.org/t/differentialequations-and-derivatives-of-solutions/49995) and [“Interpolations and Calculating Derivatives”](https://docs.sciml.ai/DiffEqDocs/stable/basics/solution/#Interpolations-and-Calculating-Derivatives) in the manual.

---

_[View the full topic](https://discourse.julialang.org/t/dimension-mismatch-error/122590)._
