- #1
apgt512
- 2
- 0
Homework Statement
Solve the time dependent 1D heat equation using the Crank-Nicolson method.
The conditions are a interval of length L=1, initial distribution of temperature is u(x,0) = 2-1.5x+sin(pi*x) and the temperature in the ends of the interval are u(0,t) = 2; u(1,t) = 0.5.
Homework Equations
∂u/∂t = √2 (∂2/∂x2) u.
The Attempt at a Solution
I'm really lost here. Since we started seeing finite difference methods and the heat equation I got lost. What I know is: this can be seen as a matrix problem A*u = b. A is a tridiagonal matrix and I have to find the solution matrix u. I think I know what are the elements of the tridiagonal matrix but not sure.
What I don't know is once I get u, that's the temperature at a certain point in time, then how do I solve for the next point in time?. Do I take the u I got as the b for the next step?.
This is the code I've written so far. It uses the LU decomposition method to find the solution matrix. It's written in C.
Code:
#include <stdio.h>
#include <math.h>
double * lu(double *diag,double *diag_sup,double *diag_inf,double *b){
int i;
double w[n],u[n],y[n];
static double x[n];
w[0]=diag[0];
for(i=0;i<(n-1);i++){
u[i]=diag_sup[i]/w[i];
w[i+1]=diag[i+1]-diag_inf[i+1]*u[i];
}
y[0]=b[0]/w[0]; for(i=1;i<n;i++){
y[i]=(b[i]-y[i-1]*diag_inf[i])/w[i];
}
x[n-1]=y[n-1];
for(i=(n-1);i>=1;i--){
x[i-1]=y[i-1]-x[i]*u[i-1];
}
return x;
}
int main(){
//Variables
double alfa,beta,delta,gamma;
double a[n],e[n],c[n],b[n];
int i;
//Boundary conditions
alfa=0.;
beta=0.;
delta=0.;
gamma=0.;
//Tridiagonal matrix
for (i=0; i<n; i++){
a[i] = 5.;
e[i] = 2.;
c[i+1] = 2.;
}
//Right side matrix
for(i=0;i<n;i++){
b[i]=i;
}
//Solution
double *sol;
sol = lu(a,e,c,b);
for(i=0;i<n;i++){
printf("%lf %lf\n",i*h,*(sol+i));
}
}