From charlesreid1

No edit summary
 
(24 intermediate revisions by the same user not shown)
Line 1: Line 1:
'''Could maybe do an "engineer" approach and a "mathematician" approach... Two ways of presenting the ''same'' material'''
Could maybe do an engineering approach and a mathematical approach... Two ways of presenting the same material


= Introduction =
= Introduction: discretization of derivatives =


== What are we trying to do? ==
== What are we trying to do? ==
Line 44: Line 44:
* Final result: we found a solution that worked for our boundary conditions and our initial conditions
* Final result: we found a solution that worked for our boundary conditions and our initial conditions


''Can we do this with a computer?''   
''Can we find analytical solutions using this approach with a computer?''   


Short answer: not really.
Only when the partial differential equation is really simple... Only when an analytical solution actually exists...


Long answer: Yes, we can. But what happens when we have a complicated, nonlinear partial differential equation?  A partial differential equation that ''has no analytical solution''?
But whether we can find analytical solutions is beside the point.


The point is: who cares if we can find analytical solutions with a computer?  We want a ''different'' method that is more robust than analytical solutions
We want a ''different'' method, a ''more robust'' method, than trying every analytical solution technique


''Functions are continuous.  What is the opposite of continuous?''  Discrete - if we don't want an analytical (functional) solution, we need a discrete solution
''Functions are continuous.  What is the opposite of continuous?''  Discrete - if we don't want an analytical (functional) solution, we need a discrete solution
Line 58: Line 58:




== What is a derivative? ==
== 1st derivative approximations ==
 
(A note on notation: while the following definitions/formulae hold for either partial or full derivatives, the derivative will be written as partial derivatives to keep the presentation general)


Limit definition of a derivative:
Limit definition of a derivative:


<math>
<math>
\frac{d u(x)}{dx} = \displaystyle{ \lim_{\Delta x \rightarrow 0} } \frac{ u(x_0 + \Delta x) - u(x_0) }{ \Delta x }
\frac{\partial u(x)}{ \partial x} = \displaystyle{ \lim_{\Delta x \rightarrow 0} } \frac{ u(x_0 + \Delta x) - u(x_0) }{ \Delta x }
</math>
</math>


Line 95: Line 97:
</math>
</math>


Let's look at this last term...
This is a '''forward difference'''.
 
Let's look at this last term...  This is an "error" in our difference approximation.  So we can replace it with:
 
<math>
- \left. \frac{ \partial^2 u}{\partial x^2} \right|_0 \frac{\Delta x}{2!} = O( \Delta )
</math>
 
where <math>O()</math> is the order of the error.


Tells us two things:
Tells us two things:


1. For the difference approximation of the derivative to be accurate, we have to have <math>\Delta x</math> become really small.
1. For the difference approximation of the derivative to be accurate, we have to have <math>\Delta x</math> become really small to make our error really small.


2. We ALSO have to have <math>\frac{\partial^2 u}{\partial x^2}</math> be really small.
2. We ALSO need <math>\frac{\partial^2 u}{\partial x^2}</math> to be really small.


That means if we're trying to approximate a function with a large second derivative, we need an even smaller <math>\Delta x</math>.
That means if we're trying to approximate a function with a large second derivative, we need an even smaller <math>\Delta x</math>.


