0% found this document useful (0 votes)
7 views36 pages

CBNST Kapil

The document contains a series of programs written in C to find the roots of non-linear equations using various methods including Bisection, False Position, Newton's Raphson, Iteration, and Gauss Elimination. Each section provides the code for the respective method, along with example outputs and mathematical functions used. Additionally, it includes methods for numerical interpolation using Newton's forward and backward difference methods.

Uploaded by

Rajat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views36 pages

CBNST Kapil

The document contains a series of programs written in C to find the roots of non-linear equations using various methods including Bisection, False Position, Newton's Raphson, Iteration, and Gauss Elimination. Each section provides the code for the respective method, along with example outputs and mathematical functions used. Additionally, it includes methods for numerical interpolation using Newton's forward and backward difference methods.

Uploaded by

Rajat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Objective 1: WAP to find the roots of non-linear equation using

Bisection method.

Program:

#include <stdio.h>
#include <math.h>
#define func(x) (x * x * x - 4 * x - 9)
int main()
{
float x0, x1, x2, e;
while (1)
{
printf("Enter the initial range: ");
scanf("%f%f", &x0, &x1);

printf("Enter the tolerable error: ");


scanf("%f", &e);

if (func(x0) * func(x1) <= 0)


{
break;
}
else
printf("\nIncorrect Range\n");
}
do
{
x2 = (x0 + x1) / 2;
if (func(x0) * func(x2) < 0)
x1 = x2;
else
x0 = x2;

} while (fabs(func(x2)) >= e);


printf("\nThe root is: %f\n", x2);
return 0;
}

Kapil Bisht F1 34
OUTPUT :

f(x) = x*x*x – 4*x – 9

f(x) = x*x*x-20

f(x) = x-cos(x)

Kapil Bisht F1 34
Objective 2: WAP to find the roots of non-linear equation using
False position method.

Program:

#include <stdio.h>
#include <math.h>
#define func(x) (x * log10(x) - 1.20)
int main()
{
float x0, x1, x2 = 0, temp, f0, f1, f2, e;
int iteration = 1;
while(1)
{
printf("Enter the initial range: ");
scanf("%f%f", &x0, &x1);

printf("Enter the tolerable error: ");


scanf("%f", &e);

if(func(x0) * func(x1) <= 0.0)


break;
else
printf("\nIncorrect Range\n");
}
printf("Iteration \t x0 \t\t\t x1 \t\t\t x2 \t\t\t f(x2)");
do
{
temp = x2;

f0 = func(x0);
f1 = func(x1);

x2 = (x0 * f1 - x1 * f0) / (f1 - f0);


f2 = func(x2);
printf("\n%d \t\t %f \t\t %f \t\t %f \t\t %f", iteration, x0, x1, x2, f2);
if(f2 == 0)
break;
else if(f0 * f2 < 0)
x1 = x2;
else
Kapil Bisht F1 34
x0 = x2;
iteration = iteration + 1;
} while (fabs(x2 - temp) > e);
printf("\nThe Root is : %f\n", x2);
return 0;
}

Kapil Bisht F1 34
OUTPUT :

f(x) = x*x – 30

f(x) = x*x*x – 4*x – 9

Kapil Bisht F1 34
Objective 3: WAP to find the roots of non-linear equation using
Newton’s Raphson method.

Program:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define f(x) x * x - 30
#define g(x) 2 * x

int main()
{

float x0, x1, f0, f1, g0, e;


int step = 1, N;

printf("Enter the initial guess: ");


scanf("%f", &x0);

printf("Enter tolerable error: ");


scanf("%f", &e);

printf("Enter maximum iteration: ");


scanf("%d", &N);

printf("\nStep \t\t x0 \t\t f(x0) \t\t x1 \t\t f(x1)\n");


do
{
g0 = g(x0);
f0 = f(x0);
if (g0 == 0.0)
{
printf("Mathematical Error!");
exit(0);
}
x1 = x0 - f0 / g0;
f1 = f(x1);
printf("\n%d \t\t %f \t %f \t %f \t %f ", step, x0, f0, x1, f1);
x0 = x1;
step = step + 1;
if (step > N)
Kapil Bisht F1 34
{
printf("Not Convergent.");
exit(0);
}

} while (fabs(f1) > e);

printf("\n\nThe Root is: %f\n", x1);

return 0;
}

Kapil Bisht F1 34
OUTPUT :
f(x) = x*x-30 , g(x) = 2*x

f(x) = x-exp(-x) , g(x) = x +exp(-x)

Kapil Bisht F1 34
Objective 4: WAP to find the roots of non-linear equation using
Iteration method.

Program:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define f(x) (2 * x - log10(x) - 7)


#define g(x) ((log10(x) + 7) / 2)
int main()
{
float x0, x1, e;
int step = 1, N;

printf("Enter the initial guess: ");


scanf("%f", &x0);
printf("Enter tolerable error: ");
scanf("%f", &e);
printf("Enter maximum iteration: ");
scanf("%d", &N);
printf("\nStep \t\t x0 \t\t f(x0) \t\t x1 \t\t f(x1)\n");
do
{
x1 = g(x0);
printf("\n%d \t\t %f \t %f \t %f \t %f ", step, x0, f(x0), x1, f(x1));
step = step + 1;
if(step > N)
{
printf("Not Convergent.");
exit(0);
}
x0 = x1;
} while(fabs(f(x1)) > e);
printf("\n\nThe Root is: %f\n", x1);
return(0);
}

Kapil Bisht F1 34
OUTPUT :

f(x) = 2*x - log10x – 7 , g(x) = (log10x + 7) / 2

Kapil Bisht F1 34
Objective 5: WAP to solve the system of linear equations using
Gauss Elimination method.

Program:

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>

int main()
{
int n, m, i, j, k;
float a[100][101] = {0};
printf("Enter the number of equations: ");
scanf("%d", &n);
int idx = 0;
int xyz = 0;
while (n--)
{
char s[200];
printf("Enter the equation:\n");
scanf("%s", s);
bool flag = false;
int num = 0;
int cont = 0;
int sign = 1;
for (int i = 0; i < strlen(s); i++)
{
if (flag && (s[i] >= '0' && s[i] <= '9'))
{
cont = cont * 10 + (s[i] - '0');
}
if (s[i] == '=')
flag = true;
else if (s[i] == '-')
sign = -1;
else if (s[i] == 'x')
{
if (sign == -1 && num == 0)
{
num = 1;
}
Kapil Bisht F1 34
a[idx][0] = sign * num == 0 ? 1 : sign * num;
num = 0;
xyz++;
sign = 1;
}
else if (s[i] == 'y')
{
if (sign == -1 && num == 0)
{
num = 1;
}
a[idx][1] = sign * num == 0 ? 1 : sign * num;
num = 0;

if (xyz == 0)
a[idx][0] = 0;
xyz++;
sign = 1;
}
else if (s[i] == 'z')
{
if (sign == -1 && num == 0)
{
num = 1;
}
a[idx][2] = sign * num == 0 ? 1 : sign * num;
num = 0;
if (xyz == 0)
{
a[idx][0] = 0;
a[idx][1] = 0;
}
if (xyz == 1)
{
if (a[idx][1] == '\0')
a[idx][1] = 0;
else
a[idx][0] = 0;
}
sign = 1;
xyz++;
}
else if (s[i] >= '0' && s[i] <= '9')
{

Kapil Bisht F1 34
num = num * 10 + (s[i] - '0');
}
}
a[idx][3] = sign * cont;
idx++;
}
float t;
printf("Equation 1: (%.0f)x+(%.0f)y+(%.0f)z=%.0f\n", a[0][0], a[0][1], a[0][2], a[0][3]);
printf("Equation 2: (%.0f)x+(%.0f)y+(%.0f)z=%.0f\n", a[1][0], a[1][1], a[1][2], a[1][3]);
printf("Equation 3: (%.0f)x+(%.0f)y+(%.0f)z=%.0f\n", a[2][0], a[2][1], a[2][2], a[2][3]);
printf("\n\n");
printf("Augmented Matrix: \n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
{
printf("%.2f ", a[i][j]);
}
printf(" \n");
}
printf("\n");
for (int j = 0; j < 3; j++)
{
for (int i = 0; i < 3; i++)
{
if (i > j)
{
t = a[i][j] / a[j][j];
for (k = 0; k < 4; k++)
{
a[i][k] = a[i][k] - t * a[j][k];
}
}
}
}
printf("Final Matrix form : \n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
{
printf("\t %.2f", a[i][j]);
}
printf("\n");
}

Kapil Bisht F1 34
n = 2;
float sum = 0.0;
float x[n];
x[n] = a[n][n + 1] / a[n][n];
for (i = n - 1; i >= 0; i--)
{
sum = 0;
for (j = i + 1; j <= n; j++)
{
sum = sum + a[i][j] * x[j];
}
x[i] = (a[i][n + 1] - sum) / a[i][i];
}
printf("\n\nValues are :\n");

for (i = 0; i <= n; i++)


{
printf("x%d = %.2f\n", i, x[i]);
}
}

Kapil Bisht F1 34
OUTPUT :

x + 4x - z = -5 , x + y - 6z = - 12 , 3x - y - z = 4

Kapil Bisht F1 34
Objective 6: WAP to interpolate numerically using Newton’s
forward difference method.

Program:

#include <stdio.h>
#include <stdlib.h>

int fact(int x)
{
int f = 1;
for(int i = 1; i <= x; i++)
f = f * i;
return f;
}

int main()
{
int n;
printf("Enter the number of arguments: ");
scanf("%d", &n);

float arr[n][n+1];
for(int i = 0; i < n; i++)
{
printf("Enter x%d and y%d : ", i, i);
scanf("%f%f", &arr[i][0], &arr[i][1]);
}
for(int i = 2; i < n+1; i++)
{
for(int j = 0; j < n-i+1; j++)
{
arr[j][i] = arr[j+1][i-1] - arr[j][i-1];
}
}
printf("\n\nThe forward difference table is: \n\n");
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n-i+1; j++)
{
printf("%0.2f ", arr[i][j]);
}
Kapil Bisht F1 34
printf("\n");
}
float x;
printf("\nEnter a point of interpolation: ");
scanf("%f", &x);

float h = arr[1][0] - arr[0][0];


float u = (x - arr[0][0]) / h;

float result = arr[0][1];


float t = 1.0;

for(int i = 0; i < n-1; i++)


{
t = t * (u-i);
result += (t * arr[0][i+2]) / fact(i+1);
}
printf("The value of y at x = %0.2f is: %0.2f\n", x, result);
return 0;

Kapil Bisht F1 34
OUTPUT :

Kapil Bisht F1 34
Objective 7: WAP to interpolate numerically using Newton’s
backward difference method.

Program:

#include <stdio.h>
#include <math.h>
float fact(int i, float p)
{
float ans = p;
for (int j = i; j > 1; j--)
{
ans = ans * (p - 1 + j);
}
return ans;
}
float factorial(int j)
{
float ans = 1;
for (int i = 1; i <= j; i++)
ans *= i;
return ans;
}
int main()
{
int n;
printf("Enter the number of arguments: ");
scanf("%d", &n);

float x[n];
float y[n];
for (int i = 0; i < n; i++)
{
printf("Enter x%d and y%d (space separated): ", i, i);
scanf("%f", &x[i]);
scanf("%f", &y[i]);
}

float num;
printf("\nEnter a point of interpolation: ");
scanf("%f", &num);

float p = (float)(num - x[n - 1]) / (float)(x[1] - x[0]);


Kapil Bisht F1 34
float ans = y[n - 1];

printf("\n");
for (int i = 1; i < n; i++)
{
float new_y[n - i];
printf("d^%dy: ", i);
for (int j = 0; j < n - i; j++)
{
new_y[j] = y[j + 1] - y[j];
printf("%.2f ", new_y[j]);
}
printf("\n");
ans += (fact(i, p) / factorial(i)) * new_y[n - i - 1];
for (int h = 0; h < n - i; h++)
{
y[h] = new_y[h];
}
y[n - i] = '\0';
}
printf("The value of y at x = %0.2f is: %0.2f\n", num, ans);
}

Kapil Bisht F1 34
OUTPUT :

Kapil Bisht F1 34
Objective 8: WAP to interpolate numerically using Lagrange’s
method.

Program:

#include <stdio.h>
#include <math.h>
float calculate_sum(int i, int n, float *x, float num)
{
float upper = 1;
float lower = 1;
for (int j = 0; j < n; j++)
{
if (j != i)
{
upper *= (num - x[j]);
lower *= (x[i] - x[j]);
}
}
float ans = upper / lower;
return ans;
}
int main()
{
int n;
printf("Enter number of values: ");
scanf("%d", &n);

float x[n];
float y[n];
for (int i = 0; i < n; i++)
{
printf("Enter x%d and y%d (space separated): ", i, i);
scanf("%f", &x[i]);
scanf("%f", &y[i]);
}
float num;
printf("\nEnter a point of interpolation: ");
scanf("%f", &num);

float ans = 0;
for (int i = 0; i < n; i++)
{
Kapil Bisht F1 34
float sum = calculate_sum(i, n, x, num);
ans += sum * y[i];
}
printf("The value of y at x = %0.2f is: %0.2f\n", num, ans);
}

Kapil Bisht F1 34
OUTPUT :

Kapil Bisht F1 34
Objective 9: WAP to Integrate numerically using Trapezoidal
rule.

Program:

#include <stdio.h>
#include <math.h>
double func(double x)
{
return (exp(x*x));
}
int main()
{
int n, a, b;
printf("Enter lower limit(a): ");
scanf("%d", &a);
printf("Enter upper limit(b): ");
scanf("%d", &b);
printf("\nEnter number of intervals: ");
scanf("%d", &n);

double h = (b - a) / (float)n;
double x[n + 1];
double y[n + 1];
int count = 0;
printf("\nx\t\ty\n");
for (int i = 0; i <= n; i++)
{
x[i] = a + (count * h);
count++;
y[i] = func(x[i]);
printf("%f\t%f\n", x[i], y[i]);
}
double sum = 0;
for (int i = 1; i <= n - 1; i++)
{
sum += y[i];
}
double ans = (h / 2) * (y[0] + y[n] + 2 * sum);
printf("\nApproximate value of integration is: %f \n", ans);
}

Kapil Bisht F1 34
OUTPUT :

Kapil Bisht F1 34
Objective 10: WAP to Integrate numerically using Simpson’s
1/3 rules.

Program:

#include <stdio.h>
#include <math.h>
double func(double x){
return (x + 4);
}
int main(){
int n, a, b;
printf("\nEnter number of intervals: ");
scanf("%d", &n);
printf("Enter lower limit(a): ");
scanf("%d", &a);
printf("Enter upper limit(b): ");
scanf("%d", &b);
double h = (b - a) / (float)n;
double x[n + 1];
double y[n + 1];
int count = 0;
printf("x\t \t y\n");
for (int i = 0; i <= n; i++){
x[i] = a + count * h;
count++;
y[i] = func(x[i]);
printf("%f\t %f\n", x[i], y[i]);
}
double sum_o = 0;
double sum_e = 0;
for (int i = 1; i <= n - 1; i++)
{
if (i % 2 == 0)
sum_e += y[i];
else
sum_o += y[i];
}
double ans = (h / 3) * (y[0] + y[n] + 4 * sum_o + 2 * sum_e);
printf("\nApproximate value of integration is: %f \n", ans);
}

Kapil Bisht F1 34
OUTPUT :

Kapil Bisht F1 34
Objective 11 WAP to Integrate numerically using Simpson’s 3/8
rules.

Program:

#include <stdio.h>
#include <math.h>
double func(double x)
{
return (x + 4);
}
int main()
{
int n, a, b;
printf("\nEnter number of intervals: ");
scanf("%d", &n);
printf("Enter lower limit(a): ");
scanf("%d", &a);
printf("Enter upper limit(b): ");
scanf("%d", &b);
double h = (b - a) / (float)n;
double x[n + 1];
double y[n + 1];
int count = 0;
printf("x\t\t y\n");
for (int i = 0; i <= n; i++){
x[i] = a + count * h;
count++;
y[i] = func(x[i]);
printf("%f\t%f\n", x[i], y[i]);
}
double sum_o = 0;
double sum_e = 0;
for (int i = 1; i <= n - 1; i++){
if (i % 3 == 0)
sum_e += y[i];
else
sum_o += y[i];
}
double ans = (3 * h / 8) * (y[0] + y[n] + 3 * sum_o + 2 * sum_e);
printf("\nApproximate value of integration is: %f \n", ans);
}

Kapil Bisht F1 34
OUTPUT :

