forked from fasrc/User_Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathode_test.jl
More file actions
39 lines (34 loc) · 915 Bytes
/
ode_test.jl
File metadata and controls
39 lines (34 loc) · 915 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Program: ode_test.jl
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
using DifferentialEquations
using SimpleDiffEq
using Printf
using Plots
using Plots.PlotMeasures
# --- Define the problem ---
f(u, p, t) = t - u
u0 = 1.0 # Initial conditions
tspan = (0.0, 5.0) # Time span
prob = ODEProblem(f, u0, tspan)
# --- Solve the problem ---
dt = 0.2
alg = RK4()
sol = solve(prob, alg, saveat=dt)
# --- Exact solution ---
function f_ex(t)
f = t - 1.0 + ( 2.0 * exp(-t) )
return f
end
# --- Print out solution ---
fname = "results.dat"
fo = open(fname, "w")
println(fo, " Time Numeric Exact ")
println(fo, " ------------------------------ ")
for i = 1: size(sol.t)[1]
x = sol.t[i]
y = sol.u[i]
y_exact = f_ex(x)
@printf(fo, "%8.4f %10.6f %10.6f\n", x, y, y_exact)
end
close(fo)