Bisection Method
#include<stdio.h>
#include<math.h>
float fun (float x)
{
return (x*x*x - 4*x + 9);
}
void bisection (float *x, float a, float b, int *itr)
/* this function performs and prints the result of one iteration */
{
*x=(a+b)/2;
++(*itr);
printf("Iteration no. %3d X = %7.5f\n", *itr, *x);
}
int main ()
{
int itr = 0, maxmitr;
float x, a, b, allerr, x1;
printf("\nEnter the values of a, b, allowed error and maximum iterations:\n");
scanf("%f %f %f %d", &a, &b, &allerr, &maxmitr);
bisection (&x, a, b, &itr);
do
{
if (fun(a)*fun(x) < 0)
b=x;
else
a=x;
bisection (&x1, a, b, &itr);
if (fabs(x1-x) < allerr)
{
printf("After %d iterations, root = %6.4f\n", itr, x1);
return 0;
}
x=x1;
}
while (itr < maxmitr);
printf("The solution does not converge or iterations are not sufficient");
}
#include<math.h>
#include<stdio.h>
void main()
{
float x[6], y[6],sum=0,temp,a;
int i,j;
printf("Enter the values for x:");
for(i=0;i<4;i++)
scanf("%f",&x[i]);
printf("Enter y:");
for(i=0;i<4;i++)
scanf("%f",&y[i]);
printf("Enter point:");
scanf("%f",&a);
for(i=0;i<4;i++)
{
temp=1.0;
for(j=0;j<4;j++)
{
if(i!=j)
temp=temp*((a-x[j])/(x[i]-x[j]));
}
sum=sum+temp*y[i];
}
printf("Solution: %f",sum);
}
OUTPUT:
#include<stdio.h>
#include<math.h>
float f(float x)
{
return ((x*exp(x))-2);
}
void main()
{
int i;
float a,b,x;
printf("Enter value of a: ");
scanf("%f",&a);
printf("Enter the value of b: ");
scanf("%f",&b);
for(i=0;i<=20;i++)
{
x=(((a*f(b))-(b*f(a)))/(f(b)-f(a)));
if(f(a)*f(x)<0)
b=x;
else
a=x;
printf("Root at %d iteration is %f\n",i,x);
}
}
OUTPUT:
NOTE:
The Above Code in Bold Letters May Change according to the Question (You can Edit it on your Own).
#include<math.h>
float fun (float x)
{
return (x*x*x - 4*x + 9);
}
void bisection (float *x, float a, float b, int *itr)
/* this function performs and prints the result of one iteration */
{
*x=(a+b)/2;
++(*itr);
printf("Iteration no. %3d X = %7.5f\n", *itr, *x);
}
int main ()
{
int itr = 0, maxmitr;
float x, a, b, allerr, x1;
printf("\nEnter the values of a, b, allowed error and maximum iterations:\n");
scanf("%f %f %f %d", &a, &b, &allerr, &maxmitr);
bisection (&x, a, b, &itr);
do
{
if (fun(a)*fun(x) < 0)
b=x;
else
a=x;
bisection (&x1, a, b, &itr);
if (fabs(x1-x) < allerr)
{
printf("After %d iterations, root = %6.4f\n", itr, x1);
return 0;
}
x=x1;
}
while (itr < maxmitr);
printf("The solution does not converge or iterations are not sufficient");
}
OUTPUT:
Bisection Method |
Lagrange Interpolation
#include<math.h>
#include<stdio.h>
void main()
{
float x[6], y[6],sum=0,temp,a;
int i,j;
printf("Enter the values for x:");
for(i=0;i<4;i++)
scanf("%f",&x[i]);
printf("Enter y:");
for(i=0;i<4;i++)
scanf("%f",&y[i]);
printf("Enter point:");
scanf("%f",&a);
for(i=0;i<4;i++)
{
temp=1.0;
for(j=0;j<4;j++)
{
if(i!=j)
temp=temp*((a-x[j])/(x[i]-x[j]));
}
sum=sum+temp*y[i];
}
printf("Solution: %f",sum);
}
OUTPUT:
Interpolation |
Regula Falsi Method
#include<stdio.h>
#include<math.h>
float f(float x)
{
return ((x*exp(x))-2);
}
void main()
{
int i;
float a,b,x;
printf("Enter value of a: ");
scanf("%f",&a);
printf("Enter the value of b: ");
scanf("%f",&b);
for(i=0;i<=20;i++)
{
x=(((a*f(b))-(b*f(a)))/(f(b)-f(a)));
if(f(a)*f(x)<0)
b=x;
else
a=x;
printf("Root at %d iteration is %f\n",i,x);
}
}
OUTPUT:
Regula-Falsi method |
NOTE:
The Above Code in Bold Letters May Change according to the Question (You can Edit it on your Own).
No comments:
Post a Comment