CS 71 (2007)
CS 71 (2007)
CS 71 (2007)
Algorithm:
To determine a root of f(x) = 0 that is accurate within a specified tollerence value, given values x1 and x2,
such that f(x1) * f(x2) < 0,
Repeat: Set x3 = (x1 + x2) / 2
If f(x3) * f(x1) < 0 Then
Set x2 = x3
Else Set x1 = x3 End If
Until (x1 – x2) < 2 * tolerance value
Program
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <iostream.h>
int fn[20];
int n;
double a,b,x0;
double fnval;
float abso(float x)
{
if (x<0) return -x;else return x;
}
double power(double a,int b)
{
int i;
double c=1;
for (i=0;i<b;i++) c=c*a;
return c;
}
void getfunction()
{
int i;
printf("Enter the degree of the function: ");
1
cin >> n;
printf("\nStart entering the coefficients\n\n");
for (i=n;i>=0;i--)
{
printf("Enter coefiicient of degree %d: ",i);
cin >> fn[i];
}
}
void displayfunction()
{
int i;
printf("\nCurrent function is:\n ");
for (i=n;i>-1;i--)
{
if (i==0) printf("%d",fn[i]);
else printf("%dx^%d + ",fn[i],i);
}
}
float getaccuracy()
{
float no;
printf("\nEnter the accuracy required: ");
cin >> no;
return no;
}
double fx(double x)
{
int i;
double ans=0;
for (i=n;i>-1;i--)
{
ans=ans+fn[i]*power(x,i);
}
return ans;
}
void getinitialvals()
{
float curr=0,prev=0;
static float left=-100;
for (int i=left;i<=100;i++)
{
curr=fx(i);
a=i-1;b=i;
if (curr==0)
{
left=i+1;
printf("\nroot is %f",b);
getinitialvals();
}
2
if (i==99)
{
printf("\n\nThe equation has no further real roots");
exit(1);
}
if (curr*prev<0)
{
left=i;
break;
}
prev=curr;
}
}
void checkinivals()
{
if(fx(a)>fx(b))
{
double temp=a;
a=b;
b=temp;
}
}
int main()
{
int i=0,itr=0;
int j=1;
clrscr();
getfunction();
float accu=getaccuracy();
displayfunction();
do
{
i=0;
getinitialvals();
checkinivals();
cout << "\n\nInitial values are "<< a<< " and " << b << "\n";
do
{
x0=(a+b)/2;
fnval=fx(x0);
printf("%d%15f%15f%15f%15f%15f\n",i+1,a,b,x0,fnval,b-a);
if (fnval<0) a=x0; else b=x0;
i++;
} while (abso(b-a)>accu);
3
} while(1);
}
Answer 1b) Write a program in C to find a root using Newton Raphson Method.
Documentation
One of the most widely used methods of solving equations is Newton's method
(Newton did not publish an extensive discussion of this method, but he solved a cubic polynomial in
Principia (l687). The version given here is considerably improved over his original example).
Like the previous ones, this method is also based on a linear approximation of the
function, but does so using a tangent to the curve.
Starting from a single initial estimate, , that is not too far from a root, we move
along the tangent to its intersection with the x-axis, and take that as the next approximation.
This is continued until either the successive x-values are sufficiently close or the
value of the function is sufficiently near zero.
The calculation scheme follows:
Newton's algorithm is widely used because, it is more rapidly convergent than any of the methods
discussed so far.
The method is quadratically convergent, by which we mean that the error of each step approaches a
constant K times the square of the error of the previous step.
The net result of this is, that the number of decimal places of accuracy nearly doubles at each iteration.
After three iterations, the root is correct to seven digits; convergence is much more rapid than any
previous method. In fact, the error after an iteration is about one-third of the square of the previous error.
4
Algorithm:
The method may converge to a root different from the expected one or diverge if the starting value is not
close enough to the root.
Program
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <iostream.h>
int fn[10];
int fdashn[10];
int n,i;
double x0;
double fnval,fdashnval;
double power(double a,int b)
{
int i;
double c=1;
for (i=0;i<b;i++) c=c*a;
return c;
}
void getfunction()
{
int i;
printf("Enter the degree of the function: ");
cin >> n;
printf("\nStart entering the coefficients starting from the constant term\n\n");
for (i=0;i<n+1;i++)
{
printf("Enter coefiicient of degree %d: ",i);
cin >> fn[i];
}
}
void displayfunction()
{
int i;
printf("\nCurrent function is:\n ");
5
for (i=n;i>-1;i--)
{
if (i==0) printf("%d",fn[i]);
else if (fn[i]==0) printf("");
else printf("%dx^%d + ",fn[i],i);
}
}
int getiterations()
{
int no;
printf("\nEnter The nember of iteratins: ");
cin >> no;
return no;
}
double fx(double x)
{
int i;
double ans=0;
for (i=n;i>-1;i--)
{
ans=ans+fn[i]*power(x,i);
}
return ans;
}
void getderivative()
{
int i;
for (i=1;i<n+1;i++)
{
fdashn[i-1]=fn[i]*i;
}
}
void displayderivative()
{
int i;
printf("\nCurrent function derivative is:\n ");
for (i=n-1;i>-1;i--)
{
if (i==0) printf("%d",fdashn[i]);
else if (fdashn[i]==0) printf("");
else printf("%dx^%d + ",fdashn[i],i);
}
}
double fdashx(double x)
{
int i;
double ans=0;
for (i=n-1;i>-1;i--)
{
6
ans=ans+fdashn[i]*power(x,i);
}
return ans;
}
void getinitialval()
{
printf("\n\nEnter the initial value: ");
cin >> x0;
}
int main()
{
int i=0,itr=0;
clrscr();
getfunction();
itr=getiterations();
displayfunction();
getderivative();
displayderivative();
getinitialval();
do
{
fnval=fx(x0);
fdashnval=fdashx(x0);
x0=x0-fnval/fdashnval;
i++;
} while (i<itr);
cout << "\n\nThe root of the given equation is " << x0;
return 0;
}
Answer 1c) Write a program in C evaluating the polynomial using Lagrange interpolation.
Documentation
Straightforward approach-the Lagrangian polynomial, the simplest way to exhibit the existence of a
polynomial for interpolation with unevenly spaced data.
Suppose we have a table of data with four pairs of - and -values, with indexed by variable :
7
Here we do not assume uniform spacing between the x-values, nor do we need the -values arranged in a
particular order. The x-values must all be distinct, however.
Through these four data pairs we can pass a cubic. The Lagrangian form for this is
This equation is made up of four terms, each of which is a cubic in ; hence the sum is a cubic.
The pattern of each term is to form the numerator as a product of linear factors of the form ,
omitting one in each term, the omitted value being used to form the denominator by replacing in
each of the numerator factors.
Algorithm:
Program:
#include <stdio.h>
#include <conio.h>
float value[10][10],x;
int n;
void getvalue()
{
int i,j;
printf("Enter the number of terms:");
scanf("%d",&n);
printf("Enter the x and y values simultaneously :");
for (i=0;i<n;i++)
for (j=0;j<=1;j++)
scanf("%f",&value[i][j]);
printf("Enter the value of x to interpolate :");
8
scanf("%f",&x);
}
void getans()
{
int i,k;
float prod1,prod2,sum=0;
for (k=0;k<=n;k++)
{
prod1=1;
prod2=1;
for(i=0;i<=n;i++)
{
if (i==k) continue;
prod1*=(x-value[i][0]);
prod2*=(value[k][0]-value[i][0]);
}
sum+=prod1/prod2*value[k][1];
}
printf("The coressponding value of y is :");
printf("%.4f",sum);
}
void main()
{
clrscr();
getvalue();
getans();
getch();
}
Answer 1d) Write a program in C that determines the solution of differential equation by using
Runge Kutta method.
Algorithm
1. read x,y,h,M
2. print x,y
3. N=0
4. repeat
5. k1 = h * f(x,y)
6. x = x + h/2
7. z = y + k1/2
8. k2 = h * f(x,z)
9. z = y + k2/2
10. k3 = h * f(x,z)
11. x = x + h/2
12. z = y + k3
13. k4 = h * f(x,z)
9
14. y = y + (k1 + 2k2 + 2k3 + k4)/6
15. print x,y
16. N=N+1
17. until N = M
Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
int n,choice, i;
float x0, y0, h, y1, x1, x0, z0,sum, temp, k1,k2,k3,k4;
float l1,l2,l3,l4;
float f ( float, float, float);
float g(float p, float q, float r);
void main()
{
printf("Enter starting value of x and corr. val of y and z.\n");
scanf("%f%f%f", &x0, &y0, &z0);
printf("Enter value of x for which y is found \n");
scanf("%f", &x1);
h = x1-x0;
k1 = h * f ( x0, y0, z0);
l1 = h * g ( x0, y0, z0);
k2 = h * f(x0 + h/2, y0 + k1/2, z0+ l1/2);
l2 = h * g(x0 + h/2, y0 + k1/2, z0+ l1/2);
k3 = h * f(x0 + h/2, y0 + k2/2, z0+l2/2);
l3 = h * g(x0 + h/2, y0 + k2/2, z0+l2/2);
k4 = h * f (x0+h , y0+k3, z0+ l3);
l4 = h * g (x0+h , y0+k3, z0+l3);
y1 = y0 + (k1+2*k2+2*k3+k4)/6;
printf("y = %f", y1);
getch();
}
float f(float p, float q, float r)
{
return(r);
}
float g(float p, float q, float r)
{
return(p*r*r - q*q); }
10