Bisection Method
Bisection method is the root finding method that repeatedly bisects an interval and then
selects a subinterval in which root must lie for further processing.
Let the function f(x) be continuous between a and b. For definiteness, let f(a) be negative and
1
f(b) be positive. Then the first approximation to the root is x1= (𝑎 + 𝑏). If f(x1)=0, then x1 is
2
a root of f(x)=0. Otherwise, the root lies between a and x 1 or x1 and b according as f(x1) is
positive or negative. Then we bisect the interval as before and continue the process until the
root is found to desired accuracy.
Algorithm
1) Start
2) Choose x0 and x1 as initial guess such that f(x0).f(x1)<0
x0+x1
3) x2=
2
4) Find f(x0).f(x2)
a) If f(x0).f(x2)<0, then x0=x0, x1=x2
b) If f(x0).f(x2)>0, then x0=x2, x1=x1
c) If f(x1).f(x2)=0, then root=x2
5) If f(x2)<e go to step 6
Else go to step 3
6) Display x2 as root
7) Stop
Flowchart
Start
Input x0 and x1
No f(x0)*f(x1)<0 ?
Yes
x2=
No
f(x0).f(x2)<0 ?
Yes
x0=x2, x1=x1 x0=x0, x1=x2
|f(x2)|<e? No
Yes
Root = x2
Stop
//Program for Bisection Method
#include<stdio.h>
#include<math.h>
#define e 0.05
#define f(x) 3*x*x-6*x+2
Int main()
{
float x0,x1,x2,error;
float f0,f1,f2;
int i=1;
printf("Enter the value of x0 & x1:");
scanf("%f %f", &x0,&x1);
f0=f(x0);
f1=f(x1);
do{
f0=f(x0);
f1=f(x1);
x2=((x1+x0)/2);
f2=f(x2);
if(f0*f2<0)
{
x1=x2;
}
else
{
x0=x2;
}
i++;
error= (x1-x0)/x1;
}while(fabs(error)>e);
printf("\nRoot is %f",x2);
return 0;
}
Secant Method (Chord Method)
Secant method is an improvement over the method of false position as it does not require
the condition f(x0)*f(x1)<0 of that method. Here, also the graph of the function f(x) is
approximated by a secant line but at each iteration, two most recent approximations to the
root are used to find the next approximation. Also, it is not necessary that the interval must
contain the root.
Algorithm
1. Start
2. Take two initial point x0 and x1 and stopping criteria
3. Compute f(x0) and f(x1) and check if f(x0).f(x1)<0
x1–x0
4. Compute x2=𝑥1 − ∗ 𝑓(𝑥1)
f(x1)–f(x0)
5. Test for accuracy
x2–x1
If| | > 𝐸, then x0=x1 and x1=x2 and go to step 4
x2
Else go to step 6
6. Display x2 as root
7. Stop
Flowchart
Start
Input interval
[x0, x1]
No f(x0)*f(x1)<0 ?
Yes
x2=𝑥1 − ∗ 𝑓(𝑥1)
No 𝑥2 − 𝑥1
| | > 𝐸?
𝑥2
Yes
x0=x1
x1=x2
Root = x2
Stop
//Program for secant method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define e 0.05
#define f(x) x*x*x-x-5
int main ()
{
float x0, x1, x2, error=0;
float f0, f1, f2;
printf("Enter x0 and x1: ");
scanf("%f %f",&x0,&x1);
do{
f0=f(x0);
f1=f(x1);
x2=x1-(x1-x0)/(f1-f0)*f1;
f2=f(x2);
error=fabs((x2-x1)/x2);
x0=x1;
x1=x2;
} while(error>e);
printf("\n root is %f",x2);
return 0;
}
Newton-Raphson Method
Newton-Raphson method is a method for finding successively better approximations to the roots (or
zeroes) of a real-valued function.
Algorithm
1. Start
2. Define a function f(x)
3. Define a function df(x)
4. Initialize value of x0 and stopping citeria
5. Compute f0=f(x0) and df0=df(x0)
f(x0)
6. 𝑥1 = 𝑥0 − df(x0)
x1–x0
7. If | | > 𝑒, then x0=x1 and go to step 6
x1
Else go to step 8
8. Display x1 as root
9. Stop
//Program for Newton-Raphson Method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define e 0.0001
#define f(x) x*x*x-x*x+x+7
#define fd(x) 3*x*x-2*x+1
int main()
{
float x0,x1, error=0;
float f0,fd0;
printf("Enter x0: ");
scanf("%f",&x0);
do{
f0=f(x0);
fd0=fd(x0);
x1=x0-f0/fd0;
x0=x1;
error=fabs((x1-x0)/x1);
}while(error>e);
printf("\n\nroot is %f",x1);
return 0;
}
3. Secant Method
Flowchart
Start
Define a function f(x)
Define a function df(x)
Input initial
value x0
𝑓(𝑥0)
𝑥1 = 𝑥0 −
𝑑𝑓(𝑥0)
Yes
𝑥1 − 𝑥0 x0=x1
| | > 𝐸?
𝑥1
No
Root = x1
Stop
4. Gauss Elimination Method
1. Start
2. Read Number of Unknowns: n
3. Read Augmented Matrix (A) of n by n+1 Size
4. Transform Augmented Matrix (A) to Upper Triangular Matrix by Row Operations.
5. Obtain Solution by Back Substitution.
6. Display Result.
7. Stop
Programm of gauss elimination method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define SIZE 10
int main()
{
float a[SIZE][SIZE], x[SIZE], ratio;
int i,j,k,n;
/* 1. Reading number of unknowns */
printf("Enter number of unknowns: ");
scanf("%d", &n);
/* 2. Reading Augmented Matrix */
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
printf("a[%d][%d] = ",i,j);
scanf("%f", &a[i][j]);
}}
4. Gauss Elimination Method
/* Applying Gauss Elimination */
for(i=1;i<=n-1;i++)
{
for(j=i+1;j<=n;j++)
{
ratio = a[j][i]/a[i][i];
for(k=1;k<=n+1;k++)
{
a[j][k] = a[j][k] - ratio*a[i][k];
}
}
}
/* Obtaining Solution by Back Subsitution */
x[n] = a[n][n+1]/a[n][n];
for(i=n-1;i>=1;i--)
{
x[i] = a[i][n+1];
for(j=i+1;j<=n;j++)
{
x[i] = x[i] - a[i][j]*x[j];
}
x[i] = x[i]/a[i][i];
}
/* Displaying Solution */
printf("\nSolution:\n");
for(i=1;i<=n;i++)
{
printf("x[%d] = %0.3f\n",i, x[i]);
}
return 0;
}
5. Gauss Jordan Method
1. Start
2. Read Number of Unknowns: n
3. Read Augmented Matrix (A) of n by n+1 Size
4. Transform Augmented Matrix (A) to Diagonal Matrix by Row Operations.
5. Obtain Solution by Making All Diagonal Elements to 1.
6. Display Result.
7. Stop
Program of gauss Jordan method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define SIZE 10
int main()
{ float a[SIZE][SIZE], x[SIZE], ratio;
int i,j,k,n;
/* Inputs */
/* 1. Reading number of unknowns */
printf("Enter number of unknowns: ");
scanf("%d", &n);
/* 2. Reading Augmented Matrix */
printf("Enter coefficients of Augmented Matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
printf("a[%d][%d] = ",i,j);
scanf("%f", &a[i][j]);
}}
/* Applying Gauss Jordan Elimination */
for(i=1;i<=n;i++)
5. Gauss Jordan Method
{
if(a[i][i] == 0.0)
{
printf("Mathematical Error!");
exit(0);
}
for(j=1;j<=n;j++)
{
if(i!=j)
{
ratio = a[j][i]/a[i][i];
for(k=1;k<=n+1;k++)
{
a[j][k] = a[j][k] - ratio*a[i][k];
}
}
}
}
/* Obtaining Solution */
for(i=1;i<=n;i++)
{
x[i] = a[i][n+1]/a[i][i];
}
/* Displaying Solution */
printf("\nSolution:\n");
for(i=1;i<=n;i++)
{
printf("x[%d] = %0.3f\n",i, x[i]);
}
Return 0;
}
6. Power Method
1. Start
2. Read Order of Matrix (n) and Tolerable Error (e)
3. Read Matrix A of Size n x n
4. Read Initial Guess Vector X of Size n x 1
5. Initialize: Lambda_Old = 1
6. Multiply: X_NEW = A * X
7. Replace X by X_NEW
8. Find Largest Element (Lamda_New) by Magnitude from X_NEW
9. Normalize or Divide X by Lamda_New
10. Display Lamda_New and X
11. If |Lambda_Old - Lamda_New| > e thenset
Lambda_Old = Lamda_New and goto step (6)
otherwise goto step (12)
12. Stop
Program of power method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define SIZE 10
int main()
{
float a[SIZE][SIZE], x[SIZE],x_new[SIZE];
float temp, lambda_new, lambda_old, error;
int i,j,n, step=1;
clrscr();
/* Inputs */
printf("Enter Order of Matrix: ");
scanf("%d", &n);
printf("Enter Tolerable Error: ");
scanf("%f", &error);
/* Reading Matrix */
printf("Enter Coefficient of Matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
6. Power Method
printf("a[%d][%d]=",i,j);
scanf("%f", &a[i][j]);
}
}
/* Reading Intial Guess Vector */
printf("Enter Initial Guess Vector:\n");
for(i=1;i<=n;i++)
{
printf("x[%d]=",i);
scanf("%f", &x[i]);
}
/* Initializing Lambda_Old */
lambda_old = 1;
/* Multiplication */
up:
for(i=1;i<=n;i++)
{
temp = 0.0;
for(j=1;j<=n;j++)
{
temp = temp + a[i][j]*x[j];
}
x_new[i] = temp;
}
/* Replacing */
for(i=1;i<=n;i++)
{
x[i] = x_new[i];
}
/* Finding Largest */
lambda_new = fabs(x[1]);
for(i=2;i<=n;i++)
{
if(fabs(x[i])>lambda_new)
{
lambda_new = fabs(x[i]);
}
}
/* Normalization */
for(i=1;i<=n;i++)
{
x[i] = x[i]/lambda_new;
6. Power Method
}
/* Display */
printf("\n\nSTEP-%d:\n", step);
printf("Eigen Value = %f\n", lambda_new);
printf("Eigen Vector:\n");
for(i=1;i<=n;i++)
{
printf("%f\t", x[i]);
}
/* Checking Accuracy */
if(fabs(lambda_new-lambda_old)>error)
{
lambda_old=lambda_new;
step++;
goto up;
}
Return 0;
}
7. Jacobi Iteration Method
1. Start
2. Arrange given system of linear equations in diagonally dominant form
3. Read tolerable error (e)
4. Convert the first equation in terms of first variable, second equation in terms of second variable
and so on.
5. Set initial guesses for x0, y0, z0 and so on
6. Substitute value of x0, y0, z0 ... from step 5 in equation obtained in step 4 to calculate new
values x1, y1, z1 and so on
7. If| x0 - x1| > e and | y0 - y1| > e and | z0 - z1| > e and so on thengoto step 9
8. Set x0=x1, y0=y1, z0=z1 and so on and goto step 6
9. Print value of x1, y1, z1 and so on
10. Stop
Program of jacobi iteration method
#include<stdio.h>
#include<conio.h>
#include<math.h>
/* Defining function */
#define f1(x,y,z) (17-y+2*z)/20
#define f2(x,y,z) (-18-3*x+z)/20
#define f3(x,y,z) (25-2*x+3*y)/20
/* Main function */
int main()
{
float x0=0, y0=0, z0=0, x1, y1, z1, e1, e2, e3, e;
int count=1;
clrscr();
printf("Enter tolerable error:\n");
scanf("%f", &e);
printf("\nCount\tx\ty\tz\n");
do
{
/* Calculation */
x1 = f1(x0,y0,z0);
y1 = f2(x0,y0,z0);
z1 = f3(x0,y0,z0);
printf ("%d\t%0.4f\t%0.4f\t%0.4f\n", count, x1, y1, z1);
/* Error */
e1 = fabs(x0-x1);
e2 = fabs(y0-y1);
e3 = fabs(z0-z1);
count++;
/* Set value for next iteration */
x0 = x1;
y0 = y1;
z0 = z1;
}while(e1>e && e2>e && e3>e);
printf("\nSolution: x=%0.3f, y=%0.3f and z =
%0.3f\n",x1,y1,z1);
return 0;
}
8. Gauss Seidel Iterative Method
1. Start
2. Arrange given system of linear equations in diagonally dominant form
3. Read tolerable error (e)
4. Convert the first equation in terms of first variable, second equation in terms of second variable
and so on.
5. Set initial guesses for x0, y0, z0 and so on
6. Substitute value of y0, z0 ... from step 5 in first equation obtained from step 4 to calculate new
value of x1. Use x1, z0, u0 ....in second equation obtained from step 4 to calculate new value of
y1.
Similarly, use x1, y1, u0... to find new z1 and so on.
7. If| x0 - x1| > e and | y0 - y1| > e and | z0 - z1| > e and so on thengoto step 9
8. Set x0=x1, y0=y1, z0=z1 and so on and goto step 6
9. Print value of x1, y1, z1 and so on
10. Stop
Program of seidal programming
#include<stdio.h>
#include<conio.h>
#include<math.h>
/* Defining function */
#define f1(x,y,z) (17-y+2*z)/20
#define f2(x,y,z) (-18-3*x+z)/20
#define f3(x,y,z) (25-2*x+3*y)/20
/* Main function */
int main()
{
float x0=0, y0=0, z0=0, x1, y1, z1, e1, e2, e3, e;
int count=1;
clrscr();
printf("Enter tolerable error:\n");
scanf("%f", &e);
8. Gauss Seidel Iterative Method
printf("\nCount\tx\ty\tz\n");
do
{
/* Calculation */
x1 = f1(x0,y0,z0);
y1 = f2(x1,y0,z0);
z1 = f3(x1,y1,z0);
printf("%d\t%0.4f\t%0.4f\t%0.4f\n",count, x1,y1,z1);
/* Error */
e1 = fabs(x0-x1);
e2 = fabs(y0-y1);
e3 = fabs(z0-z1);
count++;
/* Set value for next iteration */
x0 = x1;
y0 = y1;
z0 = z1;
}while(e1>e && e2>e && e3>e);
printf("\nSolution: x=%0.3f, y=%0.3f and z =
%0.3f\n",x1,y1,z1);
return 0;
}
9. Lagrange Interpolation Method
1. Start
2. Read number of data (n)
3. Read data Xi and Yi for i=1 ton n
4. Read value of independent variables say xp whose corresponding value of dependent say yp is to
be determined.
5. Initialize: yp = 0
6. For i = 1 to n Set p
= 1 For j =1 to n
If i ≠ j then
Calculate p = p * (xp - Xj)/(Xi - Xj)
End If
Next j
Calculate yp = yp + p * Yi
Next i
6. Display value of yp as interpolated value.
7. Stop
Program of lagrange interpolation
#include<stdio.h>
#include<conio.h>
void main()
{
float x[100], y[100], xp, yp=0, p;
int i,j,n;
clrscr();
/* Input Section */
printf("Enter number of data: ");
scanf("%d", &n);
printf("Enter data:\n");
for(i=1;i<=n;i++)
{
printf("x[%d] = ", i);
scanf("%f", &x[i]);
9. Lagrange Interpolation Method
printf("y[%d] = ", i);
scanf("%f", &y[i]);
}
printf("Enter interpolation point: ");
scanf("%f", &xp);
/* Implementing Lagrange Interpolation */
for(i=1;i<=n;i++)
{
p=1;
for(j=1;j<=n;j++)
{
if(i!=j)
{
p = p* (xp - x[j])/(x[i] - x[j]);
}
}
yp = yp + p * y[i];
}
printf("Interpolated value at %.3f is %.3f.", xp, yp);
getch();
}
11. Trapezoidal Method Algorithm
1. Start
2. Define function f(x)
3. Read lower limit of integration, upper limit of integration and number of sub interval
4. Calculate: step size = (upper limit - lower limit)/number of subinterval
5. Set: integration value = f(lower limit) + f(upper limit)
6. Set: i = 1
7. If i > number of sub interval then goto
8. Calculate: k = lower limit + i * h
9. Calculate: Integration value = Integration Value + 2* f(k)
10. Increment i by 1 i.e. i = i+1 and go to step 7
11. Calculate: Integration value = Integration value * step size/2
12. Display Integration value as required answer
13. Stop
Program of Trapezoidal method
#include<stdio.h>
#include<conio.h>
#include<math.h>
/* Define function here */
#define f(x) 1/(1+pow(x,2))
int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
clrscr();
/* Input */
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval);
11. Trapezoidal Method Algorithm
/* Calculation */
/* Finding step size */
stepSize = (upper - lower)/subInterval;
/* Finding Integration Value */
integration = f(lower) + f(upper);
for(i=1; i<= subInterval-1; i++)
{
k = lower + i*stepSize;
integration = integration + 2 * f(k);
}
integration = integration * stepSize/2;
printf("\nRequired value of integration is: %.3f",
integration);
getch();
return 0;
}
11. Trapezoidal Method Algorithm
12. Simpson's 1/3 Rule Algorithm
1. Start
2. Define function f(x)
3. Read lower limit of integration, upper limit ofintegration and number
of sub interval
4. Calcultae: step size = (upper limit - lower limit)/number of subinterval
5. Set: integration value = f(lower limit) + f(upper limit)
6. Set: i = 1
7. If i > number of sub interval then goto
8. Calculate: k = lower limit + i * h
9. If i mod 2 =0 then
Integration value = Integration Value + 2* f(k)Otherwise
Integration Value = Integration Value + 4 * f(k)End If
10. Increment i by 1 i.e. i = i+1 and go to step 7
11. Calculate: Integration value = Integration value * step size/3
12. Display Integration value as required answer
13. Stop
Program of simpsons 1/3 rule
#include<stdio.h>
#include<conio.h>
#include<math.h>
/* Define function here */
#define f(x) 1/(1+x*x)
int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
clrscr();
/* Input */
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval);
/* Calculation */
/* Finding step size */
12. Simpson's 1/3 Rule Algorithm
stepSize = (upper - lower)/subInterval;
/* Finding Integration Value */
integration = f(lower) + f(upper);
for(i=1; i<= subInterval-1; i++)
{
k = lower + i*stepSize;
if(i%2==0)
{
integration = integration + 2 * f(k);
}
else
{
integration = integration + 4 * f(k);
}
}
integration = integration * stepSize/3;
printf("\nRequired value of integration is: %.3f",
integration);
getch();
return 0;
}
Simpson's 3/8 Rule Algorithm
1. Start
2. Define function f(x)
3. Read lower limit of integration, upper limit of
integration and number of sub interval
4. Calcultae: step size = (upper limit - lower limit)/number of sub interval
5. Set: integration value = f(lower limit) + f(upper limit)
6. Set: i = 1
7. If i > number of sub interval then goto
8. Calculate: k = lower limit + i * h
9. If i mod 3 =0 then
Integration value = Integration Value + 2* f(k)
Otherwise
Integration Value = Integration Value + 3 * f(k)
End If
10. Increment i by 1 i.e. i = i+1 and go to step 7
11. Calculate: Integration value = Integration value * step size*3/8
12. Display Integration value as required answer
13. Stop
Program of simpson’s 3/8 method
#include<stdio.h>
#include<conio.h>
#include<math.h>
/* Define function here */
#define f(x) 1/(1+x*x)
int main()
{
float lower, upper, integration=0.0, stepSize, k;
int i, subInterval;
clrscr();
/* Input */
printf("Enter lower limit of integration: ");
scanf("%f", &lower);
printf("Enter upper limit of integration: ");
scanf("%f", &upper);
printf("Enter number of sub intervals: ");
scanf("%d", &subInterval);
/* Calculation */
/* Finding step size */
stepSize = (upper - lower)/subInterval;
/* Finding Integration Value */
integration = f(lower) + f(upper);
for(i=1; i<= subInterval-1; i++)
{
k = lower + i*stepSize;
if(i%3 == 0)
{
integration = integration + 2 * f(k);
}
else
{
integration = integration + 3 * f(k);
}
}
integration = integration * stepSize*3/8;
printf("\nRequired value of integration is: %.3f",
integration);
getch();
return 0;
}
Euler's Method
1. Start
2. Define function f(x,y)
3. Read values of initial condition(x0 and y0), number of steps (n) and calculation point (xn)
4. Calculate step size (h) = (xn - x0)/b
5. Set i=0
6. Loop
yn = y0 + h * f(x0 + i*h, y0)
y0 = yn
i=i+1
While i < n
7. Display yn as result
8. Stop
Program of Euler algorithm
#include<stdio.h>
#include<conio.h>
#define f(x,y) x+y
int main()
{
float x0, y0, xn, h, yn, slope;
int i, n;
clrscr();
printf("Enter Initial Condition\n");
printf("x0 = ");
scanf("%f", &x0);
printf("y0 = ");
scanf("%f", &y0);
printf("Enter calculation point xn = ");
scanf("%f", &xn);
printf("Enter number of steps: ");
scanf("%d", &n);
/* Calculating step size (h) */
h = (xn-x0)/n;
/* Euler's Method */
printf("\nx0\ty0\tslope\tyn\n");
printf("------------------------------\n");
for(i=0; i < n; i++)
{
slope = f(x0, y0);
yn = y0 + h * slope;
printf("%.4f\t%.4f\t%0.4f\t%.4f\n",x0,y0,slope,yn);
y0 = yn;
x0 = x0+h;
}
/* Displaying result */
printf("\nValue of y at x = %0.2f is %0.3f",xn, yn);
return 0;
}
13. Runge Kutta (RK-4) Method
1. Start
2. Define function f(x,y)
3. Read values of initial condition(x0 and y0), number of steps (n) andcalculation point (xn)
4. Calculate step size (h) = (xn - x0)/n
5. Set i=0
6. Loop
k1 = h * f(x0, y0)
k2 = h*f(x0+h/2, y0+k1/2)
k3 = h*f(x0+h/2, y0+k2/2)
k4 = h * f(x0+h, y0+k3)
k = (k1+2*k2+2*k3+k4)/6
yn = y0 + k
i=I+1
x0 = x0 + h
y0 = yn
While i < n
7. Display yn as result
8. Stop
Program of 4th order Runge kutta method
#include<stdio.h>
#include<conio.h>
#define f(x,y) (y*y-x*x)/(y*y+x*x)
int main()
{
float x0, y0, xn, h, yn, k1, k2, k3, k4, k;
int i, n;
clrscr();
printf("Enter Initial Condition\n");
printf("x0 = ");
scanf("%f", &x0);
printf("y0 = ");
scanf("%f", &y0);
printf("Enter calculation point xn = ");
scanf("%f", &xn);
printf("Enter number of steps: ");
scanf("%d", &n);
/* Calculating step size (h) */
h = (xn-x0)/n;
/* Runge Kutta Method */
printf("\nx0\ty0\tyn\n");
for(i=0; i < n; i++)
{
k1 = h * (f(x0, y0));
k2 = h * (f((x0+h/2), (y0+k1/2)));
k3 = h * (f((x0+h/2), (y0+k2/2)));
k4 = h * (f((x0+h), (y0+k3)));
k = (k1+2*k2+2*k3+k4)/6;
yn = y0 + k;
printf("%0.4f\t%0.4f\t%0.4f\n",x0,y0,yn);
x0 = x0+h;
y0 = yn;
}
/* Displaying result */
printf("\nValue of y at x = %0.2f is %0.3f",xn, yn);
return 0;
}
Curve Fitting of Type y=ax^b Algorithm
1. Start
2. Read Number of Data (n)
3. For i=1 to n:
Read Xi and Yi
Next i
4. Initialize:
sumX = 0
sumX2 = 0
sumY = 0
sumXY = 0
5. Calculate Required Sum
For i=1 to n:
sumX = sumX + log(Xi)
sumX2 = sumX2 + log(Xi) * log(Xi)
sumY = sumY + log(Yi)
sumXY = sumXY + log(Xi) * log(Yi)
Next i
6. Calculate Required Constant A and b of Y = A + bX:
b = (n * sumXY - sumX * sumY)/(n*sumX2 - sumX * sumX)
A = (sumY - b*sumX)/n
7. Transformation of A to a:
a = exp(A)
8. Display value of a and b
8. Stop
Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define S 50
int main()
{
int n, i;
float x[S], y[S], sumX=0, sumX2=0, sumY=0, sumXY=0, a, b,
A;
clrscr();
/* Input */
printf("How many data points?\n");
scanf("%d", &n);
for(i=1;i<=n;i++)
{
printf("x[%d]=",i);
scanf("%f", &x[i]);
printf("y[%d]=",i);
scanf("%f", &y[i]);
}
/* Calculating Required Sum */
for(i=1;i<=n;i++)
{
sumX = sumX + log(x[i]);
sumX2 = sumX2 + log(x[i])*log(x[i]);
sumY = sumY + log(y[i]);
sumXY = sumXY + log(x[i])*log(y[i]);
}
/* Calculating A and b */
b = (n*sumXY-sumX*sumY)/(n*sumX2-sumX*sumX);
A = (sumY - b*sumX)/n;
/* Transformation of A to a */
a = exp(A);
/* Displaying value of a and b */
printf("Values are: a=%0.2f and b = %0.2f",a,b);
getch();
return(0);
}