Skip to content
Snippets Groups Projects
Commit 33ccaad3 authored by Dainius's avatar Dainius
Browse files

Add nonlinear equation solver example

parent e6ddb736
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id:e71b93f4 tags:
``` R
library(nleqslv)
```
%% Cell type:code id:df6017b9 tags:
``` R
eqs = function(x) {
a = 2* x[1] + x[2] + x[3] + x[4] - 2
b = 3* x[1] + 2*x[2] +0.5*x[3] + 0.3*x[4] - 4
d = x[1] + 2*x[2] + x[3]*x[4] - 5
e = 0.5*x[1] + 3*x[2] + x[3]+ 0.8*x[4] - 1.5
return(c(a,b,d,e))
}
```
%% Cell type:code id:5dfab7d9 tags:
``` R
solution = nleqslv(c(0,0,0,0), eqs)
```
%% Cell type:code id:aa49261b tags:
``` R
print(solution)
```
%% Output
$x
[1] 1.196018 1.040414 -1.343332 -0.962883
$fvec
[1] -0.8737645 0.7083522 -0.4296814 0.1056137
$termcd
[1] 3
$message
[1] "No better point found (algorithm has stalled)"
$scalex
[1] 1 1 1 1
$nfcnt
[1] 80
$njcnt
[1] 6
$iter
[1] 35
%% Cell type:code id:8428588b tags:
``` R
solution = nleqslv(c(0,0,0,0), eqs, method="Newton", global="gline")
```
%% Cell type:code id:66631006 tags:
``` R
print(solution)
```
%% Output
$x
[1] 1.0766247 0.5363726 -0.4786634 -0.2109586
$fvec
[1] 1.388134e-08 -4.953707e-09 -2.749652e+00 4.138353e-09
$termcd
[1] 3
$message
[1] "No better point found (algorithm has stalled)"
$scalex
[1] 1 1 1 1
$nfcnt
[1] 140
$njcnt
[1] 12
$iter
[1] 12
%% Cell type:code id:aefea231 tags:
``` R
solution = nleqslv(c(0,0,0,0), eqs, global="none", control=list(maxit=15000))
```
%% Cell type:code id:52d7bb84 tags:
``` R
print(solution)
```
%% Output
$x
[1] 1.0657401 0.4601809 0.2995803 -0.8912416
$fvec
[1] -9.170442e-14 -8.215650e-14 -3.280896e+00 8.482104e-14
$termcd
[1] 4
$message
[1] "Iteration limit exceeded"
$scalex
[1] 1 1 1 1
$nfcnt
[1] 15000
$njcnt
[1] 1
$iter
[1] 15000
%% Cell type:code id:7d39bf1b tags:
``` R
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment