-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexact_solution.c
More file actions
106 lines (94 loc) · 3.26 KB
/
exact_solution.c
File metadata and controls
106 lines (94 loc) · 3.26 KB
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
/* File: exact_solution.c
* Author: Peter Pacheco
*
* Purpose: Compute exact solution to the one-dimensional heat equation
* on [0,1]x[0,1].
*
* Compile: gcc -g -Wall -o exact_solution exact_solution.c -lm
* Run: ./exact_solution
*
* Input: m: the number of segments in the metal bar
* n: the number of time intervals
* k: the ``frequency'' parameter given to input_data.c
* Output: For each time t = 0, 1/n, 2/n, . . . , (n-1)/n, 1,
* t and u(x,t), for x = 0, 1/m, 2/m, . . . , (m-1)/m, 1.
*
* Notes:
* 1. This program will only compute solutions when the input
* is generated by the program input_data.c. There will be
* no relation between the solution generated by this program
* if general input is used.
* 2. Assumes constant 0 boundary conditions; i.e., u(0,t) = u(1,t) = 0,
* for all t.
*/
#include <stdio.h>
#include <math.h>
void Get_input(int* m_p, int* n_p, int* k_p);
void Print_exact(int m, double h_x, double t, int k);
double u_exact(double x, double t, int k);
/*-------------------------------------------------------------------*/
int main(void) {
int m, n, k, j;
double h_x, h_t;
double t;
Get_input(&m, &n, &k);
h_x = 1.0/m;
h_t = 1.0/n;
for (j = 0; j <= n; j++) {
t = j*h_t;
Print_exact(m, h_x, t, k);
}
return 0;
} /* main */
/*-------------------------------------------------------------------*/
/* Function: Get_input
* Purpose: Read in the number of segments, number of intervals,
* and frequency parameter.
* Output args: m_p: pointer to number of segments in bar
* n_p: pointer to number of time intervals
* k_p: pointer to frequency parameter
*/
void Get_input(int* m_p, int* n_p, int* k_p) {
printf("What's m? (number of segments in bar)?\n");
scanf("%d", m_p);
printf("What's n? (number of time intervals)?\n");
scanf("%d", n_p);
printf("What's the frequency parameter k\n");
scanf("%d", k_p);
} /* Get_input */
/*-------------------------------------------------------------------*/
/* Function: Print_exact
* Purpose: Print the values of the exact solution to the heat
* equation at a given time t.
* Input args: m: the number of segments into which the bar is
* divided
* h_x: the length of a segment (= 1/m).
* t: the time of interest
* k: the "frequency" of the solution.
*/
void Print_exact(int m, double h_x, double t, int k) {
int i;
double x;
printf("%.3f ", t);
for (i = 0; i <= m; i++) {
x = i*h_x;
printf("%.3f ", u_exact(x,t,k));
}
printf("\n");
} /* Print_exact */
/*-------------------------------------------------------------------*/
/* Function: u_exact
* Purpose: Compute the exact solution to the heat
* equation at a given point x and a given time t.
* Input args: x: the point on the bar.
* t: the time of interest
* k: the "frequency" of the solution.
*/
double u_exact(double x, double t, int k) {
double pi = 4.0*atan(1.0);
double t_fact;
double x_fact;
t_fact = exp(-k*k*pi*pi*t);
x_fact = sin(k*pi*x);
return t_fact*x_fact;
} /* u_exact */