-
Notifications
You must be signed in to change notification settings - Fork 149
/
f_cos.go
80 lines (68 loc) · 1.57 KB
/
f_cos.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2016 The Gosl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dbf
import (
"math"
"github.com/cpmech/gosl/chk"
)
// Cos implements y(t) = a * cos(b*t) + c
// Input:
// b/pi -- is a flag that says that b is in fact b divided by π
// thus, the code will multiply b by π internally
type Cos struct {
// parameters
A float64
B float64
C float64
// derived
bIsBdivPi bool
}
// set allocators database
func init() {
allocators["cos"] = func() T { return new(Cos) }
}
// Init initialises the function
func (o *Cos) Init(prms Params) {
e := prms.Connect(&o.A, "a", "cos function")
e += prms.Connect(&o.C, "c", "cos function")
p := prms.Find("b/pi")
if p == nil {
e += prms.Connect(&o.B, "b", "cos function")
} else {
e += prms.Connect(&o.B, "b/pi", "cos function")
o.bIsBdivPi = true
}
if e != "" {
chk.Panic("%v\n", e)
}
}
// F returns y = F(t, x)
func (o Cos) F(t float64, x []float64) float64 {
b := o.B
if o.bIsBdivPi {
b = o.B * math.Pi
}
return o.A*math.Cos(b*t) + o.C
}
// G returns ∂y/∂t_cteX = G(t, x)
func (o Cos) G(t float64, x []float64) float64 {
b := o.B
if o.bIsBdivPi {
b = o.B * math.Pi
}
return -o.A * b * math.Sin(b*t)
}
// H returns ∂²y/∂t²_cteX = H(t, x)
func (o Cos) H(t float64, x []float64) float64 {
b := o.B
if o.bIsBdivPi {
b = o.B * math.Pi
}
return -o.A * b * b * math.Cos(b*t)
}
// Grad returns ∇F = ∂y/∂x = Grad(t, x)
func (o Cos) Grad(v []float64, t float64, x []float64) {
setvzero(v)
return
}