source: CIVL/examples/accuracy/derivative.cvl@ beab7f2

main test-branch
Last change on this file since beab7f2 was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 1.3 KB
Line 
1/* Discrete derivative function using central differencing, which is
2 * second-order accurate, except at the two end-points (where it is
3 * first-order). To verify with CIVL, type:
4 * civl verify derivative.cvl
5 */
6$input double dx; // delta x
7$assume(0<dx && dx<1);
8$input int num_elements;
9$assume(num_elements >= 1);
10$input double in[num_elements];
11double out[num_elements];
12// the following says rho is a function from R to R which has 3 continuous
13// derivatives in the closed interval [-1,1]:
14$abstract $differentiable(3, [-1,1]) $real rho($real x);
15
16/* Computes discrete derivative by central differencing.
17 * Right end-point is computed by backwards differencing.
18 * Left end-point is computed by forward differencing.
19 */
20void differentiate(int n, double y[], double h, double result[]) {
21 $assume(n*h<=1);
22 $assume($forall (int i : 0..n-1) y[i] == rho(i*h));
23
24 /*@ loop invariant 1<=i && i<=n-1;
25 @ loop invariant \forall int j; (1<=j && j<i) ==> result[j] == (y[j+1]-y[j-1])/(2*h);
26 @ loop assigns i, result[1 .. n-2];
27 @*/
28 for (int i=1; i<n-1; i++) {
29 result[i] = (y[i+1]-y[i-1])/(2*h);
30 }
31 result[0] = (y[1]-y[0])/h;
32 result[n-1] = (y[n-1] - y[n-2])/h;
33 $assert($forall (int i : 1..n-2) result[i]-$D[rho,{x,1}](i*h) == $O(h*h));
34}
35
36int main() {
37 differentiate(num_elements, in, dx, out);
38}
Note: See TracBrowser for help on using the repository browser.