<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4222509099263598785</id><updated>2011-11-27T16:42:14.761-08:00</updated><title type='text'>Physics Simulation</title><subtitle type='html'>A collection of tools for simulations</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://physics-simulation.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4222509099263598785/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://physics-simulation.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>I Wayan Sudiarta</name><uri>http://www.blogger.com/profile/10559430452013301600</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>9</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4222509099263598785.post-7792081329419482531</id><published>2008-10-07T10:56:00.000-07:00</published><updated>2008-10-07T11:02:47.715-07:00</updated><title type='text'>Books on Eigenvalue Problem</title><content type='html'>Solutions of many physical systems involve finding eigenvalues and eigenvectors. For large systems, iterative methods are often used instead of exact diagonalization method. I think these books may be useful for you if you want to learn and solve large eigenvalue problems.&lt;br /&gt;&lt;br /&gt;Iterative methods for sparse linear systems (1st edition) and Numerical Methods for Large Eigenvalue Problems by Yousef Saad. Both can be read at &lt;a href="http://www-users.cs.umn.edu/~saad/books.html"&gt;http://www-users.cs.umn.edu/~saad/books.html &lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4222509099263598785-7792081329419482531?l=physics-simulation.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://physics-simulation.blogspot.com/feeds/7792081329419482531/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4222509099263598785&amp;postID=7792081329419482531' title='48 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4222509099263598785/posts/default/7792081329419482531'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4222509099263598785/posts/default/7792081329419482531'/><link rel='alternate' type='text/html' href='http://physics-simulation.blogspot.com/2008/10/books-on-eigenvalue-problem.html' title='Books on Eigenvalue Problem'/><author><name>I Wayan Sudiarta</name><uri>http://www.blogger.com/profile/10559430452013301600</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>48</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4222509099263598785.post-4506860470878429778</id><published>2008-09-05T02:59:00.000-07:00</published><updated>2008-10-07T10:24:11.741-07:00</updated><title type='text'>A practical guide to computer simulations</title><content type='html'>I just stumbled upon a useful article which explains concisely how to do computer simulation from programming to visualization. &lt;br /&gt;&lt;br /&gt;&lt;a href="http://arxiv.org/abs/cond-mat/0111531"&gt;http://arxiv.org/abs/cond-mat/0111531&lt;/a&gt;&lt;br /&gt;&lt;span style="font-style:italic;"&gt;A practical guide to computer simulations&lt;/span&gt;&lt;br /&gt;abstract:&lt;br /&gt;Here practical aspects of conducting research via computer simulations are discussed. The following issues are addressed: software engineering, object-oriented software development, programming style, macros, make files, scripts, libraries, random numbers, testing, debugging, data plotting, curve fitting, finite-size scaling, information retrieval, and preparing presentations.&lt;br /&gt;Because of the limited space, usually only short introductions to the specific areas are given and references to more extensive literature are cited. All examples of code are in C/C++.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4222509099263598785-4506860470878429778?l=physics-simulation.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://physics-simulation.blogspot.com/feeds/4506860470878429778/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4222509099263598785&amp;postID=4506860470878429778' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4222509099263598785/posts/default/4506860470878429778'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4222509099263598785/posts/default/4506860470878429778'/><link rel='alternate' type='text/html' href='http://physics-simulation.blogspot.com/2008/09/practical-guide-to-computer-simulations.html' title='A practical guide to computer simulations'/><author><name>I Wayan Sudiarta</name><uri>http://www.blogger.com/profile/10559430452013301600</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4222509099263598785.post-1330388190114767920</id><published>2008-04-11T10:27:00.000-07:00</published><updated>2008-04-11T10:47:44.881-07:00</updated><title type='text'>Computational-Physics Resources</title><content type='html'>A new article by Rubin H. Landau appeared in American Journal of Physics contains many resources for Computational Physics. I think you should read it!. It has many useful information for students and teachers to do physical simulations. Other articles in the same journal I also found them very interesting!.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://dx.doi.org/10.1119/1.2837814"&gt;Rubin H. Landau (2008): Resource Letter CP-2: Computational Physics, &lt;br /&gt;American Journal of Physics, Volume 76, Issue 4, pp. 296-306&lt;/a&gt;&lt;br /&gt;Abstract:&lt;br /&gt;This Resource Letter provides a guide to print and electronic literature relevant to a computational physics course. The multidisciplinary aspect of computational physics and its relation to computational science is reflected in the sections Courses, Programs and Reviews, Journals, Conferences and Organizations, Books, Tools, Languages and Environments, Parallel Computing, and Digital Libraries.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4222509099263598785-1330388190114767920?l=physics-simulation.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://physics-simulation.blogspot.com/feeds/1330388190114767920/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4222509099263598785&amp;postID=1330388190114767920' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4222509099263598785/posts/default/1330388190114767920'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4222509099263598785/posts/default/1330388190114767920'/><link rel='alternate' type='text/html' href='http://physics-simulation.blogspot.com/2008/04/computational-physics-resources.html' title='Computational-Physics Resources'/><author><name>I Wayan Sudiarta</name><uri>http://www.blogger.com/profile/10559430452013301600</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4222509099263598785.post-1457113526938229965</id><published>2008-02-17T19:18:00.000-08:00</published><updated>2008-02-17T19:25:04.739-08:00</updated><title type='text'>Handbook of Mathematical Functions</title><content type='html'>For simulations of many physical systems, one might encounter problems that require the use of special functions and mathematical formula. I found the book by Abramowitz and Stegun "Handbook of Mathematical Functions" is very useful. An electronic copy of the tenth printing of this book can be found in the following link:&lt;br /&gt;&lt;a href="http://www.math.sfu.ca/~cbm/aands/"&gt;http://www.math.sfu.ca/~cbm/aands/&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4222509099263598785-1457113526938229965?l=physics-simulation.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://physics-simulation.blogspot.com/feeds/1457113526938229965/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4222509099263598785&amp;postID=1457113526938229965' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4222509099263598785/posts/default/1457113526938229965'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4222509099263598785/posts/default/1457113526938229965'/><link rel='alternate' type='text/html' href='http://physics-simulation.blogspot.com/2008/02/handbook-of-mathematical-functions.html' title='Handbook of Mathematical Functions'/><author><name>I Wayan Sudiarta</name><uri>http://www.blogger.com/profile/10559430452013301600</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4222509099263598785.post-6532394318282970537</id><published>2008-01-02T00:20:00.000-08:00</published><updated>2008-01-02T00:35:10.079-08:00</updated><title type='text'>Solving Schrodinger Equation and Computing Density Matrix</title><content type='html'>I have written two papers about the finite difference time domain (FDTD) method for solving the Schroedinger equation and for computing the single particle density matrix. I have put these papers in the &lt;a href="http://www.arxiv.org"&gt;arXiv.org&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;[1]  &lt;a href="http://arxiv.org/abs/0712.4317"&gt;arXiv:0712.4317 &lt;/a&gt;&lt;br /&gt;Title: The Finite Difference Time Domain Method for Computing Single-Particle Density Matrix&lt;br /&gt;    Authors: I. Wayan Sudiarta, D. J. Wallace Geldart&lt;br /&gt;    Comments: 19 pages, 8 figures&lt;br /&gt;    &lt;br /&gt;[2]  &lt;a href="http://arxiv.org/abs/0712.4201"&gt;arXiv:0712.4201 &lt;/a&gt;&lt;br /&gt;    Title: Solving the Schrodinger Equation for a Charged Particle in a Magnetic Field using the Finite Difference Time Domain Method&lt;br /&gt;    Authors: I. Wayan Sudiarta, D. J. Wallace Geldart&lt;br /&gt;    Comments: 8 pages, 4 figures&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4222509099263598785-6532394318282970537?l=physics-simulation.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://physics-simulation.blogspot.com/feeds/6532394318282970537/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4222509099263598785&amp;postID=6532394318282970537' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4222509099263598785/posts/default/6532394318282970537'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4222509099263598785/posts/default/6532394318282970537'/><link rel='alternate' type='text/html' href='http://physics-simulation.blogspot.com/2008/01/solving-schrodinger-equation-and.html' title='Solving Schrodinger Equation and Computing Density Matrix'/><author><name>I Wayan Sudiarta</name><uri>http://www.blogger.com/profile/10559430452013301600</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4222509099263598785.post-5325832919832418041</id><published>2007-10-10T14:16:00.000-07:00</published><updated>2007-10-10T22:17:52.172-07:00</updated><title type='text'>Solving Schrodinger Equation - Linear Algebra with LAPACK</title><content type='html'>Today I solve the Time-independent Schrodinger equation (TISE) using the finite difference method that I explained in previous blog and the Matrix method. Being able to solve the TISE numerically is important since only small idealized system can be solved analytically. I consider here only one dimensional case for a particle in a potential V(x). The one dimensional TISE is&lt;br /&gt;&lt;br /&gt;-(1/2) [d^2 psi(x) /dx^2] + V(x) psi(x) = E psi(x)&lt;br /&gt;&lt;br /&gt;Note I use natural unit m = hbar = 1.&lt;br /&gt;&lt;br /&gt;The first step we need to do is to transform differential equation into matrix equation. To use finite difference, we evaluate the wavefunction, psi(x) i.e. the solution of the TISE, at N+1 grid points with spacing dx, {psi(i)}. Similarly the potential is evaluated at i*dx i.e. V(i). Note i=0, 1, ..., N. The computational length is L = N*dx.&lt;br /&gt;&lt;br /&gt;Using central finite difference, &lt;br /&gt;&lt;br /&gt;d^2 psi(x)/dx^2  = (psi(x+dx) - 2psi(x) + psi(x-dx))/dx^2&lt;br /&gt;= ( psi(i+1) - 2psi(i) + psi(i-1) )/dx^2&lt;br /&gt;&lt;br /&gt;This transforms the 1D TISE into&lt;br /&gt;-( psi(i+1) - 2psi(i) + psi(i-1) )/(2dx^2) + V(i)psi(i) = E psi(i)&lt;br /&gt;&lt;br /&gt;after simple manipulation, we have&lt;br /&gt; &lt;br /&gt;-psi(i+1) + (2 + (2dx^2)V(i))psi(i) - psi(i-1) = (2dx^2)E psi(i)&lt;br /&gt;&lt;br /&gt;For computational convenience we use the boundary conditions where psi(0)=psi(L)=0;&lt;br /&gt;&lt;br /&gt;The above discretized 1D TISE and using boundary conditions, in matrix form, is given by&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_s6azUsvu4GQ/Rw2xixDvbnI/AAAAAAAAAC0/ZDmEh5tR7LQ/s1600-h/schrodinger.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_s6azUsvu4GQ/Rw2xixDvbnI/AAAAAAAAAC0/ZDmEh5tR7LQ/s400/schrodinger.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5119943562232819314" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is an eigen matrix problem. To solve it I use &lt;a href="http://www.netlib.org/clapack/"&gt;LAPACK&lt;/a&gt; function dstev_() to compute eigenvalues for symmetric matrix. As an example I consider a finite square well potential given by V(x) = 1 for |x-5|&amp;gt;1 and V(x) = 0 for |x-5|&amp;lt;1 and grid spacing dx=0.1.   &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;--- eigen.c ---&lt;br /&gt;&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;#include &amp;lt;math.h&amp;gt;&lt;br /&gt;&lt;br /&gt;#define N 100&lt;br /&gt;&lt;br /&gt;int main(void)&lt;br /&gt;{&lt;br /&gt;   int i;&lt;br /&gt;   double dx = 0.1;&lt;br /&gt;   double dxsq = dx*dx;&lt;br /&gt;   double hdiag[(N-1)],hoff[(N-2)];  //hold diagonal and off-diagonal &lt;br /&gt;   double v[(N-1)];&lt;br /&gt;   char jobType = 'V';&lt;br /&gt;   long int n = (N-1);&lt;br /&gt;   long int info,ldz = N-1;&lt;br /&gt;   double work[2*N-4];&lt;br /&gt;   double z[(N-1)][(N-1)];&lt;br /&gt;&lt;br /&gt;   /* initialize V(x) */&lt;br /&gt;   for(i=0;i&lt;(N-1);i++){&lt;br /&gt;      if(fabs((i+1)*dx - 5)&lt;1){ v[i]= 0;}&lt;br /&gt;      else v[i] = 1;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   /* initialize hoff */&lt;br /&gt;   for(i=0;i&lt;(N-2);i++){&lt;br /&gt;     hoff[i] = -1;&lt;br /&gt;   }&lt;br /&gt;   /* initialize hdiag */&lt;br /&gt;   for(i=0;i&lt;(N-1);i++){&lt;br /&gt;     hdiag[i] = 2.0 + 2.0*dxsq*v[i];&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   dstev_(&amp;jobType,&amp;n,hdiag,hoff,z,&amp;ldz,work,&amp;info);&lt;br /&gt;   &lt;br /&gt;   /* Print results for four eigen states */&lt;br /&gt;   printf("info = %ld\n",info);&lt;br /&gt;   printf("The Lowest Four Eigen Values \n");&lt;br /&gt;   for(i=0;i&lt;4;i++){&lt;br /&gt;      printf("%ld  %lf\n",i,hdiag[i]/(2.0*dxsq));&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   printf("The Lowest four Eigen Vectors\n");&lt;br /&gt;   printf("x   groundstate  1st   2nd   3rd \n");&lt;br /&gt;   for(i=0;i&lt;(N-1);i++){&lt;br /&gt;      printf("%lf  %lf  %lf  %lf  %lf\n",i*dx,z[0][i],z[1][i],z[2][i],z[3][i]);&lt;br /&gt;   }&lt;br /&gt;      &lt;br /&gt;   return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;To compile:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;gcc eigen.c -lm -llapack -o try&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4222509099263598785-5325832919832418041?l=physics-simulation.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://physics-simulation.blogspot.com/feeds/5325832919832418041/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4222509099263598785&amp;postID=5325832919832418041' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4222509099263598785/posts/default/5325832919832418041'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4222509099263598785/posts/default/5325832919832418041'/><link rel='alternate' type='text/html' href='http://physics-simulation.blogspot.com/2007/10/solving-schrodinger-equation-linear.html' title='Solving Schrodinger Equation - Linear Algebra with LAPACK'/><author><name>I Wayan Sudiarta</name><uri>http://www.blogger.com/profile/10559430452013301600</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_s6azUsvu4GQ/Rw2xixDvbnI/AAAAAAAAAC0/ZDmEh5tR7LQ/s72-c/schrodinger.jpg' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4222509099263598785.post-2740479366685693590</id><published>2007-10-08T20:11:00.000-07:00</published><updated>2007-10-08T21:50:10.143-07:00</updated><title type='text'>Discrete Fourier Transform using FFTW</title><content type='html'>If we have a time series data and we want to know the frequency content of it, then we need to perform Fourier transformation. This can be done numerically using available mathematical softwares such as Matlab, Mathematica and Maple. For C programming language, I mostly use FFTW since it is free and easy to use on linux OS and in Cygwin on Windows. &lt;br /&gt;&lt;br /&gt;To understand how to do DFT, let us a look at Fourier transform of a square function given by f(t) = 1 for |t-2|&lt;=1 and f(t) = 0 for |t-2|&gt;1. We consider the function at interval (0,4). First discretize the data into grids with spacing 0.1.  &lt;br /&gt;&lt;br /&gt;--- try_fftw.c ---&lt;br /&gt;&lt;br /&gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;#include &amp;lt;math.h&amp;gt;&lt;br /&gt;#include &amp;lt;fftw3.h&amp;gt;&lt;br /&gt;&lt;br /&gt;#define N 40&lt;br /&gt;&lt;br /&gt;int main(void)&lt;br /&gt;{&lt;br /&gt;   int i;&lt;br /&gt;   double dt, t;&lt;br /&gt;   fftw_complex *f, *g;&lt;br /&gt;   fftw_plan p;&lt;br /&gt;&lt;br /&gt;   dt = 0.1;&lt;br /&gt;   /* allocate memory for f and g */&lt;br /&gt;   f = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);&lt;br /&gt;   g = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);&lt;br /&gt;&lt;br /&gt;   /* compute square function f(x) */&lt;br /&gt;   for(i=0;i&amp;lt;N;i++){&lt;br /&gt;      t = i*dt;&lt;br /&gt;      if(fabs(t-2)&lt;=1){&lt;br /&gt;         f[i][0] = 1;&lt;br /&gt;         f[i][1] = 0;&lt;br /&gt;      }  &lt;br /&gt;      else{&lt;br /&gt;         f[i][0] = 0;&lt;br /&gt;         f[i][1] = 0;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   /* Print f(t) */&lt;br /&gt;   printf(" -- i  f(t) -- \n");    &lt;br /&gt;   for(i=0;i&amp;lt;N;i++){&lt;br /&gt;      printf("%d  %e \n", i, f[i][0]);    &lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   /* create plan */&lt;br /&gt;   p = fftw_plan_dft_1d(N, f, g, FFTW_FORWARD, FFTW_ESTIMATE);&lt;br /&gt;&lt;br /&gt;   /* perform FFT */&lt;br /&gt;   fftw_execute(p); &lt;br /&gt;&lt;br /&gt;   /* Print results */&lt;br /&gt;   printf(" --  i  g(w) -- \n");    &lt;br /&gt;   for(i=0;i&amp;lt;N;i++){&lt;br /&gt;      printf("%d  %e  %e \n", i, g[i][0], g[i][1]);    &lt;br /&gt;   }&lt;br /&gt; &lt;br /&gt;   /* Free memory */ &lt;br /&gt;   fftw_destroy_plan(p);&lt;br /&gt;   fftw_free(f); &lt;br /&gt;   fftw_free(g);&lt;br /&gt;&lt;br /&gt;   return 0;&lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;-------&lt;br /&gt;&lt;br /&gt;To compile use:&lt;br /&gt;&lt;pre&gt; gcc try_fftw.c  -lm -lfftw3 -o try  &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Results are shown in the figures below&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_s6azUsvu4GQ/RwsGFBDvbgI/AAAAAAAAAB8/u8QHXX6crAA/s1600-h/ft.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://1.bp.blogspot.com/_s6azUsvu4GQ/RwsGFBDvbgI/AAAAAAAAAB8/u8QHXX6crAA/s320/ft.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5119192084689939970" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_s6azUsvu4GQ/RwsGRRDvbhI/AAAAAAAAACE/5P8fQc0dCWI/s1600-h/gw.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://2.bp.blogspot.com/_s6azUsvu4GQ/RwsGRRDvbhI/AAAAAAAAACE/5P8fQc0dCWI/s320/gw.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5119192295143337490" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Ref: FFTW3 manual&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4222509099263598785-2740479366685693590?l=physics-simulation.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://physics-simulation.blogspot.com/feeds/2740479366685693590/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4222509099263598785&amp;postID=2740479366685693590' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4222509099263598785/posts/default/2740479366685693590'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4222509099263598785/posts/default/2740479366685693590'/><link rel='alternate' type='text/html' href='http://physics-simulation.blogspot.com/2007/10/fast-fourier-transform-using-fftw.html' title='Discrete Fourier Transform using FFTW'/><author><name>I Wayan Sudiarta</name><uri>http://www.blogger.com/profile/10559430452013301600</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_s6azUsvu4GQ/RwsGFBDvbgI/AAAAAAAAAB8/u8QHXX6crAA/s72-c/ft.jpg' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4222509099263598785.post-5361587769001820263</id><published>2007-10-07T08:13:00.000-07:00</published><updated>2007-10-08T21:47:03.011-07:00</updated><title type='text'>Numerical Differentiation Formulas</title><content type='html'>In Physics, to simulate physical system, we usually encounter ordinary or partial differential equations. Let us consider a function or a field f(x) which satisfies a specified differential equation. To solve the differential equation, we first approximate the function f(x). The approximation can be in a form of Fourier series, a polynomial and a discrete functions. Today I will only consider the discrete function. The function f(x) is approximated in an interval (a,b) by (N+1) discrete values given by {f(i)} where f(x) = f(i*h) , h = (b-a)/N, i=0,1,2 ... N. Then we need to approximate derivatives in the differential equation.&lt;br /&gt;&lt;br /&gt;The numerical differentiations that I often used are central finite difference schemes:&lt;br /&gt;&lt;br /&gt;1)  d f(x)/dx =  [f(x+h)-f(x-h)]/2h + O(h^2)&lt;br /&gt;              =  [f(i+1)-f(i-1)]/2h      (second order accurate)&lt;br /&gt;&lt;br /&gt;2)  d^2 f(x)/dx^2 = [f(x+h)-2f(x) + f(x-h)]/h^2 + O(h^2)&lt;br /&gt;                  = [f(i+1)-2f(i)+f(i-1)]/h^2    (second order accurate)&lt;br /&gt;&lt;br /&gt;    d^2 f(x)/dx^2 = [-f(x+2h)+16f(x+h)-30f(x) + 16f(x-h)-f(x-2h)]/(12h^2) + O(h^4)&lt;br /&gt;                  = [-f(i+2)+16f(i+1)-30f(i)+16f(i-1)-f(i-2)]/(12h^2)     &lt;br /&gt;                    (fourth order accurate)&lt;br /&gt;&lt;br /&gt;3)  d^3 f(x)/dx^3 = [f(x+2h)-2f(x+h) +2 f(x-h)-f(x-2h)]/(2h^3) + O(h^2)&lt;br /&gt;                  = [f(i+2)-2f(i+1)+2f(i-1)-f(i-2)]/(2h^3)  (second order accurate)&lt;br /&gt;&lt;br /&gt;As an example, Let us compute derivatives for trigonometric function sin(Pi x) in the interval (0,1). In C programming language,  can be written as&lt;br /&gt;&lt;br /&gt;--- num_diff.c ---&lt;br /&gt;&lt;br /&gt;/*   Numerical Differentiation &lt;br /&gt; *   by I Wayan Sudiarta Oct 2007&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;#include&amp;lt;math.h&amp;gt;&lt;br /&gt;#include&amp;lt;stdio.h&amp;gt;&lt;br /&gt;&lt;br /&gt;#define N 20&lt;br /&gt;#define PI 3.141592653589793  &lt;br /&gt;&lt;br /&gt;int main(void)&lt;br /&gt;{&lt;br /&gt;    int i;&lt;br /&gt;    double h, hsq, hcb;&lt;br /&gt;    double f[N+1],  f1[N+1], f2[N+1], f2b[N+1], f3[N+1];&lt;br /&gt;  &lt;br /&gt;    h  =  1.0/((double)(N));&lt;br /&gt;&lt;br /&gt;    hsq = h*h;&lt;br /&gt;    hcb = h*hsq;&lt;br /&gt;&lt;br /&gt;    /* Compute f[i] */&lt;br /&gt;    for(i=0;i&amp;lt;=N;i++){&lt;br /&gt;       f[i] = sin(i*PI*h);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /* Compute derivatives */&lt;br /&gt;    for(i=2;i&amp;lt;(N-1);i++){&lt;br /&gt;       f1[i] = (f[i+1]-f[i-1])/(2*h);&lt;br /&gt;       f2[i] = (f[i+1]-2*f[i]+f[i-1])/(hsq);&lt;br /&gt;       f2b[i]= (-f[i+2]+16*f[i+1]-30*f[i]+16*f[i-1]-f[i-2])/(12*hsq);&lt;br /&gt;       f3[i] = (f[i+2]-2*f[i+1]+2*f[i-1]-f[i-2])/(2*hcb);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    /* Print results */&lt;br /&gt;    printf("x   f1(x)  f2(x)  f2b(x)  f3(x) \n");&lt;br /&gt;    for(i=2;i&amp;lt;(N-1);i++){&lt;br /&gt;       printf("%e   %e   %e  %e   %e \n", i*h, f1[i], f2[i], f2b[i], f3[i]);  &lt;br /&gt;    }&lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Ref: CRC Standard Mathematical Tables, 27th Edition, 1974&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4222509099263598785-5361587769001820263?l=physics-simulation.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://physics-simulation.blogspot.com/feeds/5361587769001820263/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4222509099263598785&amp;postID=5361587769001820263' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4222509099263598785/posts/default/5361587769001820263'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4222509099263598785/posts/default/5361587769001820263'/><link rel='alternate' type='text/html' href='http://physics-simulation.blogspot.com/2007/10/numerical-differentiation-formulas.html' title='Numerical Differentiation Formulas'/><author><name>I Wayan Sudiarta</name><uri>http://www.blogger.com/profile/10559430452013301600</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4222509099263598785.post-6124197559777856812</id><published>2007-10-06T09:39:00.000-07:00</published><updated>2007-10-06T09:55:53.615-07:00</updated><title type='text'>Welcome....The beginning</title><content type='html'>Welcome... to my physics simulation blog&lt;br /&gt;&lt;br /&gt;The purpose of this blog is to collect all things that I found interesting which are related to physics simulations. I do physics simulations almost everyday. While working, reading books and searching the internet I often discover new interesting stuff for physics simulations. One might say that this blog is a tutorial of physics simulation.&lt;br /&gt;&lt;br /&gt;Currently I am doing simulation of few electrons system at thermal equilibrium.  Interesting stuff!&lt;br /&gt;&lt;br /&gt;I hope this blog can be useful for people like me who are learning and working using simulations.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4222509099263598785-6124197559777856812?l=physics-simulation.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://physics-simulation.blogspot.com/feeds/6124197559777856812/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=4222509099263598785&amp;postID=6124197559777856812' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4222509099263598785/posts/default/6124197559777856812'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4222509099263598785/posts/default/6124197559777856812'/><link rel='alternate' type='text/html' href='http://physics-simulation.blogspot.com/2007/10/welcomethe-beginning.html' title='Welcome....The beginning'/><author><name>I Wayan Sudiarta</name><uri>http://www.blogger.com/profile/10559430452013301600</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry></feed>
