commit be590b5cfd3d3e9e6585b3da3ef2c4f4bd11fe10 Author: Amneesh Singh Date: Mon Dec 26 19:14:30 2022 +0530 exp 1-6: init Signed-off-by: Amneesh Singh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..78f61d4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +*tex +*~ +\#* +_minted-file/ +.\# +*html +lab*/*pdf +*.in +*.out \ No newline at end of file diff --git a/1.c b/1.c new file mode 100644 index 0000000..a4f9f96 --- /dev/null +++ b/1.c @@ -0,0 +1,15 @@ +#include +#include +#define EPSILON 0.0000001 +#define f(x) ((352 * x * x * x) - (64 * x * x) + (198 * x) - 36) +#define f1(x) ((1056 * x * x) - (128 * x) + 198) +double newtonRaphson(double x) { + double h = f(x) / f1(x); + if (f(x) == 0 || fabs(h) < EPSILON) + return x; + return newtonRaphson(x - h); +} +int main() { + printf("Root for f(x) = 352x^3 - 64x^2 + 198x - 36 is %lf", + newtonRaphson(-4)); +} diff --git a/2.c b/2.c new file mode 100644 index 0000000..34eb26e --- /dev/null +++ b/2.c @@ -0,0 +1,23 @@ +#include +#include +#include +#define EPSILON 0.0000001 +#define f(x) ((352 * x * x * x) - (64 * x * x) + (198 * x) - 36) +double bisection(double a, double b) { + double x; + if (f(a) * f(b) > 0) { + printf("The values of function at the respective initial guesses must have " + "opposite signs"); + exit(1); + } + x = (a + b) / 2; + if (f(x) == 0 || fabs(b - a) < EPSILON) + return x; + if (f(x) > 0) + return bisection(x, b); + return bisection(a, x); +} +int main() { + printf("Root for f(x) = 352x^3 - 64x^2 + 198x - 36 is %lf", + bisection(1.6, -4)); +} diff --git a/3.c b/3.c new file mode 100644 index 0000000..c5b4325 --- /dev/null +++ b/3.c @@ -0,0 +1,14 @@ +#include +#include +#define EPSILON 0.0000001 +#define f(x) ((352 * x * x * x) - (64 * x * x) + (198 * x) - 36) +double secant(double a, double b) { + double x1; + x1 = (a * f(b) - b * f(a)) / (f(b) - f(a)); + if (f(x1) == 0 || fabs(a - b) < EPSILON) + return x1; + return secant(b, x1); +} +int main() { + printf("Root for f(x) = 352x^3 - 64x^2 + 198x - 36 is %lf", secant(1.6, -4)); +} diff --git a/4.c b/4.c new file mode 100644 index 0000000..1786e67 --- /dev/null +++ b/4.c @@ -0,0 +1,33 @@ +#include + +int main() { + double xp, yp = 0, p; + int i, j, n; + + printf("Number of inputs: "); + scanf("%d", &n); + + double x[n], y[n]; + printf("Input sample space:\n"); + + for (i = 0; i < n; i++) { + printf("x%d: ", i); + scanf("%lf", x + i); + printf("y%d: ", i); + scanf("%lf", y + i); + } + + printf("Enter interpolation point x: "); + scanf("%lf", &xp); + + for (i = 0; i < n; i++) { + p = 1; + for (j = 0; j < n; j++) { + if (i != j) { + p *= (xp - x[j]) / (x[i] - x[j]); + } + } + yp += p * y[i]; + } + printf("Interpolated value for %lf is %lf.", xp, yp); +} diff --git a/5.c b/5.c new file mode 100644 index 0000000..89e8fcd --- /dev/null +++ b/5.c @@ -0,0 +1,42 @@ +#include + +int main() { + int n, i, j = 1; + double xp, yp, f1, f2 = 0; + + printf("Enter the number of inputs: "); + scanf("%d", &n); + + double x[n], y[n]; + + printf("Enter input values:\n"); + for (i = 0; i < n; i++) { + printf("x%d=", i); + scanf("%lf", x + i); + printf("y%d=", i); + scanf("%lf", y + i); + } + + yp = y[0]; + + printf("Enter interpolation point x: "); + scanf("%lf", &xp); + + do { + for (i = 0; i < n - 1; i++) + y[i] = ((y[i + 1] - y[i]) / (x[i + j] - x[i])); + + f1 = 1; + + for (i = 0; i < j; i++) { + f1 *= (xp - x[i]); + } + + f2 += (y[0] * f1); + j++; + } while ((--n) > 1); + + yp += f2; + + printf("Interpolated value for %lf is %lf.", xp, yp); +} diff --git a/6.c b/6.c new file mode 100644 index 0000000..ca97bbc --- /dev/null +++ b/6.c @@ -0,0 +1,23 @@ +#include +#include + +#define f(x) ((352 * x * x * x) - (64 * x * x) + (198 * x) - 36) + +double trapezoidal_integral(double a, double b, double n) { + double h = (b - a) / n; + + double s = (f(a) + f(b)) / 2; + + // Add the other heights + + for (int i = 1; i < n; i++) + s += f(a + i * h); + + return s * h; +} + +int main() { + printf("The area under the curve f(x) = 352x^3 - 64x^2 + 198x - 36 from x=3 " + "to x=4 is %lf", + trapezoidal_integral(3, 4, 10000)); +} diff --git a/file.org b/file.org new file mode 100644 index 0000000..6b6eb85 --- /dev/null +++ b/file.org @@ -0,0 +1,227 @@ +#+title: Computational Methods Lab +#+author: Amneesh Singh I6 +#+LATEX_HEADER: \usepackage[margin=0.5in]{geometry} + +#+LATEX: \clearpage + +|------------------------------------------------------------------------------------------------------------| +| Programs are followed by their respective inputs and outputs i.e, both stdin and stdout are shown together | +|------------------------------------------------------------------------------------------------------------| + +* Program for finding roots of f(x) = 0 using Newton Raphson Method +#+ATTR_LATEX: :options frame=single,breaklines=true +#+begin_src C :tangle 1.c :results output :exports both :wrap src text +#include +#include +#define EPSILON 0.0000001 +#define f(x) ((352 * x * x * x) - (64 * x * x) + (198 * x) - 36) +#define f1(x) ((1056 * x * x) - (128 * x) + 198) +double newtonRaphson(double x) { + double h = f(x) / f1(x); + if (f(x) == 0 || fabs(h) < EPSILON) + return x; + return newtonRaphson(x - h); +} +int main() { + printf("Root for f(x) = 352x^3 - 64x^2 + 198x - 36 is %lf", + newtonRaphson(-4)); +} +#+end_src + +* Program for nding roots of f(x) = 0 using bisection method +#+ATTR_LATEX: :options frame=single,breaklines=true +#+begin_src C :tangle 2.c :results output :exports both :wrap src text +#include +#include +#include +#define EPSILON 0.0000001 +#define f(x) ((352 * x * x * x) - (64 * x * x) + (198 * x) - 36) +double bisection(double a, double b) { + double x; + if (f(a) * f(b) > 0) { + printf("The values of function at the respective initial guesses must have " + "opposite signs"); + exit(1); + } + x = (a + b) / 2; + if (f(x) == 0 || fabs(b - a) < EPSILON) + return x; + if (f(x) > 0) + return bisection(x, b); + return bisection(a, x); +} +int main() { + printf("Root for f(x) = 352x^3 - 64x^2 + 198x - 36 is %lf", + bisection(1.6, -4)); +} +#+end_src + +#+LATEX: \clearpage + +* Program for finding roots of f(x) = 0 using secant method +#+ATTR_LATEX: :options frame=single,breaklines=true +#+begin_src C :tangle 3.c :results output :exports both :wrap src text +#include +#include +#define EPSILON 0.0000001 +#define f(x) ((352 * x * x * x) - (64 * x * x) + (198 * x) - 36) +double secant(double a, double b) { + double x1; + x1 = (a * f(b) - b * f(a)) / (f(b) - f(a)); + if (f(x1) == 0 || fabs(a - b) < EPSILON) + return x1; + return secant(b, x1); +} +int main() { + printf("Root for f(x) = 352x^3 - 64x^2 + 198x - 36 is %lf", secant(1.6, -4)); +} +#+end_src + +#+LATEX: \clearpage + +* Program to implement Langrange Interpolation. +#+ATTR_LATEX: :options frame=single,breaklines=true +#+begin_src C :tangle 4.c :results output :wrap src text +#include + +int main() { + double xp, yp = 0, p; + int i, j, n; + + printf("Number of inputs: "); + scanf("%d", &n); + + double x[n], y[n]; + printf("Input sample space:\n"); + + for (i = 0; i < n; i++) { + printf("x%d: ", i); + scanf("%lf", x + i); + printf("y%d: ", i); + scanf("%lf", y + i); + } + + printf("Enter interpolation point x: "); + scanf("%lf", &xp); + + for (i = 0; i < n; i++) { + p = 1; + for (j = 0; j < n; j++) { + if (i != j) { + p *= (xp - x[j]) / (x[i] - x[j]); + } + } + yp += p * y[i]; + } + printf("Interpolated value for %lf is %lf.", xp, yp); +} +#+end_src + +#+begin_src text +Number of inputs: 4 +Input sample space: +x0: 0 +y0: 2 +x1: 1 +y1: 3 +x2: 2 +y2: 12 +x3: 5 +y3: 147 +Enter interpolation point x: 3 +Interpolated value for 3.000000 is 35.000000. +#+end_src + +#+LATEX: \clearpage + +* Program to implement Newton's Divided Difference formula. +#+ATTR_LATEX: :options frame=single,breaklines=true +#+begin_src C :tangle 5.c :results output :wrap src text +#include + +int main() { + int n, i, j = 1; + double xp, yp, f1, f2 = 0; + + printf("Enter the number of inputs: "); + scanf("%d", &n); + + double x[n], y[n]; + + printf("Enter input values:\n"); + for (i = 0; i < n; i++) { + printf("x%d=", i); + scanf("%lf", x + i); + printf("y%d=", i); + scanf("%lf", y + i); + } + + yp = y[0]; + + printf("Enter interpolation point x: "); + scanf("%lf", &xp); + + do { + for (i = 0; i < n - 1; i++) + y[i] = ((y[i + 1] - y[i]) / (x[i + j] - x[i])); + + f1 = 1; + + for (i = 0; i < j; i++) { + f1 *= (xp - x[i]); + } + + f2 += (y[0] * f1); + j++; + } while ((--n) > 1); + + yp += f2; + + printf("Interpolated value for %lf is %lf.", xp, yp); +} +#+end_src + +#+LATEX: \clearpage + +#+begin_src text +Enter the number of inputs: 4 +Enter input values: +x0=3 +y0=9 +x1=5 +y1=12 +x2=9 +y2=666 +x3=15 +y3=10245 +Enter interpolation point x: 999 +Interpolated value for 999.000000 is 9525764925.000002. +#+end_src + +* Program for solving numerical integration by trapezoidal method. +#+ATTR_LATEX: :options frame=single,breaklines=true +#+begin_src C :tangle 6.c :results output :exports both :wrap src text +#include +#include + +#define f(x) ((352 * x * x * x) - (64 * x * x) + (198 * x) - 36) + +double trapezoidal_integral(double a, double b, double n) { + double h = (b - a) / n; + + double s = (f(a) + f(b)) / 2; + + // Add the other heights + + for (int i = 1; i < n; i++) + s += f(a + i * h); + + return s * h; +} + +int main() { + printf("The area under the curve f(x) = 352x^3 - 64x^2 + 198x - 36 from x=3 " + "to x=4 is %lf", + trapezoidal_integral(3, 4, 10000)); +} +#+end_src diff --git a/file.pdf b/file.pdf new file mode 100644 index 0000000..ffc6f4d Binary files /dev/null and b/file.pdf differ