forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise2b.cpp
116 lines (96 loc) · 2.21 KB
/
exercise2b.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <stdio.h>
#include <math.h>
#include "polylib.h"
/*
To compile (in same directory as polylib.c and polylib.h):
g++ -c polylib.c
g++ -c exercise2b.cpp
g++ -o exercise2b exercise2b.o polylib.o
*/
using namespace std;
using namespace polylib;
double *dvector(int np);
double **dmatrix(int n);
void *func(int np, double *z, double *p);
void *diff(int np, double **D, double *p, double *pd, double *Jac);
void *chi(int np, double *x, double *z, double *Jac, double *bound);
main()
{
int np=8;
double *w, *x,*z,*p,*pd,**D,**Dt,*Jac,*bound;
/* set up vectors and matrices */
x = dvector(np);
z = dvector(np);
w = dvector(np);
p = dvector(np);
pd = dvector(np);
D = dmatrix(np);
Dt = dmatrix(np);
Jac = dvector(1);
bound = dvector(2);
/* define domain [bound[0],bound[1]] */
bound[0] = 2;
bound[1] = 10;
/* get zeros and weights */
zwgll(z, w, np);
/* get differentiation matrix D */
Dgll(D, Dt, z, np);
/* get mapping details; x-locations and Jacobian */
chi(np, x, z, Jac, bound);
/* calculate p=x^7 at np points x */
func(np, x, p);
/* differentiate p and put result in pd */
diff(np, D, p, pd, Jac);
/* generate output */
cout << "\n x\n\n";
for(int i=0;i<np;i++){
cout << x[i] << "\n";
}cout << "\n\n du/dx\n\n";
for(int i=0;i<np;i++){
cout << pd[i] << "\n";
}cout << "\n";
}
double *dvector(int n)
{
double *v;
v = (double *)malloc(n*sizeof(double));
return v;
}
double **dmatrix(int n)
{
double **A;
A = (double **)malloc(n*sizeof(double *));
A[0] = (double *)malloc(n*n*sizeof(double));
for(int i=1;i<n;i++){
A[i] = A[i-1]+n;
}
return A;
}
void *func(int np, double *z, double *p)
{
register int pow=7;
for(int i=0;i<np;i++){
p[i] = 1;
for(int j=0;j<pow;j++){
p[i] = p[i]*z[i];
}
}
}
void *diff(int np, double **D, double*p, double *pd, double *Jac)
{
for(int i=0;i<np;i++){
pd[i] = 0;
for(int j=0;j<np;j++){
pd[i] = pd[i] + D[i][j]*p[j];
}
pd[i] = pd[i]/Jac[0];
}
}
void *chi(int np, double *x, double *z, double *Jac,double *bound)
{
for(int i=0;i<np;i++){
x[i] = ((1 - z[i])/2)*bound[0] + ((1 + z[i])/2)*bound[1];
}
Jac[0] = (-bound[0]/2) + (bound[1]/2);
}