Computational Physics Lab Test 2011: 1 The Problem
Computational Physics Lab Test 2011: 1 The Problem
The Problem
Problem No 3: It is desired to calculate all integral powers of the number x = ( (5.0) 1.0)/2.0 It turns out that the integral powers of x satisfy a simple recursive relation x(n+1) = x(n1) xn Show that the above recurrence relation is unstable by calculating x16 , x30 , x40 and x50 from the recurrence relation and comparing with the actual values obtained by using inbuilt function e.g., pow(a,b) in C.
2
2.1
Method
The program
#include<stdio.h> #include<math.h> main() { double x=(sqrt(5.0)-1.0)/2; double p,y1,y; int i,n; float f(int n); printf("\nThe value of x is : %e",x); printf("\nEnter the value of n :: "); scanf("%d",&n);
//Actual Value Evaluated using pow() y1=pow(x,n); y=f(n); printf("\n\n\n\tRecursive value is %e Pow value is %e",y,y1); printf("\n\n\t\tDifference is :: %e \n",y-y1); } //Recursive Value float f(int n) { double x=(sqrt(5.0)-1.0)/2; if(n>=2) return(f(n-2)-f(n-1)); if(n==1) return(x); if(n==0) return(1); }
2.2
***************************************** [sayan@Sayan C_Programs]$ ./prob3 The value of x is : 6.180340e-01 Enter the value of n :: 40
Difference is :: -1.679836e+00
Results
Here we can see higher the value of n,the greater is the dierence between the actual value and the recursive value.The recursion relation holds good only for lower values of n.
The method of iteration which we tried at rst for evaluating the recurrence failed,because at the time of iteration we were actually performing the multiplication etc. leading to the null dierence between the concerned results. The report has been prepared using TEX.