(Two images... one a nice 3rd or 5th order polynomial, the other the complex 100th order polynomial... "Just to make sure this concept makes sense.... which one has a larger second derivative?  Which one requires a smaller <math>\Delta x</math>?)
[[Image:loworderpoly_vs_highorderpoly.png]]
 
Applying this concept: the red line is a very high-order polynomial, and the blue line is a low-order polynomial.
 
''Which one has a larger second derivative?''
 
''Which one will require a smaller <math>\Delta x</math>?''
 
We didn't get an ''exact'' answer to the question "how big should <math>\Delta x</math> be?", but we've got an idea now
 
Next, we can write another Taylor series, this time for the point <math>u(x_0 - \Delta x)</math>:
 
<math>
u(x_0 - \Delta x)
= u(x_0)
- \left. \frac{ \partial u }{ \partial x } \right|_0 \Delta x
+ \left. \frac{ \partial^2 u}{ \partial x^2} \right|_0 \frac{ (\Delta x)^2 }{ 2! }
- \left. \frac{ \partial^3 u}{ \partial x^3} \right|_0 \frac{ (\Delta x)^3 }{ 3! }
+ \dots
</math>
 
This is a '''backward difference'''.
 
Rearranging it the same way we did the forward difference,
 
<math>
\left. \frac{ \partial u}{\partial x} \right|_{i}
= - \frac{ u_i - u_{i-1} }{ \Delta x }
+ \frac{ \partial^2 u }{ \partial x^2 } \frac{ \Delta x }{2!}
+ \dots
</math>
 
So this one has an extra error term, that is also <math>O(\Delta x)</math>.
 
Can we represent the derivative some other way?
 
Yes!
 
In fact, we can represent the derivative a whole bunch of different ways.
 
 
 
We've shown approximations of the derivative of <math>u</math> at the point <math>x</math> using the value of <math>u(x-\Delta x)</math> and <math>u(x + \Delta x)</math> - but what about using both?
 
How to approximate the derivative <math>u(x)</math> at <math>x</math> using both the value of <math>u(x-\Delta x)</math> and <math>u(x+\Delta x)</math>?
 
Subtracting the backward difference from the forward difference yields a '''central difference''':
 
<math>
\frac{ \partial u }{ \partial x } = \frac{ u_{i+1} - u_{i-1} }{ 2 \Delta x } + O( (\Delta x)^2 )
</math>
 
The order term is different, because the first-order error terms canceled out. What does that tell us?
 
1. To approximate a given function, the <math>\Delta x</math> for a central difference scheme can be larger than the <math>\Delta x</math> for a forward or backward difference scheme
 
2. If a function has large gradients (and thus a larger error term), a central difference scheme will approximate the derivative better than a forward or backward difference scheme ''for the same <math>\Delta x</math>''
 
<!--
== Alternative approach ==
 
An alternative way to think about derivatives for a discrete function...
 
For a continuous function <math>f(x), x \in X</math>, the limit is defined as:
 
<math>
\displaystyle{ \lim_{\Delta x \rightarrow 0} \frac{ f(x + \Delta x) - f(x) }{ \Delta x } } = \frac{\partial f}{\partial x}
</math>
 
Now, for a discrete function <math>f_{i}(x_{i}) \qquad i=1,2,\dots,N</math> (where <math>N</math> is the number of discrete points used to represent the domain <math>X</math>), the distance between locations at which the function can be sampled can't go to zero, it can only go to <math>X/N</math>(the <math>\Delta x</math> for the discretized domain).  This makes the discrete function's limit:


We didn't get an ''exact'' answer to the question "how big should <math>\Delta x</math> be, but we've got an idea now
<math>
\displaystyle{ \lim_{ \Delta x \rightarrow \frac{X}{N} } \frac{ f(x + \Delta x) - f(x) }{ \Delta x } } = \frac{f_{i+1} - f_{i}}{\frac{X}{N}} \approx \frac{ \partial f }{ \partial x }
</math>


While this isn't rigorous, it's another way to think about the discrete vs. continuous case.
-->


== 2nd derivative approximations ==


What if the last term looked like this?
As expected, one can also discretize second-order derivatives.  Adding the backward and forward differencing schemes and rearranging yields a '''second-order derivative central difference''' scheme:


<math>
<math>
\left. \frac{\partial^2 u}{\partial x^2} \right|_0 \frac{(\Delta x)^2}{2}
\displaystyle{ \left. \frac{\partial^2 u}{\partial x^2} \right|_{i} = \frac{ u_{i+1} - 2 u_{i} + u_{i-1} }{ (\Delta x)^2 } } + O( (\Delta x)^2 )
</math>
</math>


What does that tell us?
Like the first derivative, the second derivative may be approximated in a number of different ways.  Alternative discretization methods (analogous forward and backward differences) can be derived by expanding various points neighboring <math>x_i</math> using Taylor series, and combining these Taylor series approximations to yield discrete expressions for second derivatives.
 
For example, the second derivative forward differencing scheme is:
 
<math>
\left. \frac{ \partial^2 u }{ \partial x^2 } \right|_i = \frac{ u_i - 2 u_{i+1} + u_{i+2} }{ (\Delta x)^2 } + O[ (\Delta x) ]
</math>
 
and the second derivative backward differencing scheme is:
 
<math>
\left. \frac{ \partial^2 u }{ \partial x^2 } \right|_i = \frac{ u_i - 2 u_{i-1} + u_{i-2} }{ (\Delta x)^2 } + O[ (\Delta x) ]
</math>
 
== Alternative approaches ==
 
===Integral Form===
 
So far, we've been approximating derivatives using differences (via Taylor series).
 
An alternative approach, one that's more physically intuitive, is to approximate the ''integral'' of the derivative.
 
For example, if we are looking at a property of a fluid contained in a pipe, one way to approximate the change in the property is to use a macro balance:
 
<math>
\mbox{in} - \mbox{out} + \mbox{generated} - \mbox{consumed} = \mbox{accumulated}
</math>
 
This can be applied to the volume of the entire pipe <math>\Omega</math> for a property <math>\phi</math>.
 
First, each term can be represented using an integral quantity:
 
<math>
\begin{array}{rcl}
\mbox{in}  &=& \int_{S_{in}} \phi u_{\phi,in} d A_{in} \\
\mbox{out} &=& \int_{S_{out}} \phi u_{\phi,out} d A_{out} \\
\mbox{generated} &=& \int_{\Omega} G_{\phi} dV \\
\mbox{consumed}  &=& \int_{\Omega} C_{\phi} dV \\
&& \\
\mbox{accumulated} &=& \int_{\Omega} \frac{ \partial \phi }{ \partial t } dV
\end{array}
</math>
 
These can then be assembled into the integral form of the differential equation for <math>\phi</math>:
 
<math>
\int_{\Omega} \frac{ \partial \phi }{ \partial t } dV = \int_{S_{in}} \phi u_{\phi,in} d A_{in} - \int_{S_{out}} \phi u_{\phi,out} d A_{out} + \int_{\Omega} G_{\phi} dV - \int_{\Omega} C_{\phi} dV
</math>
 
When this approach is then applied to arbitrarily small control volumes in the fluid, and various assumptions made that simplify the above integral terms, this yields a large number of discrete equations, analogous to the set of algebraic equations that result in applying the finite difference method to a partial differential equation.  This method is known as the [[Finite volume]] method.
 
 
 
<!--
 
* Taylor series expansions
* Polyomial fitting
* integral method (a.k.a. micro-integral method)
* finite-volume (control-volume) approach
 
* More info may (possibly) be found in "Chung - CFD"
 
Likewise, discrete expressions for third and higher-order derivatives can be similarly derived.
 
-->
 
= Error Analysis =
 
Tannehill Fletcher p. 52
 
== Truncation error ==


1. The value of <math>\Delta x</math> can be bigger to approximate this function
== Stability ==


== Conservative ==






= References =


* What we're trying to do with it - i.e. discretize it (define it, explain it)
{{Cite book
|author1=Tannehill, John C.
|author2=Anderson, Dale A.
|author3=Pletcher, Richard H.
|title=Computational Fluid Mechanics and Heat Transfer
|edition=2nd
|publisher=Taylor and Francis
|year=1997
|isbn=978-1560320463
}}


* Definition of limit
{{Cite book
|author=Anderson, John D.
|title=Computational Fluid Dynamics: The Basics with Applications
|year=1995
|publisher=McGraw Hill
|isbn=0-07-001685-2
}}


* Derivative is to continuous function what algebraic difference is to discrete function
** i.e. imagine a limit for a discrete function...... it wouldn't turn into a derivative!
** <math>\lim_{\Delta x \rightarrow \delta}</math> versus <math>\lim_{\Delta x \rightarrow 0}</math>


* Taylor series
{{PDEs}}
** Expand a nearby point <math>(x+\Delta x)</math> about another point <math>(x)</math>
** Rearrange to put difference in terms of derivative
** Truncation error, order of error

Latest revision as of 08:01, 4 June 2011

Could maybe do an engineering approach and a mathematical approach... Two ways of presenting the same material

Introduction: discretization of derivatives

What are we trying to do?

Before discussing discretization, we must first discuss what we're discretizing

We start with some differential equation - either partial differential equation or ordinary differential equation

Let's say we have a function $ u $ of space and time $ x,t $

What is the general form of a partial differential equation for $ u $? (Introduction_to_partial_differential_equations)

$ F(x,t,u_x,u_xx,\dots,u_t,u_tt,\dots) = 0 $

Revisit example of heat equation, solved analytically using separation of variables (Analytical_solution_of_PDEs#Example_3:_Heat_Equation):

$ \Theta_t = \alpha^2 \Theta_{xx} $

How did we treat $ \Theta $? How did we solve the PDE? We assumed a form for the solution, plugged it in, and found out some additional information

For example, we assumed a solution of the form $ X(x) T(t) $, and then we found, for time:

$ T = c_1 \exp{( -\lambda^2 \alpha^2 t )} $

and something similar for $ X(x) $.

So the answer is: we solved the PDE by finding functions that satisfied certain conditions...

Solution approach:

  • We assumed a form for the solution
    • Found two ordinary differential equations that the solution had to satisfy
  • We then had a general solution (that worked for any boundary and initial conditions)
    • Found values for constants by plugging our boundary conditions into the general solution
  • When then had a (less general) solution that worked for our boundary conditions, and worked for any initial conditions
    • Found values for the remaining constants by plugging our initial conditions into the solution
  • Final result: we found a solution that worked for our boundary conditions and our initial conditions

Can we find analytical solutions using this approach with a computer?

Only when the partial differential equation is really simple... Only when an analytical solution actually exists...

But whether we can find analytical solutions is beside the point.

We want a different method, a more robust method, than trying every analytical solution technique

Functions are continuous. What is the opposite of continuous? Discrete - if we don't want an analytical (functional) solution, we need a discrete solution

Discrete - means, the solution only exists at discrete points, and is not an exact solution, but only an approximation


1st derivative approximations

(A note on notation: while the following definitions/formulae hold for either partial or full derivatives, the derivative will be written as partial derivatives to keep the presentation general)

Limit definition of a derivative:

$ \frac{\partial u(x)}{ \partial x} = \displaystyle{ \lim_{\Delta x \rightarrow 0} } \frac{ u(x_0 + \Delta x) - u(x_0) }{ \Delta x } $

This is saying, when $ \Delta x $ is "small enough", the algebraic difference $ \frac{ u(x_0 + \Delta x) - u(x_0) }{ \Delta x} $ is a good approximation for the derivative; if $ \Delta x $ is small enough, the representation becomes exact.

OK, so naturally we want to know: how small? How small does $ \Delta x $ have to be?

Using a Taylor series expansion for $ u(x_0 + \Delta x) $ in terms of $ u(x_0) $:

$ u(x_0 + \Delta x) = u(x_0) + \left. \frac{\partial u}{\partial x} \right|_{0} \Delta x + \left. \frac{\partial^2 u}{\partial x^2} \right|_{0} \frac{(\Delta x)^2}{2!} + \dots + \left. \frac{\partial^n u}{\partial x^n} \right|_{\xi} \frac{ (\Delta x)^n }{ n! } \qquad x_0 \leq \xi \leq x_0 + \Delta x $

What does the Taylor series tell us about how small $ \Delta x $ needs to be?

Rearrange the Taylor series to look like the limit definition of the derivative...

$ \left. \frac{\partial u}{\partial x} \right|_{x_0} = \frac{ u(x_0 + \Delta x) - u(x) }{ \Delta x } - \left. \frac{\partial^2 u}{\partial x^2} \right|_0 \frac{\Delta x}{2!} - \dots $

For a discrete representation, values of $ u $ can be indexed at each discrete point using some index $ i $, so that $ u_i = u(x_0) $, $ u_{i+1} = u(x_0 + \Delta x) $, etc...

$ \left. \frac{\partial u}{\partial x} \right|_{i} = \frac{ u_{i+1} - u_{i} }{ \Delta x } - \left. \frac{\partial^2 u}{\partial x^2} \right|_0 \frac{\Delta x}{2!} - \dots $

This is a forward difference.

Let's look at this last term... This is an "error" in our difference approximation. So we can replace it with:

$ - \left. \frac{ \partial^2 u}{\partial x^2} \right|_0 \frac{\Delta x}{2!} = O( \Delta ) $

where $ O() $ is the order of the error.

Tells us two things:

1. For the difference approximation of the derivative to be accurate, we have to have $ \Delta x $ become really small to make our error really small.

2. We ALSO need $ \frac{\partial^2 u}{\partial x^2} $ to be really small.

That means if we're trying to approximate a function with a large second derivative, we need an even smaller $ \Delta x $.

Loworderpoly vs highorderpoly.png

Applying this concept: the red line is a very high-order polynomial, and the blue line is a low-order polynomial.

Which one has a larger second derivative?

Which one will require a smaller $ \Delta x $?

We didn't get an exact answer to the question "how big should $ \Delta x $ be?", but we've got an idea now

Next, we can write another Taylor series, this time for the point $ u(x_0 - \Delta x) $:

$ u(x_0 - \Delta x) = u(x_0) - \left. \frac{ \partial u }{ \partial x } \right|_0 \Delta x + \left. \frac{ \partial^2 u}{ \partial x^2} \right|_0 \frac{ (\Delta x)^2 }{ 2! } - \left. \frac{ \partial^3 u}{ \partial x^3} \right|_0 \frac{ (\Delta x)^3 }{ 3! } + \dots $

This is a backward difference.

Rearranging it the same way we did the forward difference,

$ \left. \frac{ \partial u}{\partial x} \right|_{i} = - \frac{ u_i - u_{i-1} }{ \Delta x } + \frac{ \partial^2 u }{ \partial x^2 } \frac{ \Delta x }{2!} + \dots $

So this one has an extra error term, that is also $ O(\Delta x) $.

Can we represent the derivative some other way?

Yes!

In fact, we can represent the derivative a whole bunch of different ways.


We've shown approximations of the derivative of $ u $ at the point $ x $ using the value of $ u(x-\Delta x) $ and $ u(x + \Delta x) $ - but what about using both?

How to approximate the derivative $ u(x) $ at $ x $ using both the value of $ u(x-\Delta x) $ and $ u(x+\Delta x) $?

Subtracting the backward difference from the forward difference yields a central difference:

$ \frac{ \partial u }{ \partial x } = \frac{ u_{i+1} - u_{i-1} }{ 2 \Delta x } + O( (\Delta x)^2 ) $

The order term is different, because the first-order error terms canceled out. What does that tell us?

1. To approximate a given function, the $ \Delta x $ for a central difference scheme can be larger than the $ \Delta x $ for a forward or backward difference scheme

2. If a function has large gradients (and thus a larger error term), a central difference scheme will approximate the derivative better than a forward or backward difference scheme for the same $ \Delta x $


2nd derivative approximations

As expected, one can also discretize second-order derivatives. Adding the backward and forward differencing schemes and rearranging yields a second-order derivative central difference scheme:

$ \displaystyle{ \left. \frac{\partial^2 u}{\partial x^2} \right|_{i} = \frac{ u_{i+1} - 2 u_{i} + u_{i-1} }{ (\Delta x)^2 } } + O( (\Delta x)^2 ) $

Like the first derivative, the second derivative may be approximated in a number of different ways. Alternative discretization methods (analogous forward and backward differences) can be derived by expanding various points neighboring $ x_i $ using Taylor series, and combining these Taylor series approximations to yield discrete expressions for second derivatives.

For example, the second derivative forward differencing scheme is:

$ \left. \frac{ \partial^2 u }{ \partial x^2 } \right|_i = \frac{ u_i - 2 u_{i+1} + u_{i+2} }{ (\Delta x)^2 } + O[ (\Delta x) ] $

and the second derivative backward differencing scheme is:

$ \left. \frac{ \partial^2 u }{ \partial x^2 } \right|_i = \frac{ u_i - 2 u_{i-1} + u_{i-2} }{ (\Delta x)^2 } + O[ (\Delta x) ] $

Alternative approaches

Integral Form

So far, we've been approximating derivatives using differences (via Taylor series).

An alternative approach, one that's more physically intuitive, is to approximate the integral of the derivative.

For example, if we are looking at a property of a fluid contained in a pipe, one way to approximate the change in the property is to use a macro balance:

$ \mbox{in} - \mbox{out} + \mbox{generated} - \mbox{consumed} = \mbox{accumulated} $

This can be applied to the volume of the entire pipe $ \Omega $ for a property $ \phi $.

First, each term can be represented using an integral quantity:

$ \begin{array}{rcl} \mbox{in} &=& \int_{S_{in}} \phi u_{\phi,in} d A_{in} \\ \mbox{out} &=& \int_{S_{out}} \phi u_{\phi,out} d A_{out} \\ \mbox{generated} &=& \int_{\Omega} G_{\phi} dV \\ \mbox{consumed} &=& \int_{\Omega} C_{\phi} dV \\ && \\ \mbox{accumulated} &=& \int_{\Omega} \frac{ \partial \phi }{ \partial t } dV \end{array} $

These can then be assembled into the integral form of the differential equation for $ \phi $:

$ \int_{\Omega} \frac{ \partial \phi }{ \partial t } dV = \int_{S_{in}} \phi u_{\phi,in} d A_{in} - \int_{S_{out}} \phi u_{\phi,out} d A_{out} + \int_{\Omega} G_{\phi} dV - \int_{\Omega} C_{\phi} dV $

When this approach is then applied to arbitrarily small control volumes in the fluid, and various assumptions made that simplify the above integral terms, this yields a large number of discrete equations, analogous to the set of algebraic equations that result in applying the finite difference method to a partial differential equation. This method is known as the Finite volume method.



Error Analysis

Tannehill Fletcher p. 52

Truncation error

Stability

Conservative

References

Tannehill, John C.; Anderson, Dale A.; Pletcher, Richard H. (1997). Computational Fluid Mechanics and Heat Transfer (2nd ed.). Taylor and Francis. ISBN 978-1560320463. 

Anderson, John D. (1995). Computational Fluid Dynamics: The Basics with Applications. McGraw Hill. ISBN 0-07-001685-2.