# Dmd for 1D heat equation

**URL:** <https://discourse.julialang.org/t/dmd-for-1d-heat-equation/83159>\
**Category:** New to Julia\
**Created:** [June 22, 2022, 2:33am UTC](https://discourse.julialang.org/t/dmd-for-1d-heat-equation/83159 "2022-06-22T02:33:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![totti94](https://avatars.discourse-cdn.com/v4/letter/t/d26b3c/32.png) [@totti94](https://discourse.julialang.org/u/totti94)\
**Post date:** [June 22, 2022, 2:33am UTC](https://discourse.julialang.org/t/dmd-for-1d-heat-equation/83159/1 "2022-06-22T02:33:04Z")

</div>

Hello everybody.  
I am working on a heat equation of 1D heat equation and I was applying the DMD method on the numeric solution using DMDSVD() which gave me the following error DimensionMismatch("expected axes .  
I have pasted my code with the topic so does anybody knoq how can I solve this error.

```julia
# rod Length 
L = 0.5

Tf = 10000.0  

# Thermal conductivity
λ = 45.0 

# Specific heat capacity
c = 480.0

# Mass density
ρ = 7800.0

# Thermal diffusivity
α = λ/(c*ρ)

using SparseArrays
using Plots
using DifferentialEquations
using LinearAlgebra

# Number of grid elements
N = 101

# Finite discretization
Δx = L/(N-1)   

# 1-dimensional grid
xspan = 0 : Δx : L

# Diffusion matrix
M = spdiagm(-1 => ones(N-1), 0 => -2*ones(N), 1 => ones(N-1));

#creat matrix M 5x5
Matrix(M)[1:5,1:5]

# First row
M[1,1] = M[1,2] = 0;

# Last row
M[end,end] = M[end,end-1] = 0;

# Heat Equation as ODE
function heat_eq(dθ, θ, p, t)
    return dθ .= (1/Δx^2) * α * M * θ
end

#Initial conditions: θ(0) = m * (L * x - x^2)
function initial_data(x, params)
    # α = params[1] # unused here 
    L = params[2]
    m = params[3]

    return m*( L * x - x^2)
end

# Initial heat distribution
θ₀ = zeros(N);

m = 50
amp = 50

# Parameters
param = [α, L, amp]

let

	for i = 1 : length(xspan)
		θ₀[i] = initial_data(xspan[i], param)
	end
	
	plot(xspan, θ₀, xlabel="Position X", ylabel="Temperature", legend=false)
end

# Upper limit of sampling time
ul = 0.5 * Δx^2 / α

# Sampling time
Δt = 0.8

# time discretization
tspan = (0.0, 1000.0)

# Solving the ODE
let
	prob = ODEProblem( heat_eq, θ₀, tspan ) # ODE Problem
	
	sol = solve(prob,Euler(),dt=Δt,progress=true, save_everystep=false, 
				save_start=true) # Solving the ODE
	
	plot(xspan, sol.u[2], xlabel = "Position X", ylabel="Temperature", legend=false)
end

using DataDrivenDiffEq

Y = sol[:,:]
#plot(Y, legend = nothing)

DY = sol(sol.t, Val{1})[:,:]
approx_heat = ContinuousDataDrivenProblem(Y, DY)
#plot(approx_heat)
#mutable struct DMDSVD{T} <: DataDrivenDiffEq.AbstractKoopmanAlgorithm
#global res = solve(approx_heat, DMDSVD(0.0),digits =1)    
#end

res = solve(approx_heat, DMDSVD(),digits =1)

```

---

<div class="post-metadata">

**Author:** ![Julius\_Martensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julius_martensen/32/17077_2.png) [@Julius\_Martensen](https://discourse.julialang.org/u/Julius_Martensen)\
**Post date:** [June 22, 2022, 5:45am UTC](https://discourse.julialang.org/t/dmd-for-1d-heat-equation/83159/2 "2022-06-22T05:45:16Z")

</div>

Hey and welcome!

As far as I can tell, you define the ContinuousDataDrivenProblem with the wrong arguments. Since you have also the time derivative data, you should use

```julia
prob = ContinuousDataDrivenProblem(X,t,DX=DY)

```

Alternatively, you can just call

```julia
prob = DataDrivenProblem(solution)

```

Where solution is the solution of the DifferentialEquation.

---

<div class="post-metadata">

**Author:** ![totti94](https://avatars.discourse-cdn.com/v4/letter/t/d26b3c/32.png) [@totti94](https://discourse.julialang.org/u/totti94)\
**Post date:** [June 22, 2022, 11:44am UTC](https://discourse.julialang.org/t/dmd-for-1d-heat-equation/83159/4 "2022-06-22T11:44:21Z")

</div>

when I use

```julia
approx_heat = ContinuousDataDrivenProblem(y ,t , DY)

```

ERROR: AssertionError: One or more measurements are not sized equally.

when I use

```julia
approx_heat = ContinuousDataDrivenProblem(y ,t , Dx=DY)

```

ERROR: syntax: expression too large

The error is always on this line

```julia
res = solve(approx_heat, DMDSVD(),digits =1)

```

---

<div class="post-metadata">

**Author:** ![Julius\_Martensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julius_martensen/32/17077_2.png) [@Julius\_Martensen](https://discourse.julialang.org/u/Julius_Martensen)\
**Post date:** [June 23, 2022, 7:05am UTC](https://discourse.julialang.org/t/dmd-for-1d-heat-equation/83159/5 "2022-06-23T07:05:17Z")

</div>

@totti94 Could you open an issue on this? There seems to be a problem with the Jacobian of the basis.
