Skip to content

Commit fe27fcb

Browse files
add plots to least squares
1 parent 69b3679 commit fe27fcb

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

docs/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ DocumenterCitations = "daee34ce-89f3-4625-b898-19384cb65244"
55
LLSModels = "39f5bc3e-5160-4bf8-ac48-504fd2534d24"
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
77
NLPModelsModifiers = "e01155f1-5c6f-4375-a9d8-616dd036575f"
8+
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
89
ProximalOperators = "a725b495-10eb-56fe-b38b-717eba820537"
910
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1011
RegularizedProblems = "ea076b23-609f-44d2-bb12-a4ae45328278"

docs/src/examples/ls.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ end
5050
f_model = ADNLSModel(F, x0, m, name = "nonlinear LS model of f")
5151
5252
# Get the regularizer from ProximalOperators
53-
λ = 1.0
53+
λ = 0.01
5454
h = NormL0(λ)
5555
5656
# Wrap into a RegularizedNLPModel
@@ -69,9 +69,41 @@ out = LM(regularized_pb, verbose = 1, atol = 1e-4)
6969
println("LM converged after $(out.iter) iterations.")
7070
```
7171

72+
We can visualize the solution with plots,
73+
```@example ls
74+
using Plots
75+
76+
# Extract estimated parameters
77+
a_est, b_est = out.solution
78+
79+
# True curve
80+
y_true = [a_true * exp(b_true * ti) for ti in t]
81+
82+
# Estimated curve
83+
y_est = [a_est * exp(b_est * ti) for ti in t]
84+
85+
# Plot
86+
scatter(t, y, label="Noisy data", legend=:bottomleft)
87+
plot!(t, y_true, label="True model", lw=2)
88+
plot!(t, y_est, label="Fitted model", lw=2, ls=:dash)
89+
```
90+
7291
```@example ls
7392
#We can choose LMTR instead which is a trust-region method
7493
out = LMTR(regularized_pb, verbose = 1, atol = 1e-4)
7594
println("LMTR converged after $(out.iter) iterations.")
95+
```
96+
97+
```@example ls
98+
# Extract estimated parameters
99+
a_est, b_est = out.solution
100+
101+
# Estimated curve
102+
y_est = [a_est * exp(b_est * ti) for ti in t]
103+
104+
# Plot
105+
scatter(t, y, label="Noisy data", legend=:bottomleft)
106+
plot!(t, y_true, label="True model", lw=2)
107+
plot!(t, y_est, label="Fitted model", lw=2, ls=:dash)
76108
77109
```

0 commit comments

Comments
 (0)