CBNST Kapil
CBNST Kapil
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);
Kapil Bisht F1 34
OUTPUT :
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);
f0 = func(x0);
f1 = func(x1);
Kapil Bisht F1 34
OUTPUT :
f(x) = x*x – 30
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()
{
return 0;
}
Kapil Bisht F1 34
OUTPUT :
f(x) = x*x-30 , g(x) = 2*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>
Kapil Bisht F1 34
OUTPUT :
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");
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);
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);
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 data:\n");
for (i = 1; i <= n; i++)
{
printf("x[%d]=", i);
scanf("%f", &x[i]);
printf("y[%d]=", i);
scanf("%f", &y[i]);
}
Kapil Bisht F1 34
OUTPUT :
Kapil Bisht F1 34