Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Newton-Raphson:
- double fun(double x) {
- return 3 * x * x + 2 * x - 4;
- }
- double dfun(double x) {
- return 6 * x + 2;
- }
- int main() {
- int i = 0; double x = 0;
- do {
- double x1 = x;
- x = x1 - fun(x1) / dfun(x1);
- cout << "At iteration-" << ++i << " , Root = " << x << '\n';
- } while (fabs(fun(x)) > 0.0001);
- }
- // Secant Method:
- double fun(double x) {
- return 2 * x * x + 3 * x - 7;
- }
- int main() {
- int i = 1; double x1 = 5, x2 = 1;
- double x3 = (fun(x2) * x1 - fun(x1) * x2) / (fun(x2) - fun(x1));
- cout << fixed << "At iteration-" << i << " :\n";
- cout << "Root = " << x3 << "\nFunction value = " << fun(x3) << '\n';
- while (fabs((x3 - x2) / x3) > 0.0001) {
- x1 = x2; x2 = x3;
- x3 = (fun(x2) * x1 - fun(x1) * x2) / (fun(x2) - fun(x1));
- cout << "At iteration-" << ++i << " :\n";
- cout << "Root = " << x3 << "\nFunction value = " << fun(x3) << '\n';
- }
- }
- // Guess Eliminaton:
- float a[10][10], x[10], ratio;
- int i, j, k, n;
- cout << setprecision(3) << fixed;
- cout << "Enter number of unknowns:";
- cin >> n;
- cout << "Enter co of augmented matrix:" << '\n';
- for (i = 1;i <= n;++i) {
- for (j = 1;j <= n + 1;++j) {
- cout << "a[" << i << "]" << j << "] =";
- cin >> a[i][j];
- }
- }
- for (k = 1;k <= n - 1;k++) {
- for (i = k + 1;i <= n;++i) {
- ratio = a[i][k] / a[k][k];
- for (j = k;j <= n + 1;++j) {
- a[i][j] -= (ratio * a[k][j]);
- }
- }
- }
- cout << "after gauess:" << '\n';
- for (i = 1;i <= n;++i) {
- for (j = 1;j <= n + 1;++j) {
- cout << a[i][j] << " ";
- }
- cout << '\n';
- }
- x[n] = a[n][n + 1] / a[n][n];
- for (i = n - 1;i >= 1;i--)
- {
- x[i] = a[i][n + 1];
- for (j = i + 1;j <= n;++j)
- {
- x[i] = x[i] - a[i][j] * x[j];
- }
- x[i] = x[i] / a[i][i];
- }
- cout << '\n' << "Solution: " << '\n';
- for (i = 1;i <= n;++i)
- {
- cout << "x[" << i << "] = " << x[i] << '\n';
- }
- return 0;
- }
- // Jecobi Method:
- int n = 3;
- double a[n][n + 1] = { {2,1,1,5},{3,5,2,15},{2,1,4,8} }, xn[3], xo[3] = { 0,0,0 };
- int c = 0;
- while (true) {
- int f = 0;
- for (int i = 0;i < n;i++) {
- double sum = 0;
- for (int j = 0;j < n;j++) {
- if (j != i) sum += (a[i][j] * xo[j]);
- }
- xn[i] = (a[i][n] - sum) / a[i][i];
- }
- for (int k = 0;k < n;k++) {
- if (abs((xn[k] - xo[k])) > 0.0001) {
- xo[k] = xn[k];
- f++;
- }
- }
- c++;
- if (f == 0)break;
- }
- for (int i = 0;i < n;i++) {
- cout << xn[i] << " " << endl;
- }
- // Gauss Seidal
- int n = 3;
- double a[n][n + 1] = { {2,1,1,5},{3,5,2,15},{2,1,4,8} }, xo[3] = { 0,0,0 };
- int c = 0;
- while (true) {
- int f = 0;
- double xn;
- for (int i = 0;i < n;++i) {
- double sum = 0;
- for (int j = 0;j < n;++j) {
- if (j != i) sum += (a[i][j] * xo[j]);
- }
- double xn = (a[i][n] - sum) / a[i][i];
- if (abs((xn - xo[i]) / xn) > 0.0001) {
- xo[i] = xn;
- f++;
- }
- }
- c++;
- if (f == 0)break;
- }
- for (int i = 0;i < n;++i) {
- cout << xo[i] << " " << '\n';
- }
- return 0;
- }
- // Least Square
- #define f(x) 1/(1+pow(x,2))
- int i, n;
- double x, y, a, b;
- double sx = 0, sxx = 0, sy = 0, sxy = 0;
- cout << "Enter the number of values for n : ";
- cin >> n;
- for (int i = 0;i < n;++i) {
- cout << "Enter the value of x and y : ";
- cin >> x >> y;
- sx += x;
- sy += y;
- sxx += (x * x);
- sxy += (x * y);
- }
- double xb = sx / n;
- double yb = sy / n;
- b = (sxy - yb * sx) / (sxx - xb * sx);
- a = (sy - b * sx) / n;
- cout << "Values of a and b are : " << a << " " << b << '\n';
- // Simpson 3/8
- #define f(x) 1/(1+pow(x,2))
- double lower, upper, i = 0.0, h, k;
- int i, n;
- cout << "Enter lower limit of integration: ";
- cin >> lower;
- cout << "Enter upper limit of integration: ";
- cin >> upper;
- cout << "Enter number of sub intervals: ";
- cin >> n;
- h = (upper - lower) / n;
- i = f(lower) + f(upper);
- for (i = 1; i <= n - 1; ++i)
- {
- k = lower + i * h;
- if (i % 3 == 0) i = i + 2 * (f(k));
- else i = i + 3 * (f(k));
- }
- i = i * h * 3.0 / 8.0;
- cout << '\n' << "Area is: " << i;
- // Simpson 1/3
- #define f(x) 1/(1+pow(x,2))
- double lower, upper, i = 0.0, h, k;
- int i, n;
- cout << "Enter lower limit of integration: ";
- cin >> lower;
- cout << "Enter upper limit of integration: ";
- cin >> upper;
- cout << "Enter number of sub intervals: ";
- cin >> n;
- h = (upper - lower) / n;
- i = f(lower) + f(upper);
- for (i = 1; i <= n - 1; ++i)
- {
- k = lower + i * h;
- if (i % 2 == 0) i = i + 2 * (f(k));
- else i = i + 4 * (f(k));
- }
- i = i * h / 3;
- cout << '\n' << "Area is: " << i;
- /// Trapizoidal
- #define f(x) 1/(1+pow(x,2))
- double lower, upper, i = 0.0, h, k;
- int i, n;
- cout << "Enter lower limit of integration: ";
- cin >> lower;
- cout << "Enter upper limit of integration: ";
- cin >> upper;
- cout << "Enter number of sub intervals: ";
- cin >> n;
- h = (upper - lower) / n;
- i = f(lower) + f(upper);
- for (i = 1; i <= n - 1; ++i)
- {
- k = lower + i * h;
- i = i + 2 * (f(k));
- }
- i = i * h / 2;
- cout << '\n' << "Area is: " << i;
- // Lagrange
- double x[100], y[100], xp, yp = 0, p;
- int i, j, n;
- cout << "Enter number of data: ";
- cin >> n;
- cout << "Enter data:" << '\n';
- for (i = 1;i <= n;++i)
- {
- cout << "x[" << i << "] = ";
- cin >> x[i];
- cout << "y[" << i << "] = ";
- cin >> y[i];
- }
- cout << "Enter interpolation point: ";
- cin >> xp;
- for (i = 1;i <= n;++i)
- {
- p = 1;
- for (j = 1;j <= n;++j)
- {
- if (i != j)
- {
- p = p * (xp - x[j]) / (x[i] - x[j]);
- }
- }
- yp = yp + p * y[i];
- }
- cout << '\n' << "Interpolated value at " << xp << " is " << yp;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement