Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- newton 1================================================
- #include <iostream>
- #include <math.h>
- #include<conio.h>
- using namespace std;
- float f(float x) {
- return x * exp(x) - 1;
- }
- float g(float x) {
- return exp(x)*(x + 1);
- }
- void main() {
- const float eps =1e-5;
- const int ITMAX = 100;
- float x = 0;
- int n = 0;
- while ((fabs(f(x)) > eps) && n<=ITMAX) {
- x = (x - f(x) / g(x));
- n = n + 1;
- }
- if (n > ITMAX)
- cout << "Numarul maxim de iteratii depasit";
- else {
- cout << "Valoarea aproximativa a solutiei este" << x << endl;
- cout << "Numarul de iteratii este:" << n;
- }
- _getch();
- }
- NEWTON 2===================================================================
- #include <iostream>
- #include <math.h>
- #include<conio.h>
- #include<stdio.h>
- using namespace std;
- void newton_radical(double x, int itmax, double eps, int p, double a) {
- int n = 1;
- while ((fabs(pow(x, p) - a) > eps) && (n <= itmax)) {
- x = (1.0 / p) * ((x * (p - 1)) + (a / (pow(x, p - 1))));
- n = n + 1;
- }
- if (n > itmax) {
- printf("In %d nu a fost realizata aproximarea dorita", itmax);
- printf("\n");
- }
- else {
- printf("Aproximarea obtinuta este %lf", x);
- printf("\n");
- }
- }
- void main() {
- double a;
- int p;
- double x;
- double eps;
- int itmax;
- printf("Introduceti Aproximatia initiala: ");
- scanf_s("%lf", &x);
- printf("\n");
- printf("Precizia de eroare: ");
- scanf_s("%lf", &eps);
- printf("\n");
- printf(" Introduceti numarul de iteratii maxime: ");
- scanf_s("%d", &itmax);
- printf("\n");
- printf("Introduceti numarul real: ");
- scanf_s("%lf", &a);
- printf("\n");
- printf(" Introduceti ordinul radicalului: ");
- scanf_s("%d", &p);
- printf("\n");
- newton_radical(x, itmax, eps, p, a);
- _getch();
- }
- NEWTON 3=============================================================================
- #include <iostream>
- #include <math.h>
- #include<conio.h>
- #include<stdio.h>
- using namespace std;
- double fun(double x) {
- return((x*exp(x)) - 1);
- }
- void secanta(double x0, double x1, int itmax, double eps) {
- int n = 2;
- double x;
- while ((fabs(fun(x1) > eps) && (n <= itmax))) {
- x = x1 - ((fun(x1) * (x1 - x0)) / (fun(x1) - fun(x0)));
- x0 = x1;
- x1 = x;
- n = n + 1;
- }
- if (n > itmax) {
- printf("In %d nu a fost realizata aproximarea dorita", itmax);
- printf("\n");
- }
- else {
- printf("Aproximarea obtinuta este %lf", x);
- printf("\n");
- }
- }
- void main() {
- double x0, x1;
- double eps;
- int itmax;
- printf("Aproximatia initiala x0= ");
- scanf_s("%lf", &x0);
- printf("\n");
- printf("Aproximatia initiala x1= ");
- scanf_s("%lf", &x1);
- printf("\n");
- printf("Diferenta de eroare: ");
- scanf_s("%lf", &eps);
- printf("\n");
- printf("Numarul de iteratii maxime: ");
- scanf_s("%d", &itmax);
- printf("\n");
- secanta(x0, x1, itmax, eps);
- _getch();
- }
- NEWTON 4=============================================
- #include <iostream>
- #include <math.h>
- #include<conio.h>
- #include<stdio.h>
- using namespace std;
- double fun(double x) {
- return exp(-x);
- }
- void aprox_succes(double x, int itmax, double eps) {
- int n = 1;
- while ((fabs(x - fun(x)) > eps) && (n <= itmax)) {
- x = fun(x);
- n = n + 1;
- }
- if (n > itmax) {
- printf("In %d nu a fost realizata aproximarea dorita", itmax);
- printf("\n");
- }
- else {
- printf("Aproximarea obtinuta este %lf", x);
- printf("\n");
- printf("Numarul de iteratii efectuate: %d", n);
- }
- }
- void main() {
- double x;
- double eps;
- int itmax;
- printf("Aproximatia initiala x= ");
- scanf_s("%lf", &x);
- printf("\n");
- printf("Diferenta de eroare: ");
- scanf_s("%lf", &eps);
- printf("\n");
- printf("Numarul de iteratii maxime: ");
- scanf_s("%d", &itmax);
- printf("\n");
- aprox_succes(x, itmax, eps);
- _getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement