C Program For Numerical Methods Unit 1
C Program For Numerical Methods Unit 1
1. Start
2. Read x0, x1, err
*Here x1 and x2 are initial guesses
e is the absolute error i.e. the desired degree of accuracy*
3. Compute: f1 = f(x0) and f2 = f(x1)
4. If (f0*f1) > 0, then display initial guesses are wrong and goto (11).
Otherwise continue.
5. X2 = (x0 + x1)/2
6. If ( [ (x1 – x2) ] > e ), then display x2 and goto (11).
* Here [ ] refers to the modulus sign. *
7. Else, f2 = f(x2)
8. If ((f2*f0) > 0), then x0 = x2 and f0 = f2.
9. Else, x1 = x2 and f1 = f2.
Below is a source code in C program for bisection method to find a root of the nonlinear
function x^3 – 4*x – 9. The initial guesses taken are a and b. The calculation is done until the
following condition is satisfied:
|a-b| < 0.0005 OR If (a+b)/2 < 0.0005 (or both equal to zero)
where, (a+b)/2 is the middle point value.
Variables:
20. f0=f(x0);
21. f1=f(x1);
22. x2=(x0+x1)/2;
23. f2=f(x2); /*assigning of values*/
24. printf("\n%d\t%f %f %f %lf %lf %lf",i,x0,x1,x2,f0,f1,f2);
/*values of the process*/
25. if(f0*f2<0) /*the main process*/
26. x1=x2;
27. else
28. x0=x2;
29. i++;
30. }while(fabs(f2)>err); /*check if the error term is met*/
31. printf("\n--------------------------------------\n");
32. printf("Approximate Root: %f",x2); /*final answer*/
33. getch();
34. }
Input/Output:
Regula Falsi method, also known as the false position method, is the oldest approach to find the
real root of a function. It is a closed bracket method and closely resembles the bisection method.
The C Program for regula falsi method requires two initial guesses of opposite nature. Like
the secant method, interpolation is done to find the new values for successive iterations, but in
this method one interval always remains constant.
The programming effort for Regula Falsi or False Position Method in C language is simple and
easy. The convergence is of first order and it is guaranteed. In manual approach, the method of
false position may be slow, but it is found superior to the bisection method.
Below is a short and simple source code in C program for regula falsi method to find the root
of cos(x) – x*e^x. Here, x0 and x1 are the initial guesses taken.
Variables:
f(x) = x^3-2*x-5
Input/Output:
Newton Raphson method, also called the Newton’s method, is the fastest and simplest approach
of all methods to find the real root of a nonlinear function. It is an open bracket approach,
requiring only one initial guess. This method is quite often used to improve the results obtained
from other iterative approaches.
The convergence is fastest of all the root-finding methods we have discussed in Code with C. The
algorithm and flowchart for Newton Raphson method given below is suitable for not only find the
roots of a nonlinear equation, but the roots of algebraic and transcendental equations as well.
The overall approach of Newton’s method is more useful in case of large values the first derivative
of f(X) i.e f'(X). The iterative formula for Newton Raphson method is:
Finding the f'(x) i.e. the first derivative of f(x) can be difficult if f(x) is complicated.
When f'(xn) tends to zero i.e. the first derivative of f(xn) tends to zero, Newton-Raphson
method gives no solution.
Near local maxima or local minima, there is infinite oscillation resulting in slow
convergence.
In Newton’s method C program, if the initial guess is far from the desired root, then the
method may converge to some other roots.
If root jumping occurs, the intended solution is not obtained.
Newton Raphson Method Algorithm:
Below is a very short and simple source code in C program for Newton’s method to find the root
of x^2+2*x – 2
.Variables:
itr – a counter which keeps track of the no. of iterations performed
z=df(x) – the derivative of f(x) with respect to x
x0 – the value of root at nth iteration
xn – the value of root at (n+1)th iteration
err – allowed error
f(x) = x^2+2*x – 2
1.
2. #include<stdio.h>
3. #include<conio.h>
4. #include<math.h> /*header files included*/
5. float f(float x) /*declaring the function f(x)*/
6. {
7. float y;
8. y=x*x+2*x-2;
9. return y;
10. }
11. float f1(float x) /*declaring the function f'(x)*/
12. {
13. float z;
14. z=2*x+2;
15. return z;
16. }
17. void main()
18. {
19. int n=0;
20. float x0,xn,err=1,h;
21. printf("\nEnter the initial value of x = "); /*asking for the
intial value of x*/
22. scanf("%f",&x0);
23. printf("\n n\t xn \t\t f(xn)\t\t f'(xn)\t\tx(n+1)\n");
/*designing the table*/
24. while(fabs(err)>.000001)
25. {
26. h=-(f(x0)/f1(x0)); /*working formula*/
27. xn=x0+h;
28. err=xn-x0;
29. printf("\n %d\t %.10f\t %.10f\t %.10f\t
%.10f\n",n,x0,f(x0),f1(x0),xn); /*formatting values in output*/
30. x0=xn;
31. n++;
32. }
33. printf("\n One real root of the given equation is %.5f (correct
upto 5 decimal places)",xn); /*printing the final answer*/
34. getch();
35. }
Input/Output:
Below is a short and simple C programming source code for Secant method to find the root
of x^3-8x-5
f(x) = x^3-8x-5
1. #include<stdio.h>
2. float f(float x)
3. {
4. return(x*x*x-8*x-5); // f(x)= x^3-8x-5
5. }
6. float main()
7. {
8. float x0,x1,x2,err;
9. int count=1,itr;
10. printf("\n\nEnter the values of x0 and x1:\n"); //(a,b) must
contain the solution.
11. scanf("%f%f",&x0,&x1);
12. printf("Enter the values of allowed error and maximun number
of iterations:\n");
13. scanf("%f %d",&err,&itr);
14. do
15. {
16. if(f(x0)==f(x1))
17. {
18. printf("\nSolution cannot be found as the values of a and b
are same.\n");
19. return;
20. }
21. x2=(x0*f(x1)-x1*f(x0))/(f(x1)-f(x0));
22. x0=x1;
23. x1=x2;
24. printf("Iteration No-%d x=%f\n",count,x2);
25. count++;
26. if(count==itr)
27. {
28. break;
29. }
30. } while(fabs(f(x2))>err);
31. printf("\n The required solution is %.4f\n",x2);
32. }
Input/Output:
When the values for a and b, the initial guesses, are entered same, the solution can’t be obtained.
In such case, enter different values for a and b such that f(a) and f(b) are not equal.
Fixed point iteration method is commonly known as the iteration method. It is one of the most
common methods used to find the real roots of a function. The C program for fixed point iteration
method is more particularly useful for locating the real roots of an equation given in the form of
an infinite series. So, this method can be used for finding the solution of arithmetic series,
geometric series, Taylor’s series and other forms of infinite series.
This method is linearly convergent with somewhat slower rate of convergence, similar to
the bisection method. It is based on modification approach to find the fixed point. It is commonly
referred to as simple enclosure method or open bracket method.
Like other methods to find the root of a function, the programming effort for Iteration Method in
C is easy, short and simple. Just like the Newton-Raphson method, it requires only one initial guess,
and the equation is solved by the assumed approximation.
Iterations and modifications are successively continued with the updated approximations of the
guess. Iterative method gives good accuracy overall just like the other methods.
Below is a source code in C program for iteration method to find the root of e-x-x. The desired
degree of accuracy in the program can be achieved by continuing the iteration i.e. by increasing the
maximum number of iterations.
f(x) = e-x-x
1. #include<conio.h>
2. #include<stdio.h>
3. #include<math.h>
4. #define g(x) (exp(-x))
Input/Output:
y = c1 x2 + c2 x + c3
can be rearranged as
y = (c1 x + c2) x + c3
y = c1 x3 + c2 x2 + c3 x + c4
y = (c1 x2 + c2x + c3) x + c4
This pattern is called Horner's rule for evaluating a polynomial. For hand calculation of low degree,
it makes sense to use direct computation of the polynomial in its standard form. To evaluate a
polynomial in a computer program, Horner's rule makes more sense, especially if speed and
accuracy are important and the degree of the polynomial is large.
Below is a source code in C program for Horner’s method to evakluate the polynomial,
1. #include <stdio.h>
2. int main()
3. {
4. float a[100],sum=0,x;
5. int n,i;
6. printf("\nEnter degree of the polynomial X :: ");
7. scanf("%d",&n);
8. printf("\nEnter coefficient's of the polynomial X :: \n");
9. for(i=n;i>=0;i--)
10. {
11. printf("\nEnter Coefficient of [ X^%d ] :: ",i);
12. scanf("%f",&a[i]);
13. }
14. printf("\nEnter the value of X :: ");
15. scanf("%f",&x);
16. for(i=n;i>0;i--)
17. {
18. sum=(sum+a[i])*x;
19. }
20. sum=sum+a[0];
21. printf("\nValue of the polynomial is = [ %f ]\n",sum);
22. return 0;
23. }
Input/Output:
THE END
COMPILED BY: ER.SARBESH CHAUDHARY 21