SCM

SCM Repository

[rmetrics] Annotation of /pkg/fEcofin/R/3F-HeavisideFunction.R
ViewVC logotype

Annotation of /pkg/fEcofin/R/3F-HeavisideFunction.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1311 - (view) (download)

1 : wuertz 1034
2 :     # This library is free software; you can redistribute it and/or
3 :     # modify it under the terms of the GNU Library General Public
4 :     # License as published by the Free Software Foundation; either
5 :     # version 2 of the License, or (at your option) any later version.
6 :     #
7 :     # This library is distributed in the hope that it will be useful,
8 :     # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 :     # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 :     # GNU Library General Public License for more details.
11 :     #
12 :     # You should have received a copy of the GNU Library General
13 :     # Public License along with this library; if not, write to the
14 :     # Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
15 :     # MA 02111-1307 USA
16 :    
17 :     # Copyrights (C)
18 :     # for this R-port:
19 : wuertz 1235 # 1999 - 2007, Diethelm Wuertz, GPL
20 : wuertz 1034 # Diethelm Wuertz <wuertz@itp.phys.ethz.ch>
21 :     # info@rmetrics.org
22 :     # www.rmetrics.org
23 :     # for the code accessed (or partly included) from other R-ports:
24 :     # see R's copyright and license files
25 :     # for the code accessed (or partly included) from contributed R-ports
26 :     # and other sources
27 :     # see Rmetrics's copyright file
28 :    
29 :    
30 :     ################################################################################
31 :     # FUNCTION: HEAVISIDE AND RELATED FUNCTIONS:
32 :     # Heaviside Computes Heaviside unit step function
33 :     # Sign Another signum function
34 :     # Delta Computes delta function
35 :     # Boxcar Computes boxcar function
36 :     # Ramp Computes ramp function
37 : wuertz 1311 # FUNCTION: HEAVISIDE SLIDER:
38 : wuertz 1034 # .heavisideSlider Displays Heaviside and related functions
39 :     ################################################################################
40 :    
41 :    
42 : wuertz 1311 ################################################################################
43 :     # FUNCTION: HEAVISIDE AND RELATED FUNCTIONS:
44 :     # Heaviside Computes Heaviside unit step function
45 :     # Sign Another signum function
46 :     # Delta Computes delta function
47 :     # Boxcar Computes boxcar function
48 :     # Ramp Computes ramp function
49 :    
50 :    
51 : wuertz 1034 Heaviside =
52 :     function(x, a = 0)
53 :     { # A function implemented by Diethelm Wuertz
54 :    
55 :     # Description:
56 :     # Computes the Heaviside or unit step function.
57 :    
58 :     # Arguments:
59 :     # x - a numeric vector.
60 :     # a - the location of the break.
61 :    
62 :     # Details:
63 :     # The Heaviside step function is 1 for x>a, 1/2 for x=a,
64 :     # and 0 for x<a.
65 :    
66 :     # Notes:
67 :     # Heaviside Unit Step Function and Related Functions
68 :     # See: http://mathworld.wolfram.com/HeavisideStepFunction.html
69 :     # Note: sign(x) is part of R's base package
70 : wuertz 1274
71 : wuertz 1034 # FUNCTION:
72 :    
73 :     # Compute H:
74 :     result = (sign(x-a) + 1)/2
75 :    
76 :     # Return Value:
77 :     result
78 :     }
79 :    
80 :    
81 :     # ------------------------------------------------------------------------------
82 :    
83 :    
84 :     Sign =
85 :     function(x, a = 0)
86 :     { # A function implemented by Diethelm Wuertz
87 :    
88 :     # Description:
89 :     # Computes the (modofied) Sign function.
90 :    
91 :     # Arguments:
92 :     # x - a numeric vector.
93 :     # a - the location of the break.
94 :    
95 :     # Details:
96 :     # The Sign function is 1 for x>a, 0 for x=a,
97 :     # and -1 for x<a.
98 : wuertz 1274
99 : wuertz 1034 # FUNCTION:
100 :    
101 :     # Compute Sign:
102 :     result = sign(x-a)
103 :    
104 :     # Return Value:
105 :     result
106 :     }
107 :    
108 :    
109 :     # ------------------------------------------------------------------------------
110 :    
111 :    
112 :     Delta =
113 :     function(x, a = 0)
114 :     { # A function implemented by Diethelm Wuertz
115 :    
116 :     # Description:
117 :     # Computes the delta function.
118 :    
119 :     # Arguments:
120 :     # x - a numeric vector.
121 :     # a - the location of the break.
122 :    
123 :     # Details:
124 :     # The delta function is defined as: delta(x) = d/dx Heaviside(x-a)
125 : wuertz 1274
126 : wuertz 1034 # FUNCTION:
127 :    
128 :     # Compute delta:
129 :     result = 1/sign(abs(x-a))-1
130 :    
131 :     # Return Value:
132 :     result
133 :     }
134 :    
135 :    
136 :     # ------------------------------------------------------------------------------
137 :    
138 :    
139 :     Boxcar =
140 :     function(x, a = 0.5)
141 :     { # A function implemented by Diethelm Wuertz
142 :    
143 :     # Description:
144 :     # Computes the boxcar function.
145 :    
146 :     # Arguments:
147 :     # x - a numeric vector.
148 :     # a - the location of the break.
149 :    
150 :     # Details:
151 :     # The boxcar function is defined as:
152 :     # Pi(x) = Heaviside(x+a) - Heaviside(x-a)
153 : wuertz 1274
154 : wuertz 1034 # FUNCTION:
155 :    
156 :     # Compute boxcar:
157 :     result = Heaviside(x-a) - Heaviside(x+a)
158 :    
159 :     # Return Value:
160 :     result
161 :     }
162 :    
163 :    
164 :     # ------------------------------------------------------------------------------
165 :    
166 :    
167 :     Ramp =
168 :     function(x, a = 0)
169 :     { # A function implemented by Diethelm Wuertz
170 :    
171 :     # Description:
172 :     # Computes the ramp function.
173 :    
174 :     # Arguments:
175 :     # x - a numeric vector.
176 :     # a - the location of the break.
177 :    
178 :     # Details:
179 :     # The ramp function is defined as: R(x)= (x-a)*Heaviside(x-a)
180 : wuertz 1274
181 : wuertz 1034 # FUNCTION:
182 :    
183 :     # Compute ramp:
184 :     result = (x-a) * Heaviside(x-a)
185 :    
186 :     # Return Value:
187 :     result
188 :     }
189 :    
190 :    
191 : wuertz 1311 ################################################################################
192 :     # FUNCTION: HEAVISIDE SLIDER:
193 :     # .heavisideSlider Displays Heaviside and related functions
194 : wuertz 1034
195 :    
196 :     .heavisideSlider =
197 :     function()
198 :     { # A function implemented by Diethelm Wuertz
199 :    
200 :     # Description
201 :     # Displays Heaviside and related functions
202 : wuertz 1274
203 : wuertz 1034 # FUNCTION:
204 :    
205 :     # Internal Function:
206 :     refresh.code = function(...)
207 :     {
208 :     # Sliders:
209 :     a = .sliderMenu(no = 1)
210 :     by = .sliderMenu(no = 2)
211 :    
212 :     # Frame:
213 :     par(mfrow = c(2, 2), cex = 0.7)
214 :    
215 :     # FGN TimeSeries:
216 :     x = seq(-10, 10, by = by)
217 :     H = Heaviside(x, a)
218 :     Sign = Sign(x, a)
219 :     Delta = Delta(x, a)
220 :     Boxcar = Boxcar(x, a)
221 :     Ramp = Ramp(x, a)
222 :     plot(x, H, type = "b", ylim = c(-1, 1),
223 :     col = "steelblue", pch = 19)
224 :     title(main = paste("Heaviside | a =", a))
225 :     grid()
226 :     plot(x, Sign, type = "b", ylim = c(-1, 1),
227 :     col = "steelblue", pch = 19)
228 :     title(main = paste("Sign | a =", a))
229 :     grid()
230 :     plot(x, Boxcar, type = "b", ylim = c(-1, 1),
231 :     col = "steelblue", pch = 19)
232 :     title(main = paste("Boxcar | a =", a))
233 :     grid()
234 :     plot(x, Ramp, type = "b",
235 :     col = "steelblue", pch = 19)
236 :     title(main = paste("Ramp | a =", a))
237 :     grid()
238 :    
239 :     # Reset Frame:
240 :     par(mfrow = c(1, 1), cex = 0.7)
241 :     }
242 :    
243 :     # Open Slider Menu:
244 :     .sliderMenu(refresh.code,
245 :     names = c( "a", "by"),
246 :     minima = c( -10, 0.1),
247 :     maxima = c( 10, 1.0),
248 :     resolutions = c( 0.1, 0.1),
249 :     starts = c( 0, 0.2))
250 :     }
251 :    
252 :    
253 :     ################################################################################
254 :    

R-Forge@R-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business University of Wisconsin - Madison Powered By FusionForge