Skip to content

Commit 2fc9251

Browse files
author
J. Gergaud
committed
foo
2 parents 92287c3 + e31c211 commit 2fc9251

File tree

6 files changed

+302
-200
lines changed

6 files changed

+302
-200
lines changed

.github/workflows/UpdateReadme.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
has_template: ${{ steps.check.outputs.has_template }}
1313
steps:
1414
- name: Checkout repo
15-
uses: actions/checkout@v4
15+
uses: actions/checkout@v5
1616

1717
- name: Check for README.template.md
1818
id: check

README.template.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
# CTDiffFlow.jl
22

3-
This repository is a template to create an application or a package for the [control-toolbox ecosystem](https://github.com/control-toolbox).
4-
5-
The instructions to set up a new application / package are given in discussion https://github.com/control-toolbox/CTDiffFlow.jl/discussions/9.
6-
7-
[![**Click here to follow instructions to use this template!** ⬅️](https://img.shields.io/badge/Click_here_to_follow_instructions_to_use_this_template!-darkgreen)](https://github.com/orgs/control-toolbox/discussions/65)
8-
9-
----
10-
113
<!--
124
For instructions on how to customize this README.template.md and use the centralized workflow,
135
please see the user guide: https://github.com/orgs/control-toolbox/discussions/67

article/figures/graphs-END.jl

Lines changed: 103 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,72 +3,121 @@ using LinearAlgebra
33
using Plots
44
using LaTeXStrings
55
# Definition of the second member
6-
function bruss(x,par,t)
7-
λ = par[1]
8-
x₁ = x[1]
9-
x₂ = x[2]
10-
return [1+x₁^2*x₂-+1)*x₁ , λ*x₁-x₁^2*x₂]
6+
function bruss(x, par, t)
7+
λ = par[1]
8+
x₁ = x[1]
9+
x₂ = x[2]
10+
return [1+x₁^2*x₂-+1)*x₁, λ*x₁-x₁^2*x₂]
1111
end
1212

1313
tf = 20
14-
tspan = (0.,tf)
14+
tspan = (0.0, tf)
1515
x₁0 = 1.3
16-
par = [3.]
16+
par = [3.0]
1717
tol = 1.e-4
1818

1919
function graph_END()
20-
function END(fun,x₁0,tf,tol,λ,δλ;algo=Tsit5())
21-
reltol = tol; abstol = tol;
22-
tspan = (0.,tf)
23-
x0 = [x₁0,λ+δλ]
24-
ivp = ODEProblem(fun, x0, tspan, (λ+δλ))
25-
sol2 = solve(ivp, alg=algo, reltol = tol, abstol = tol)
26-
x0 = [x₁0,λ]
27-
ivp = ODEProblem(fun, x0, tspan, (λ))
28-
sol1 = solve(ivp, alg=algo, reltol = tol, abstol = tol)
29-
return (sol2.u[end]-sol1.u[end])/δλ
30-
end
20+
function END(fun, x₁0, tf, tol, λ, δλ; algo=Tsit5())
21+
reltol = tol;
22+
abstol = tol;
23+
tspan = (0.0, tf)
24+
x0 = [x₁0, λ+δλ]
25+
ivp = ODEProblem(fun, x0, tspan, (λ+δλ))
26+
sol2 = solve(ivp; alg=algo, reltol=tol, abstol=tol)
27+
x0 = [x₁0, λ]
28+
ivp = ODEProblem(fun, x0, tspan, (λ))
29+
sol1 = solve(ivp; alg=algo, reltol=tol, abstol=tol)
30+
return (sol2.u[end]-sol1.u[end])/δλ
31+
end
3132

32-
Λ = range(2.88,stop=3.08,length=1001)
33-
n = 2; N = length(Λ)
34-
fdiff = zeros(N,n)
35-
δλ = 4*tol
36-
for i in 1:N
37-
fdiff[i,:] = END(bruss,x₁0,tf,tol,Λ[i],δλ)
38-
end
39-
p1 = plot(Λ,fdiff[:,1],xlabel="λ", ylabel=L"\frac{\partial x_1}{\partial \lambda}(t_f,\lambda)", lw = 3)
40-
p2 = plot(Λ,fdiff[:,2],xlabel="λ", ylabel=L"\frac{\partial x_2}{\partial \lambda}(t_f,\lambda)", lw = 3)
33+
Λ = range(2.88; stop=3.08, length=1001)
34+
n = 2;
35+
N = length(Λ)
36+
fdiff = zeros(N, n)
37+
δλ = 4*tol
38+
for i in 1:N
39+
fdiff[i, :] = END(bruss, x₁0, tf, tol, Λ[i], δλ)
40+
end
41+
p1 = plot(
42+
Λ,
43+
fdiff[:, 1];
44+
xlabel="λ",
45+
ylabel=L"\frac{\partial x_1}{\partial \lambda}(t_f,\lambda)",
46+
lw=3,
47+
)
48+
p2 = plot(
49+
Λ,
50+
fdiff[:, 2];
51+
xlabel="λ",
52+
ylabel=L"\frac{\partial x_2}{\partial \lambda}(t_f,\lambda)",
53+
lw=3,
54+
)
4155

42-
δλ = sqrt(tol)
43-
for i in 1:N
44-
fdiff[i,:] =END(bruss,x₁0,tf,tol,Λ[i],δλ)
45-
end
46-
p3 = plot(Λ,fdiff[:,1],xlabel="λ", ylabel=L"\frac{\partial x_1}{\partial \lambda}(t_f,\lambda)", lw = 3)
47-
p4 = plot(Λ,fdiff[:,2],xlabel="λ", ylabel=L"\frac{\partial x_2}{\partial \lambda}(t_f,\lambda)", lw = 3)
48-
plot(p1,p2,p3,p4,layout=(2,2),legend=false)
56+
δλ = sqrt(tol)
57+
for i in 1:N
58+
fdiff[i, :] = END(bruss, x₁0, tf, tol, Λ[i], δλ)
59+
end
60+
p3 = plot(
61+
Λ,
62+
fdiff[:, 1];
63+
xlabel="λ",
64+
ylabel=L"\frac{\partial x_1}{\partial \lambda}(t_f,\lambda)",
65+
lw=3,
66+
)
67+
p4 = plot(
68+
Λ,
69+
fdiff[:, 2];
70+
xlabel="λ",
71+
ylabel=L"\frac{\partial x_2}{\partial \lambda}(t_f,\lambda)",
72+
lw=3,
73+
)
74+
plot(p1, p2, p3, p4; layout=(2, 2), legend=false)
4975

50-
#| label: fig-finite-diff-DP5
51-
#| fig-cap: "Derivative computing by finite differences. $t_f=20, \\lambda$ ranging from 2.88 to 3.08, $Tol=RelTol=AbsTol=10^{-4}$. Top graphs is for $\\delta\\lambda=4Tol$ and bottom graphs for $\\delta\\lambda=\\sqrt{Tol}$. The numerical integrattion is done with DP5()."
52-
#algo = DP5()
53-
algo = Tsit5()
54-
δλ = 4*tol
55-
for i in 1:N
56-
fdiff[i,:] = END(bruss,x₁0,tf,tol,Λ[i],δλ;algo=algo)
57-
end
58-
p5 = plot(Λ,fdiff[:,1],xlabel="λ", ylabel=L"\frac{\partial x_1}{\partial \lambda}(t_f,\lambda)", lw = 3)
59-
p6 = plot(Λ,fdiff[:,2],xlabel="λ", ylabel=L"\frac{\partial x_2}{\partial \lambda}(t_f,\lambda)", lw = 3)
76+
#| label: fig-finite-diff-DP5
77+
#| fig-cap: "Derivative computing by finite differences. $t_f=20, \\lambda$ ranging from 2.88 to 3.08, $Tol=RelTol=AbsTol=10^{-4}$. Top graphs is for $\\delta\\lambda=4Tol$ and bottom graphs for $\\delta\\lambda=\\sqrt{Tol}$. The numerical integrattion is done with DP5()."
78+
#algo = DP5()
79+
algo = Tsit5()
80+
δλ = 4*tol
81+
for i in 1:N
82+
fdiff[i, :] = END(bruss, x₁0, tf, tol, Λ[i], δλ; algo=algo)
83+
end
84+
p5 = plot(
85+
Λ,
86+
fdiff[:, 1];
87+
xlabel="λ",
88+
ylabel=L"\frac{\partial x_1}{\partial \lambda}(t_f,\lambda)",
89+
lw=3,
90+
)
91+
p6 = plot(
92+
Λ,
93+
fdiff[:, 2];
94+
xlabel="λ",
95+
ylabel=L"\frac{\partial x_2}{\partial \lambda}(t_f,\lambda)",
96+
lw=3,
97+
)
6098

61-
δλ = sqrt(tol)
62-
algo = DP5()
63-
for i in 1:N
64-
fdiff[i,:] = END(bruss,x₁0,tf,tol,Λ[i],δλ;algo=algo)
65-
end
66-
p7 = plot(Λ,fdiff[:,1],xlabel="λ", ylabel=L"\frac{\partial x_1}{\partial \lambda}(t_f,\lambda)", lw = 3)
67-
p8 = plot(Λ,fdiff[:,2],xlabel="λ", ylabel=L"\frac{\partial x_2}{\partial \lambda}(t_f,\lambda)", lw = 3)
68-
plt_END = plot(p5,p6,p7,p8,layout=(2,2),legend=false)
99+
δλ = sqrt(tol)
100+
algo = DP5()
101+
for i in 1:N
102+
fdiff[i, :] = END(bruss, x₁0, tf, tol, Λ[i], δλ; algo=algo)
103+
end
104+
p7 = plot(
105+
Λ,
106+
fdiff[:, 1];
107+
xlabel="λ",
108+
ylabel=L"\frac{\partial x_1}{\partial \lambda}(t_f,\lambda)",
109+
lw=3,
110+
)
111+
p8 = plot(
112+
Λ,
113+
fdiff[:, 2];
114+
xlabel="λ",
115+
ylabel=L"\frac{\partial x_2}{\partial \lambda}(t_f,\lambda)",
116+
lw=3,
117+
)
118+
plt_END = plot(p5, p6, p7, p8; layout=(2, 2), legend=false)
69119

70-
savefig(plt_END, "article/figures/plot_END.png")
120+
savefig(plt_END, "article/figures/plot_END.png")
71121
end
72122

73123
graph_END()
74-

article/figures/graphs-eq-var.jl

Lines changed: 97 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -14,88 +14,115 @@ include("../../src/DiffFlow.jl")
1414
using .DiffFlow
1515
#
1616
# Definition of the second member
17-
function bruss(x,par,t)
18-
λ = par[1]
19-
x₁ = x[1]
20-
x₂ = x[2]
21-
return [1+x₁^2*x₂-+1)*x₁ , λ*x₁-x₁^2*x₂]
17+
function bruss(x, par, t)
18+
λ = par[1]
19+
x₁ = x[1]
20+
x₂ = x[2]
21+
return [1+x₁^2*x₂-+1)*x₁, λ*x₁-x₁^2*x₂]
2222
end
2323

