Skip to content

Commit 409341e

Browse files
authored
Add files via upload
0 parents  commit 409341e

15 files changed

+729
-0
lines changed

lab_01_bisection.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <iostream>
2+
#include <cmath>
3+
#define f(x) x *x *x + 3 * x *log10(x)
4+
#define e 0.000001
5+
6+
using namespace std;
7+
int main()
8+
{
9+
system("cls");
10+
double a, b, c;
11+
double fa, fb, fc;
12+
cout << "Enter Intervals a and b: ";
13+
cin >> a >> b;
14+
do
15+
{
16+
fa = f(a);
17+
fb = f(b);
18+
if (fa * fb > 0)
19+
{
20+
cout << "Invalid Intervals! They donot Bracket the root!" << endl;
21+
return 0;
22+
}
23+
c = (a + b) / 2;
24+
fc = f(c);
25+
if (fc * fa < 0)
26+
b = c;
27+
else if (fb * fc < 0)
28+
a = c;
29+
else
30+
cout << "ERROR!! \n";
31+
} while (fabs((b-a)/a) > e);
32+
cout << "Root = " << c << endl;
33+
return 0;
34+
}

lab_02_newton_raphson.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
#include <cmath>
3+
#define e 0.000001
4+
#define f(x) cos(x) + x *x - 4
5+
#define fd(x) 2 * x - sin(x)
6+
using namespace std;
7+
int main()
8+
{
9+
system("cls");
10+
double x0, x = 0, fx0, fdx0;
11+
cout << "Enter Initial Guess: ";
12+
cin >> x0;
13+
do
14+
{
15+
fx0 = f(x0);
16+
fdx0 = fd(x0);
17+
x = x0 - (fx0 / fdx0);
18+
x0 = x;
19+
} while (fabs(f(x)) > e);
20+
cout << "Root = " << x;
21+
return 0;
22+
}

lab_02_secantmethod.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
#include <cmath>
3+
#define e 0.0001
4+
#define f(x) x*x-3*x*log(x)
5+
using namespace std;
6+
int main()
7+
{
8+
system("cls");
9+
double a, b, c, fa, fb, fc;
10+
int iterations;
11+
cout << "Enter Initial Guesses a and b: ";
12+
cin >> a >> b;
13+
do
14+
{
15+
fa = f(a);
16+
fb = f(b);
17+
c = (a * fb - b * fa) / (fb - fa);
18+
fc = f(c);
19+
a = b;
20+
b = c;
21+
} while (fabs(fc) > e);
22+
cout << "Root: " << c;
23+
return 0;
24+
}

lab_03_gauss_elimination.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int n, i, j, k;
7+
cout << "Enter number of unknowns: ";
8+
cin >> n;
9+
10+
float a[n][n + 1], x[n], ratio;
11+
12+
cout << "Enter Coefficients of Augmented Matrix: " << endl;
13+
for (i = 0; i < n; i++)
14+
for (j = 0; j < n + 1; j++)
15+
cin >> a[i][j];
16+
17+
/* Gaussian Elimination */
18+
for (i = 0; i < n - 1; i++)
19+
{
20+
if (a[i][i] == 0.0)
21+
{
22+
cout << "Mathematical Error!";
23+
return 0;
24+
}
25+
for (j = i + 1; j < n; j++)
26+
{
27+
ratio = a[j][i] / a[i][i];
28+
for (k = 0; k <= n; k++)
29+
{
30+
a[j][k] = a[j][k] - ratio * a[i][k];
31+
}
32+
}
33+
}
34+
35+
/* Back Substitution */
36+
x[n - 1] = a[n - 1][n] / a[n - 1][n - 1];
37+
for (i = n - 2; i >= 0; i--)
38+
{
39+
x[i] = a[i][n];
40+
for (j = i + 1; j < n; j++)
41+
{
42+
x[i] -= a[i][j] * x[j];
43+
}
44+
x[i] = x[i] / a[i][i];
45+
}
46+
47+
/* Display Solution */
48+
cout << endl
49+
<< "Solution: " << endl;
50+
for (i = 0; i < n; i++)
51+
{
52+
cout << "x[" << i + 1 << "] = " << x[i] << endl;
53+
}
54+
55+
return 0;
56+
}

