Finite Difference Approximation, Mathematica code

In summary, the conversation discusses the problem of programming a three component decay chain using finite difference approximation. The person has written the code but is getting an erroneous answer due to an error they cannot find. They suspect the issue lies in the second part of the chain and provide the governing differential equation and forward approximation. They also include images of the real and approximate graphs. After further examination, they realize that the numerical solution was correct but the analytical solution was plotted incorrectly. They then ask for any suggestions on streamlining the code.
  • #1
timman_24
52
0

Homework Statement



I have to program a three component decay chain using finite difference approximation. I understand finite difference and have written my code, but I have an error I can not find which is giving me an erroneous answer. The curve is correct, but the magnitude of the y-axis values is too high. I've gone through it for hours but can not find the mistake.

2. The attempt at a solution

It should be pretty basic stuff, but there is something screwy in there. My problem starts with the second chain (N2a and N2b.)

The governing differential equation for that section is:
[tex]\frac{dN_{2}(t)}{dt}=-\lambda _{2}N_{2}(t)+\lambda _{1}N_{1}(t)[/tex]

My forward approximation is:
[tex]N_2b=(\lambda_{1} N_1a - \lambda_{2} N_2a) (tchange) + N_2a[/tex]

For some reason result of this is far too large, but the curve looks to be correct. Here is an image of the real versus approx graph I get:

Approx:
[PLAIN]http://img19.imageshack.us/img19/9539/n2approxgraph.png [Broken]
Real (analytical):
[PLAIN]http://img222.imageshack.us/img222/5544/n2realgraph.png [Broken]

Here is a portion of the code in question:

Code:
lambdaA = .6931;
lambdaB = .13863;
tchange = .05;
ttotal = 75;
A0 = 100;
B0 = 0;
C0 = 0;

timesteps = ttotal/tchange;
list = Table[{i*tchange, 0}, {i, timesteps}];
list[[1, 2]] = A0;
list2 = Table[{i*tchange, 0}, {i, timesteps}];
list2[[1, 2]] = B0;
list3 = Table[{i*tchange, 0}, {i, timesteps}];
list3[[1, 2]] = C0;
For[i = 1, i < timesteps, i++,
 N1a = Evaluate[Extract[list, {i, 2}]];
 N1b = Evaluate[N1a (1 - tchange*lambdaA)];
 list[[i + 1, 2]] = N1b;
 N2a = Extract[list2, {i, 2}];
 N2b = Evaluate[(lambdaA N1a - lambdaB N2a) tchange + N2a];
 list2[[i + 1, 2]] = N2b;
 N3a = Extract[list3, {i, 2}];
 N3b = Evaluate[tchange*lambdaB*N2b + N3a];
 list3[[i + 1, 2]] = N3b;]

Thanks a lot!
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
Oh my god! Its not wrong! I plotted the analytical solution incorrectly. How about that? The numerical solution being correct but the analytical being wrong. Ugh!

Well, anyway.

Anyone see any ways to streamline the code?
 

1. What is a finite difference approximation?

A finite difference approximation is a numerical method used to approximate the solutions to differential equations. It involves dividing the domain of the function into small intervals and using discrete values to approximate the derivatives of the function at each point.

2. How is the finite difference method implemented in Mathematica code?

The finite difference method can be implemented in Mathematica by defining the function to be approximated and its corresponding derivatives, choosing appropriate step sizes for the intervals, and using built-in functions such as NDSolve and D to solve the differential equation and compute the derivatives, respectively.

3. What are the advantages of using the finite difference method?

One advantage of the finite difference method is its simplicity and ease of implementation. It also allows for the handling of complex boundary conditions and non-linear functions. Additionally, it can be used for a wide range of differential equations and provides accurate results when the step size is appropriately chosen.

4. Are there any limitations to the finite difference method?

One limitation of the finite difference method is that it may not provide accurate solutions for highly non-linear or stiff differential equations. It also requires a large number of grid points to achieve high accuracy, which can be computationally expensive.

5. How can the accuracy of a finite difference approximation be improved?

The accuracy of a finite difference approximation can be improved by using smaller step sizes for the intervals, which reduces the error in the approximation. Additionally, using higher-order finite difference schemes or adaptive methods can also improve the accuracy of the approximation.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
10
Views
1K
  • Atomic and Condensed Matter
Replies
4
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
Replies
2
Views
2K
  • Differential Equations
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Replies
0
Views
2K
Back
Top