24-
t0 = 0.; tf = 20.
25-
tspan = (t0,tf)
26-
λ = [3.]
24+
t0 = 0.0;
25+
tf = 20.0
26+
tspan = (t0, tf)
27+
λ = [3.0]
2728
x01 = 1.3
2829
funx0(λ) = [x01, λ[1]]
2930
tol = 1.e-4
3031

31-
plt1 = plot(); plt2 = plot()
32-
33-
reltol = 1.e-4; abstol = 1.e-4
34-
35-
function graph_eq_var(plt1,plt2)
36-
37-
∂λ_flow_var = DiffFlow.build_∂λ_flow_var(bruss,t0,funx0,tf,λ)
38-
39-
println(∂λ_flow_var(t0,funx0,tf,λ;reltol=reltol,abstol=abstol))
40-
# ne fonctionne pas
41-
# @btime ∂λ_flow_var(t0,funx0,tf,λ;reltol=reltol,abstol=abstol)
42-
#println(∂x0_flow_var(t0,funx0,tf,λ;reltol=reltol, abstol=abstol))
43-
44-
45-
Λ = range(2.88,stop=3.08,length=1001)
46-
n = 2; N = length(Λ)
47-
fdiff = zeros(N,n)
48-
49-
for i in 1:N
50-
fdiff[i,:] = ∂λ_flow_var(t0,funx0 ,tf,[Λ[i]];reltol=reltol,abstol=abstol)
51-
end
52-
plot!(plt1,Λ,fdiff[:,1],xlabel="λ", ylabel=L"\frac{\partial x_1}{\partial \lambda}(t_f,\lambda)", lw = 3)
53-
plot!(plt2,Λ,fdiff[:,2],xlabel="λ", ylabel=L"\frac{\partial x_2}{\partial \lambda}(t_f,\lambda)", lw = 3)
54-
55-
56-
#plt = plot!(plt,p1,p2,layout=(2,2),legend=false)
57-
58-
#| label: fig-finite-diff-DP5
59-
#| fig-cap: "Derivative computing by finite differences. $t_f=20, \\lambda$ ranging from 2.88 to 3.08, $Tol=RelTol=AbsTol=10^{-4}$. Top graphs is for $\\delta\\lambda=4Tol$ and bottom graphs for $\\delta\\lambda=\\sqrt{Tol}$. The numerical integrattion is done with DP5()."
60-
#algo = DP5()
61-
32+
plt1 = plot();
33+
plt2 = plot()
34+
35+
reltol = 1.e-4;
36+
abstol = 1.e-4
37+
38+
function graph_eq_var(plt1, plt2)
39+
∂λ_flow_var = DiffFlow.build_∂λ_flow_var(bruss, t0, funx0, tf, λ)
40+
41+
println(∂λ_flow_var(t0, funx0, tf, λ; reltol=reltol, abstol=abstol))
42+
# ne fonctionne pas
43+
# @btime ∂λ_flow_var(t0,funx0,tf,λ;reltol=reltol,abstol=abstol)
44+
#println(∂x0_flow_var(t0,funx0,tf,λ;reltol=reltol, abstol=abstol))
45+
46+
Λ = range(2.88; stop=3.08, length=1001)
47+
n = 2;
48+
N = length(Λ)
49+
fdiff = zeros(N, n)
50+
51+
for i in 1:N
52+
fdiff[i, :] = ∂λ_flow_var(t0, funx0, tf, [Λ[i]]; reltol=reltol, abstol=abstol)
53+
end
54+
plot!(
55+
plt1,
56+
Λ,
57+
fdiff[:, 1];
58+
xlabel="λ",
59+
ylabel=L"\frac{\partial x_1}{\partial \lambda}(t_f,\lambda)",
60+
lw=3,
61+
)
62+
plot!(
63+
plt2,
64+
Λ,
65+
fdiff[:, 2];
66+
xlabel="λ",
67+
ylabel=L"\frac{\partial x_2}{\partial \lambda}(t_f,\lambda)",
68+
lw=3,
69+
)
70+
71+
#plt = plot!(plt,p1,p2,layout=(2,2),legend=false)
72+
73+
#| label: fig-finite-diff-DP5
74+
#| fig-cap: "Derivative computing by finite differences. $t_f=20, \\lambda$ ranging from 2.88 to 3.08, $Tol=RelTol=AbsTol=10^{-4}$. Top graphs is for $\\delta\\lambda=4Tol$ and bottom graphs for $\\delta\\lambda=\\sqrt{Tol}$. The numerical integrattion is done with DP5()."
75+
#algo = DP5()
6276

