Overview
This example uses crank nicolson to solve the heat equation in one dimension.
{% \frac{\partial^2 u}{\partial x^2} = \frac{\partial u}{\partial t} + f(x,t) %}
where we take {% f(x,t) = 0 %}
and given the boundary condition:
{% u(x,0) = sin(2 \pi x) %}
The analytic solution of the equation is
{% u(x,t) = sin(2 \pi x) \times exp[-4 \pi^2 t] %}
Scripts
The following code sets the function {% f(x, y) %} to just return 0. It sets {% x \in [0,1] %} and {% t \in [0,0.1] %}. Then it creates a function to represent the boundary condition.
Next, we import the finite-difference module and run the crankNicholsonHeat function to obtain the solution. The solution is returned as and array of arrays, mapping out the values at each point in the specified grid.
The last section of the code is designed to match the calculated values against the analytic solution. IN particular, we create a dataset that we will chart. The code itereates through the points in the grid and computes the difference between the computed value and the actual values.
For the purposes of visualization, we collect the values at the point {% t=950 %} in order to chart the two sets. The data is set back to the desktop using
{% $val.set('data',data); %}
This makes the dataset accessible to the charting app.