Changeset 125 for trunk/SRC/Interpolation/spl_incr.pro
- Timestamp:
- 07/06/06 16:10:25 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SRC/Interpolation/spl_incr.pro
r121 r125 12 12 ; in a way that interpolated values are also monotonically increasing. 13 13 ; 14 ; @param x1 {in}{required} 15 ; An n-element (at least 2) input vector that specifies the tabulate points in 14 ; @param x1 {in}{required} 15 ; An n-element (at least 2) input vector that specifies the tabulate points in 16 16 ; a strict ascending order. 17 17 ; 18 ; @param y1 {in}{required} 18 ; @param y1 {in}{required} 19 19 ; f(x) = y. An n-element input vector that specifies the values 20 20 ; of the tabulated function F(Xi) corresponding to Xi. As f is … … 22 22 ; monotonically increasing. y can have equal consecutive values. 23 23 ; 24 ; @param x2 {in}{required} 24 ; @param x2 {in}{required} 25 25 ; The input values for which the interpolated values are 26 ; desired. Its values must be strictly monotonically increasing. 26 ; desired. Its values must be strictly monotonically increasing. 27 27 ; 28 28 ; @param der2 29 ; @param x 30 ; 31 ; @returns 29 ; @param x 30 ; 31 ; @returns 32 32 ; 33 33 ; y2: f(x2) = y2. Double precision array … … 37 37 ; values (amplitude smaller than 1.e-6)... 38 38 ; 39 ; @examples 39 ; @examples 40 40 ; 41 41 ; IDL> n = 100L 42 ; IDL> x = (dindgen(n))^2 42 ; IDL> x = (dindgen(n))^2 43 43 ; IDL> y = abs(randomn(0, n)) 44 44 ; IDL> y[n/2:n/2+1] = 0. … … 53 53 ; IDL> oplot, x2, y2, color = 100 54 54 ; IDL> c = y2[1:n2-1] - y2[0:n2-2] 55 ; IDL> print, min(c) LT 0 55 ; IDL> print, min(c) LT 0 56 56 ; IDL> print, min(c, max = ma), ma 57 57 ; IDL> splot,c,xstyle=1,ystyle=1, yrange=[-.01,.05], ysurx=.25, petit = [1, 2, 2], /noerase … … 87 87 88 88 ;+ 89 ; @param x1 {in}{required} 90 ; An n-element (at least 2) input vector that specifies the tabulate points in 89 ; @param x1 {in}{required} 90 ; An n-element (at least 2) input vector that specifies the tabulate points in 91 91 ; a strict ascending order. 92 92 ; 93 ; @param y1 {in}{required} 93 ; @param y1 {in}{required} 94 94 ; f(x) = y. An n-element input vector that specifies the values 95 95 ; of the tabulated function F(Xi) corresponding to Xi. As f is … … 97 97 ; monotonically increasing. y can have equal consecutive values. 98 98 ; 99 ; @param x2 {in}{required} 99 ; @param x2 {in}{required} 100 100 ; The input values for which the interpolated values are 101 ; desired. Its values must be strictly monotonically increasing. 101 ; desired. Its values must be strictly monotonically increasing. 102 102 ; 103 103 ; @param der2 104 ; @param x 104 ; @param x 105 105 ; 106 106 ;- … … 134 134 ; @keyword YPN_1 The first derivative of the interpolating function at the 135 135 ; point Xn-1. If YPN_1 is omitted, the second derivative at the 136 ; boundary is set to zero, resulting in a "natural spline." 136 ; boundary is set to zero, resulting in a "natural spline." 137 137 ;- 138 138 FUNCTION spl_incr, x, y, x2, YP0 = yp0, YPN_1 = ypn_1 … … 148 148 nx2 = n_elements(x2) 149 149 ; x must have at least 2 elements 150 IF nx LT 2 THEN stop 150 IF nx LT 2 THEN stop 151 151 ; y must have the same number of elements than x 152 152 IF nx NE ny THEN stop 153 153 ; x be monotonically increasing 154 IF min(x[1:nx-1]-x[0:nx-2]) LE 0 THEN stop 154 IF min(x[1:nx-1]-x[0:nx-2]) LE 0 THEN stop 155 155 ; x2 be monotonically increasing 156 156 IF N_ELEMENTS(X2) GE 2 THEN $ 157 IF min(x2[1:nx2-1]-x2[0:nx2-2]) LE 0 THEN stop 157 IF min(x2[1:nx2-1]-x2[0:nx2-2]) LE 0 THEN stop 158 158 ; y be monotonically increasing 159 IF min(y[1:ny-1]-y[0:ny-2]) LT 0 THEN stop 159 IF min(y[1:ny-1]-y[0:ny-2]) LT 0 THEN stop 160 160 ;--------------------------------- 161 161 ; first check: check if two consecutive values are equal … … 172 172 xinx2_1 = value_locate(x2, x[bad+1]) 173 173 ; 174 ; left side ... if there is x2 values smaller that x[bad[0]]. 174 ; left side ... if there is x2 values smaller that x[bad[0]]. 175 175 ; we force ypn_1 = 0.0d 176 176 IF xinx2[0] NE -1 THEN BEGIN … … 178 178 IF xinx2[0] NE 0 THEN stop 179 179 y2[0] = y[0] 180 ENDIF ELSE BEGIN 180 ENDIF ELSE BEGIN 181 181 y2[0:xinx2[0]] $ 182 182 = spl_incr(x[0:bad[0]], y[0:bad[0]], x2[0:xinx2[0]] $ 183 183 , yp0 = yp0, ypn_1 = 0.0d) 184 ENDELSE 185 ENDIF 184 ENDELSE 185 ENDIF 186 186 ; flat section 187 187 IF xinx2_1[0] NE -1 THEN $ … … 206 206 ENDFOR 207 207 ENDIF 208 ; right side ... if there is x2 values larger that x[bad[cntbad-1]+1]. 208 ; right side ... if there is x2 values larger that x[bad[cntbad-1]+1]. 209 209 ; we force yp0 = 0.0d 210 210 IF xinx2_1[cntbad-1] NE nx2-1 THEN $ … … 237 237 ; 238 238 ; we define the new values of the keyword ypn_1: 239 ; if the first derivative of the last value of x is negative 239 ; if the first derivative of the last value of x is negative 240 240 ; we define the new values of the keyword ypn_1 to 0.0d0 241 IF bad[cntbad-1] EQ nx-1 THEN BEGIN 241 IF bad[cntbad-1] EQ nx-1 THEN BEGIN 242 242 ypn_1new = 0.0d 243 243 ; we remove this case from the list … … 248 248 ; 249 249 ; we define the new values of the keyword yp0: 250 ; if the first derivative of the first value of x is negative 250 ; if the first derivative of the first value of x is negative 251 251 ; we define the new values of the keyword yp0 to 0.0 252 252 IF bad[0] EQ 0 THEN BEGIN … … 265 265 ; else: there is still cases with negative derivative ... 266 266 ; we will cut spl_incr in n spl_incr and specify yp0, ypn_1 267 ; for each of this n spl_incr 267 ; for each of this n spl_incr 268 268 ENDIF ELSE BEGIN 269 269 ; define xinx2: see help of value_locate … … 273 273 xinx2 = value_locate(x2, x[bad]) 274 274 y2 = dblarr(nx2) 275 ; left side ... if there is x2 values smaller that x[bad[0]]. 275 ; left side ... if there is x2 values smaller that x[bad[0]]. 276 276 ; we force ypn_1 = 0.0d 277 277 IF xinx2[0] NE -1 THEN $ … … 280 280 , yp0 = yp0new, ypn_1 = 0.0d) 281 281 ; middle pieces ... if cntbad gt 1 then we have to cut spl_incr in 282 ; more than 2 pieces -> we have middle pieces for which 282 ; more than 2 pieces -> we have middle pieces for which 283 283 ; we force yp0 = 0.0d and ypn_1 = 0.0d 284 284 IF cntbad GT 1 THEN BEGIN … … 295 295 ENDFOR 296 296 ENDIF 297 ; right side ... if there is x2 values larger that x[bad[cntbad-1]]. 297 ; right side ... if there is x2 values larger that x[bad[cntbad-1]]. 298 298 ; we force yp0 = 0.0d 299 299 IF xinx2[cntbad-1] NE nx2-1 THEN $ … … 302 302 , x2[xinx2[cntbad-1]+1:nx2-1] $ 303 303 , yp0 = 0.0d, ypn_1 = ypn_1new) 304 ENDELSE 304 ENDELSE 305 305 ; we return the checked and corrected value of yfrst 306 306 ; FOR i = 0, nx-1 DO BEGIN 307 307 ; same = where(abs(x2- x[i]) LT 1.e-10, cnt) 308 ; ; IF cnt NE 0 THEN y2[same] = y[i] 308 ; ; IF cnt NE 0 THEN y2[same] = y[i] 309 309 ; ENDFOR 310 310 RETURN, y2 … … 313 313 ; we can be in this part of the code only if: 314 314 ; (1) spl_incr is called by itself 315 ; (2) none are the first derivative in x are negative (because they have been 316 ; checked and corrected by the previous call to spl_incr, see above) 315 ; (2) none are the first derivative in x are negative (because they have been 316 ; checked and corrected by the previous call to spl_incr, see above) 317 317 ;--------------------------------- 318 318 ; third check: we have to make sure that the first derivative cannot … … 321 321 ; 322 322 ; first we compute the first derivative, next we correct the values 323 ; where we know that the first derivative can be negative. 323 ; where we know that the first derivative can be negative. 324 324 ; 325 325 y2 = spl_interp(x, y, yscd, x2, /double) … … 330 330 ; y''= 6a*X + 2b 331 331 ; if we take X = x[i+1]-x[i] then 332 ; d = y[i]; c = y'[i]; b = 0.5 * y''[i], 332 ; d = y[i]; c = y'[i]; b = 0.5 * y''[i], 333 333 ; a = 1/6 * (y''[i+1]-y''[i])/(x[i+1]-x[i]) 334 ; 334 ; 335 335 ; y'[i] and y'[i+1] are positive so y' can be negative 336 ; between x[i] and x[i+1] only if 336 ; between x[i] and x[i+1] only if 337 337 ; 1) a > 0 338 338 ; ==> y''[i+1] > y''[i] 339 ; 2) y' reach its minimum value between x[i] and x[i+1] 340 ; -> 0 < - b/(3a) < x[i+1]-x[i] 339 ; 2) y' reach its minimum value between x[i] and x[i+1] 340 ; -> 0 < - b/(3a) < x[i+1]-x[i] 341 341 ; ==> y''[i+1] > 0 > y''[i] 342 342 ; … … 412 412 ; in those cases, the first derivative has 2 zero between 413 413 ; x[bad[ib]] and x[bad[ib]+1]. We look for the minimum value of the 414 ; first derivative that correspond to the inflection point of y 414 ; first derivative that correspond to the inflection point of y 415 415 xinfl = -bbb[ib]/(3.0d*aaa[ib]) 416 416 ; we compute the y value for xinfl 417 417 yinfl = aaa[ib]*xinfl*xinfl*xinfl + bbb[ib]*xinfl*xinfl $ 418 418 + ccc[ib]*xinfl + ddd[ib] 419 ; 419 ; 420 420 CASE 1 OF 421 421 ; if y[xinfl] smaller than y[bad[ib]] then we conserve y2 until … … 450 450 , yifrst[bad[ib]+1] $ 451 451 , x2[xinx2_3+1:xinx2_2[ib]]) 452 ENDIF 452 ENDIF 453 453 END 454 454 ; if y[xinfl] bigger than y[bad[ib]+1] then we conserve y2 from … … 480 480 , yifrst[bad[ib]] $ 481 481 , x2[xinx2_1[ib]+1:xinx2_3]) 482 ENDIF 482 ENDIF 483 483 END 484 484 ELSE:BEGIN … … 496 496 , yifrst[bad[ib]] $ 497 497 , x2[xinx2_1[ib]+1:xinx2_3]) 498 499 ENDIF 498 499 ENDIF 500 500 IF xinx2_2[ib] GE xinx2_3+1 THEN BEGIN 501 501 y2[xinx2_3+1:xinx2_2[ib]] $ … … 504 504 , yifrst[bad[ib]+1] $ 505 505 , x2[xinx2_3+1:xinx2_2[ib]]) 506 ENDIF 506 ENDIF 507 507 END 508 508 ENDCASE 509 509 510 END 510 END 511 511 ENDCASE 512 512 ENDIF
Note: See TracChangeset
for help on using the changeset viewer.