lab_03_gauss_jordan.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
int main()
5+
{
6+
system("cls");
7+
int size, i, j, k;
8+
cout << "Enter Size of a: ";
9+
cin >> size;
10+
float a[size][size + 1], x[size];
11+
float ratio;
12+
cout << "Enter Coefficients of Augmented a: ";
13+
for (i = 0; i < size; i++)
14+
{
15+
for (j = 0; j < size + 1; j++)
16+
{
17+
cin >> a[i][j];
18+
}
19+
}
20+
21+
// Calculating the Identity a
22+
for (i = 0; i < size; i++)
23+
{
24+
if (a[i][i] == 0)
25+
{
26+
cout << "Gauss Jordan Method Fails! ";
27+
exit(0);
28+
}
29+
for (j = 0; j < size; j++)
30+
{
31+
if (i != j)
32+
{
33+
ratio = a[j][i] / a[i][i];
34+
for (k = 0; k < size + 1; k++)
35+
a[j][k] -= ratio * a[i][k];
36+
}
37+
}
38+
}
39+
40+
// Assigning and Displaying the Result
41+
for (i = 0; i < size; i++)
42+
{
43+
x[i] = a[i][size] / a[i][i];
44+
cout << "x" << i << " = " << x[i] << endl;
45+
}
46+
47+
return 0;
48+
}

lab_04_EulersMethod.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*Solution of First Order Ordinary Differential Equation*/
2+
// Also called Initial Value Problem
3+
// Approximate Value is found out
4+
5+
#include <iostream>
6+
#include <cmath>
7+
#include <iomanip>
8+
#define f(x, y) 6 * x - 3
9+
using namespace std;
10+
int main()
11+
{
12+
system("cls");
13+
float x0, y0, x1, y1, xn, h, s;
14+
cout << "Enter x0 and y0: ";
15+
cin >> x0 >> y0;
16+
cout << "Enter xn: ";
17+
cin >> xn;
18+
cout << "Enter incremental value: ";
19+
cin >> h;
20+
cout << "Solution: \n\n";
21+
cout << " x\t y\n";
22+
cout << "---------------\n";
23+
cout << setw(4) << x0 << "\t" << setw(6) << y0 << endl;
24+
while (x0 < xn)
25+
{
26+
s = f(x0, y0);
27+
x1 = x0 + h;
28+
y1 = y0 + h * s;
29+
x0 = x1;
30+
y0 = y1;
31+
cout << setw(4) << x0 << "\t" << setw(6) << y0 << endl;
32+
}
33+
return 0;
34+
}

lab_04_Inverse_GJordan.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <iostream>
2+
#include <iomanip>
3+
using namespace std;
4+
int main()
5+
{
6+
system("cls");
7+
int size, i, j, k;
8+
cout << "Enter no. of Unknowns: ";
9+
cin >> size;
10+
float a[size][size], inv[size][size], ratio;
11+
12+
/*Initializing Identity Matrix*/
13+
for (i = 0; i < size; i++)
14+
{
15+
for (j = 0; j < size; j++)
16+
{
17+
if (i == j)
18+
inv[i][j] = 1;
19+
else
20+
inv[i][j] = 0;
21+
}
22+
}
23+
24+
/*Input the Matrix to be Inversed*/
25+
cout << "Enter Matrix to be Inversed: ";
26+
for (i = 0; i < size; i++)
27+
for (j = 0; j < size; j++)
28+
cin >> a[i][j];
29+
30+
/*Gauss Jordan Method*/
31+
for (i = 0; i < size; i++)
32+
{
33+
if (a[i][i] == 0)
34+
{
35+
cout << "Diagonal Element Cannot Be Zero!";
36+
exit(0);
37+
}
38+
for (j = 0; j < size; j++)
39+
{
40+
ratio = a[j][i] / a[i][i];
41+
if (i != j)
42+
{
43+
for (k = 0; k < size; k++)
44+
{
45+
a[j][k] -= ratio * a[i][k];
46+
inv[j][k] -= ratio * inv[i][k]; // simultaneously applying row transformation to the identity matrix
47+
}
48+
}
49+
}
50+
}
51+
52+
/*Display the Inverse*/
53+
cout << endl
54+
<< "Inverse of Matrix is: " << endl;
55+
for (i = 0; i < size; i++)
56+
{
57+
for (j = 0; j < size; j++)
58+
{
59+
inv[i][j] /= a[i][i];
60+
cout << setw(5) << inv[i][j] << "\t";
61+
}
62+
cout << endl;
63+
}
64+
return 0;
65+
}

