Hello everyone,
I’m working on a project in Julia to predict reaction mechanisms for a given set of inputs using kinetic modeling. I’m fairly new to Julia, and everything seems to be running well except for a dimension mismatch error in the plotting section.
The Issue
The problem arises because I’m using the diff
function to calculate the reaction rate (i.e., the change in mass over time). This results in an array with a length of ( n - 1 ), whereas the temperature array, derived from the solution time points, has a length of ( n ). I attempted to adjust by truncating the temperature array (Temp[1:end-1]
) to match the length of the diff
output, but the error persists.
Code Snippet
Here’s the relevant part of my code and issue:
# Adjust Temp to match the length of `diff(mass_r1)` and `diff(time)`
plot(Temp[1:end-1] .- 273.15, -diff(mass_r1) ./ diff(time), label="R1", linewidth=1.5)
plot!(Temp[1:end-1] .- 273.15, -diff(mass_r2) ./ diff(time), label="R2", linewidth=1.5)
plot!(Temp[1:end-1] .- 273.15, -diff(mass_r3) ./ diff(time), label="R3", linewidth=1.5)
plot!(Temp[1:end-1] .- 273.15, -diff(mass_r4) ./ diff(time), label="R4", linewidth=1.5)
DimensionMismatch: arrays could not be broadcast to a common size; got a dimension with lengths 7 and 5
Stacktrace:
[1] _bcs1
@ .\broadcast.jl:529 [inlined]
[2] _bcs
@ .\broadcast.jl:523 [inlined]
[3] broadcast_shape
@ .\broadcast.jl:517 [inlined]
[4] combine_axes
@ .\broadcast.jl:512 [inlined]
[5] instantiate
@ .\broadcast.jl:294 [inlined]
[6] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, typeof(/), Tuple{Vector{Float64}, Vector{Float64}}})
@ Base.Broadcast .\broadcast.jl:873
[7] top-level scope
@ In[7]:3
My Question
What would be the best way to handle the dimension mismatch here? Is there a more effective or Julia-friendly approach to ensure that the arrays match up correctly? I’d like to know if there’s a way to plot reaction rates calculated with diff
against temperature in Julia.
Thanks in advance for any suggestions!