6377
end
6478

65-
graph_eq_var(plt1,plt2)
66-
plt = plot(plt1,plt2)
79+
graph_eq_var(plt1, plt2)
80+
plt = plot(plt1, plt2)
6781
savefig(plt, "article/figures/plot_var1.png")
6882

69-
function graph_diff_auto_flow(plt1,plt2)
70-
71-
∂λ_flow = DiffFlow.build_∂λ_flow(bruss,t0,funx0,tf,λ)
72-
δλ = [1.]
73-
println(∂λ_flow(t0,funx0,tf,λ;reltol=reltol,abstol=abstol))
74-
75-
76-
77-
Λ = range(2.88,stop=3.08,length=1001)
78-
n = 2; N = length(Λ)
79-
fdiff = zeros(N,n)
80-
81-
for i in 1:N
82-
fdiff[i,:] = ∂λ_flow(t0,funx0,tf,[Λ[i]];reltol=reltol,abstol=abstol)
83-
end
84-
plot!(plt1,Λ,fdiff[:,1],color=:magenta,xlabel="λ", ylabel=L"\frac{\partial x_1}{\partial \lambda}(t_f,\lambda)", lw = 3)
85-
plot!(plt2,Λ,fdiff[:,2],color=:magenta,xlabel="λ", ylabel=L"\frac{\partial x_2}{\partial \lambda}(t_f,\lambda)", lw = 3)
86-
87-
88-
#plt_diff_flow1 = plot(p1,p2,layout=(2,2),color=:magenta,legend=false)
89-
#plt = plot!(plt,p1,p2,layout=(2,2),color=:magenta,legend=false)
90-
91-
#| label: fig-finite-diff-DP5
92-
#| fig-cap: "Derivative computing by finite differences. $t_f=20, \\lambda$ ranging from 2.88 to 3.08, $Tol=RelTol=AbsTol=10^{-4}$. Top graphs is for $\\delta\\lambda=4Tol$ and bottom graphs for $\\delta\\lambda=\\sqrt{Tol}$. The numerical integrattion is done with DP5()."
93-
algo = DP5()
94-
95-
#savefig(plt_diff_flow1, "plot_diff_flow1.png")
83+
function graph_diff_auto_flow(plt1, plt2)
84+
∂λ_flow = DiffFlow.build_∂λ_flow(bruss, t0, funx0, tf, λ)
85+
δλ = [1.0]
86+
println(∂λ_flow(t0, funx0, tf, λ; reltol=reltol, abstol=abstol))
87+
88+
Λ = range(2.88; stop=3.08, length=1001)
89+
n = 2;
90+
N = length(Λ)
91+
fdiff = zeros(N, n)
92+
93+
for i in 1:N
94+
fdiff[i, :] = ∂λ_flow(t0, funx0, tf, [Λ[i]]; reltol=reltol, abstol=abstol)
95+
end
96+
plot!(
97+
plt1,
98+
Λ,
99+
fdiff[:, 1];
100+
color=:magenta,
101+
xlabel="λ",
102+
ylabel=L"\frac{\partial x_1}{\partial \lambda}(t_f,\lambda)",
103+
lw=3,
104+
)
105+
plot!(
106+
plt2,
107+
Λ,
108+
fdiff[:, 2];
109+
color=:magenta,
110+
xlabel="λ",
111+
ylabel=L"\frac{\partial x_2}{\partial \lambda}(t_f,\lambda)",
112+
lw=3,
113+
)
114+
115+
#plt_diff_flow1 = plot(p1,p2,layout=(2,2),color=:magenta,legend=false)
116+
#plt = plot!(plt,p1,p2,layout=(2,2),color=:magenta,legend=false)
117+
118+
#| label: fig-finite-diff-DP5
119+
#| fig-cap: "Derivative computing by finite differences. $t_f=20, \\lambda$ ranging from 2.88 to 3.08, $Tol=RelTol=AbsTol=10^{-4}$. Top graphs is for $\\delta\\lambda=4Tol$ and bottom graphs for $\\delta\\lambda=\\sqrt{Tol}$. The numerical integrattion is done with DP5()."
120+
algo = DP5()
121+
122+
#savefig(plt_diff_flow1, "plot_diff_flow1.png")
96123
end
97124

98-
graph_diff_auto_flow(plt1,plt2)
125+
graph_diff_auto_flow(plt1, plt2)
99126

100-
plt = plot(plt1,plt2)
127+
plt = plot(plt1, plt2)
101128
savefig(plt, "article/figures/plot_diff_flow1.png")

article/figures/time-steps.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ sol_∂λ_flow = [λ[2]*exp(λ[1]*tf) exp(λ[1]*tf)
6565
0. tf*exp(λ[2]*tf)
6666
tf*exp((λ[1]-λ[2])*tf) tf*exp((λ[1]-λ[2])*tf)]
6767
algo = Tsit5()
68-
reltol = 1.e-4; abstol = 1.e-4
68+
reltol = 1.e-4;
69+
abstol = 1.e-4
6970

7071
# Flow
7172
#x0λ = funx0(λ)

0 commit comments

Comments
 (0)