lab_04_RK4_Method.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*Solution of First Order Ordinary Differential Equation*/
2+
// Also called Initial Value Problem
3+
// Approximate Value is found out
4+
// yi+1 = yi + h*f(xi,yi)
5+
// Tabular form output
6+
// define f(x,y)
7+
// Input x0,y0,h,xn
8+
// s =f(x0,y0)
9+
// y1 = y0 + h * s
10+
// x1 = x0 + h
11+
// x0 = x1
12+
// y0 = y1
13+
// print in tablular form
14+
15+
#include <iostream>
16+
#include <cmath>
17+
#include <iomanip>
18+
#define f(x, y) 6*x - 3
19+
using namespace std;
20+
int main()
21+
{
22+
system("cls");
23+
float x0, y0, x1, y1, xn, h, m1, m2, m3, m4, m;
24+
cout << "Enter x0 and y0: ";
25+
cin >> x0 >> y0;
26+
cout << "Enter xn: ";
27+
cin >> xn;
28+
cout << "Enter incremental value: ";
29+
cin >> h;
30+
cout << "Solution: \n\n";
31+
cout << " x\t y\n";
32+
cout << "---------------\n";
33+
cout << setw(4) << x0 << "\t" << setw(6) << y0 << endl;
34+
while (x0 < xn)
35+
{
36+
m1 = f(x0, y0);
37+
m2 = f(x0 + h / 2, y0 + m1 * h / 2);
38+
m3 = f(x0 + h / 2, y0 + m2 * h / 2);
39+
m4 = f(x0 + h, y0 + m3 * h);
40+
m = (m1 + 2 * m2 + 2 * m3 + m4) / 6;
41+
x1 = x0 + h;
42+
y1 = y0 + h * m;
43+
x0 = x1;
44+
y0 = y1;
45+
cout << setw(4) << x0 << "\t" << setw(6) << y0 << endl;
46+
}
47+
return 0;
48+
}

lab_05_ExpCurveFitting.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <iostream>
2+
#include <iomanip>
3+
#include <cmath>
4+
using namespace std;
5+
int main()
6+
{
7+
system("cls");
8+
int n, i;
9+
float sumx = 0, sumlny = 0, sumxlny = 0, sumx2 = 0, a, b;
10+
cout << "Enter Number of Data Points: ";
11+
cin >> n;
12+
float x[n], y[n];
13+
14+
// Enter Values of X and Y
15+
cout << "Enter Values of x and y: " << endl;
16+
for (i = 0; i < n; i++)
17+
cin >> x[i] >> y[i];
18+
19+
// Calculate the Summations
20+
for (i = 0; i < n; i++)
21+
{
22+
sumx += x[i];
23+
sumlny += log(y[i]);
24+
sumxlny += x[i] * log(y[i]);
25+
sumx2 += x[i] * x[i];
26+
}
27+
28+
if ((sumx * sumx - sumx2 * n) != 0)
29+
{
30+
b = (sumlny * sumx - sumxlny * n) / (sumx * sumx - sumx2 * n);
31+
a = (sumx * sumxlny - sumx2 * sumlny) / (sumx * sumx - sumx2 * n);
32+
a = exp(a);
33+
cout << "The Equation of Curve is: \n";
34+
cout << fixed;
35+
cout << "y = " << setw(5) << setprecision(4) << a << "e^" << setw(5) << b << "x";
36+
}
37+
else
38+
{
39+
cout << "Determinant is Zero!";
40+
exit(0);
41+
}
42+
return 0;
43+
}

0 commit comments

Comments
 (0)