-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode61.c
77 lines (68 loc) · 1.92 KB
/
code61.c
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
/************************* Program 6.1 ****************************/
/* */
/************************************************************************/
/* Please Note: */
/* */
/* (1) This computer program is written by Tao Pang in conjunction with */
/* his book, "An Introduction to Computational Physics," published */
/* by Cambridge University Press in 1997. */
/* */
/* (2) No warranties, express or implied, are made for this program. */
/* */
/************************************************************************/
#include <stdio.h>
#include <math.h>
#define NMAX 99
#define MMAX 500
main()
/* This program solves the problem of a person sitting on a
bench as described in the text. Copyright (c) Tao Pang 1997. */
{
int n,i;
double xl,h,h2,y0,x0,xd,rho,g,d,e,e0,f0;
double b[NMAX],x[NMAX],y[NMAX],w[NMAX],u[NMAX];
n = NMAX;
xl = 3;
h = xl/(n+1);
h2 = h*h;
y0 = 1e9*pow(0.03,3)*0.2/3;
x0 = 0.25;
rho = 3;
g = 9.8;
f0 = 200;
d = 2;
e = -1;
e0 = 1.0/exp(1);
/* Find elements in L and U */
w[0] = d;
u[0] = e/d;
for (i = 1; i < n; ++i)
{
w[i] = d-e*u[i-1];
u[i] = e/w[i];
}
/* Assign the array B */
for (i = 0; i < n; ++i)
{
xd = h*(i+1);
b[i] = -h2*rho*g;
if (fabs(xd-xl/2) < x0)
b[i] = b[i]-h2*f0*(exp(-pow((xd-xl/2)/x0,2))-e0);
b[i] = b[i]/y0;
}
/* Find the solution of the curvature */
y[0] = b[0]/w[0];
for (i = 1; i < n; ++i)
{
y[i] = (b[i]+y[i-1])/w[i];
}
x[n-1] = y[n-1];
for (i = n-2; i >= 0; i = i-1)
{
x[i] = y[i]-u[i]*x[i+1];
}
for (i = 0; i < n; ++i)
{
printf("%16.8lf %16.8lf\n", h*(i+1),100*x[i]);
}
}