Kapil Bisht F1 34
Objective 12: WAP to find numerical solution of ordinary
differential equations by Euler’s method.

Program:

#include <stdio.h>
#include <math.h>
double func(double x, double y)
{
return x + y;
}
int main()
{
double h, x0, y0, num;
int n;
printf("Enter x0: ");
scanf("%lf", &x0);
printf("Enter y0: ");
scanf("%lf", &y0);
printf("Enter h: ");
scanf("%lf", &h);
printf("Enter the vaue of x for which corresponding y to be found: ");
scanf("%lf", &num);

n = num / h;
double x[n + 1];
double y[n + 1];
double differtiation[n];
double new_y[n];
x[0] = x0;
y[0] = y0;
printf("\nx\t\ty\t\t(dy/dx)\t\tnew_y\n");
for (int i = 0; i < n; i++)
{
differtiation[i] = func(x[i], y[i]);
new_y[i] = y[i] + h * differtiation[i];
printf("%.4f\t\t%.4f\t\t%.4f\t\t%.4f\n", x[i], y[i], differtiation[i], new_y[i]);
x[i + 1] = x0 + (i + 1) * h;
y[i + 1] = new_y[i];
}
printf("\n%.4f\t\t%.4f\t\t\n", x[n], y[n]);
printf("\nApproximate value is: %f\n", new_y[n - 1]);
}
Kapil Bisht F1 34
OUTPUT :

Kapil Bisht F1 34
Objective 13: WAP to find numerical solution of ordinary
differential equations by Runge-Kutta (fourth order) method.

Program:

#include <stdio.h>
double function(double x, double y){
return (x + y * y);
}
int main(){
double h, num, x0, y0, k, k1, k2, k3, k4;
int n;
printf("Enter x0: ");
scanf("%lf", &x0);
printf("Enter y0: ");
scanf("%lf", &y0);
printf("Enter h: ");
scanf("%lf", &h);
printf("Enter the value of x for which corresponding y to be found: ");
scanf("%lf", &num);

n = (num - x0) / h;
double x[n];
double y[n];
x[0] = x0, y[0] = y0;
double ans;
for (int i = 0; i < n; i++){
x[i] = x0 + i * h;
k1 = h * (function(x[i], y[i]));
k2 = h * (function(x[i] + (h * 0.5), y[i] + (k1 * 0.5)));
k3 = h * (function(x[i] + (h * 0.5), y[i] + (k2 * 0.5)));
k4 = h * (function(x[i] + h, y[i] + k3));
k = (k1 + 2 * k2 + 2 * k3 + k4) / 6;
printf("\nAt approximation: %d", i + 1);
printf("\nk1\tk2\tk3\tk4\tk\n");
printf("%.4f\t%.4f\t%.4f\t%.4f\t%.4f\n", k1, k2, k3, k4, k);
y[i + 1] = y[i] + k;
ans = y[i + 1];
printf("\nValue at y(%.4f): %f\n", h * (i + 1), ans);
printf("---------------------------------------------\n");
}
printf("\nApproximate value is: %f\n", ans);
}
Kapil Bisht F1 34
OUTPUT :

Kapil Bisht F1 34
Objective 14: WAP to linear Curve fitting by least – square
approximations.

Program:

#include <stdio.h>
#define S 50
int main()
{
int n, i;
float x[S], y[S], sumX = 0, sumX2 = 0, sumY = 0, sumXY = 0, a, b;

printf("Enter the number of data points (n): ");


scanf("%d", &n);

printf("Enter data:\n");
for (i = 1; i <= n; i++)
{
printf("x[%d]=", i);
scanf("%f", &x[i]);

printf("y[%d]=", i);
scanf("%f", &y[i]);
}

for (i = 1; i <= n; i++)


{
sumX = sumX + x[i];
sumX2 = sumX2 + x[i] * x[i];
sumY = sumY + y[i];
sumXY = sumXY + x[i] * y[i];
}
b = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX * sumX);
a = (sumY - b * sumX) / n;

printf("Values are: a=%0.2f and b = %0.2f", a, b);


printf("\nEquation of best fit is: y = %0.2f + %0.2fx", a, b);
return (0);
}

Kapil Bisht F1 34
OUTPUT :

Kapil Bisht F1 34

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy