0% found this document useful (0 votes)
34 views7 pages

Metode Numerice

This document is a laboratory work on numerical methods for solving nonlinear equations using bisection, chord, iteration, and Newton's methods. It presents the implementation of each method in C++ to solve two example equations: x^3 + 12x + 5 = 0 and 12sin(x) + x = 1. For each method, the code includes definitions of the functions, initialization of variables, termination criteria, and output of the number of iterations and solution.

Uploaded by

OlegOxid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views7 pages

Metode Numerice

This document is a laboratory work on numerical methods for solving nonlinear equations using bisection, chord, iteration, and Newton's methods. It presents the implementation of each method in C++ to solve two example equations: x^3 + 12x + 5 = 0 and 12sin(x) + x = 1. For each method, the code includes definitions of the functions, initialization of variables, termination criteria, and output of the number of iterations and solution.

Uploaded by

OlegOxid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Universitatea de Stat din Moldova

Facultatea de Fizică si Inginerie


Departamentul Fizica Aplicată si Informatica

Lucrare de laborator nr.1

Tema: „Metode umerice de rezolvare a


ecuatiilor neliniare prin metoda : bisectiei, coardei,
iteratiei si Newton”

Lucrarea a fost elaborată de


Barbos Oleg, gr. 2.2 TI

Lucrarea a fost verificată de


Curlicovschi A.

Chisinau 2019
Ecuații propuse spre rezolvare:

23) x3+12x+5=0

53) 12sin(x)+x=1

Metodele au fost realizate in limbajul de programare C++ :


Metoda bisectiei:

#include<iostream>
#include<math.h>
#define EPS 10e-7
using namespace std;
double f(double x){ return x * x * x + 12.0 * x + 5.0; }
//double f(double x){ return 0.5 * sin(x) + x - 1.0; }
int main()
{
int i = 0;
double a, b, c;
cout<<"a : "; cin>>a;
cout<<"b : "; cin>>b;
if(f(a)*f(b) < 0)
{
c = a;
while(fabs(b - a) >= EPS)
{
c = (b + a)/ 2;
if(f(c) == 0.0) break;
(f(a) * f(c) < 0) ? b = c : a = c;
i++;
}
cout<<"Iter : "<<i<<endl; cout<<"Result : "<<c;
}
else
cout<<"Error! No roots in this interval"<<endl;
return 0;
}
Metoda coardelor:

#include<iostream>
#include<math.h>
#define EPS 10e-7
#define MAXITER 200
using namespace std;
double f(double x){ return x * x * x + 12.0 * x + 5.0; }
int main()
{
double a, b, c;
int i = 0;
cout<<"a : "; cin>>a;
cout<<"b : "; cin>>b;
if (f(a) * f(b) < 0 )
{
do {
c = a - f(a) * ((b - a)/(f(b) - f(a)));
f(a) * f(c) < 0 ? b = c : a = c;
i++;
} while(fabs(f(c)) >= EPS && i < MAXITER);
cout<<"Iter : "<<i<<endl; cout<<"Result : "<<c;
}
else
cout<<"Error! No roots in this interval"<<endl;
return 0;
}
Metoda iterative:

#include<iostream>
#include<math.h>
#define EPS 10e-7
using namespace std;
// double f(double x){ return x * x * x + 12.0 * x + 5.0; }
// double fi(double x){ return (- pow(x, 3) - 5.0) / 12; }
double f(double x){ return 0.5 * sin(x) + x - 1.0; }
double fi(double x){ return -0.5* sin(x) + 1.0; }
int main()
{
double x0, res, a, b;
int i = 0;
cout<<"a : "; cin>>a;
cout<<"b : "; cin>>b;
cout<<"x0 : "; cin>>x0;
if(f(a)*f(b) < 0)
{
do {
res = x0;
x0 = fi(res);
i++;
} while(fabs(x0 - res) >= EPS);
cout<<"Iter : "<<i<<endl; cout<<"Result : "<<res;
}
else
cout<<"Error! No roots in this interval"<<endl;
return 0;
}
Metoda Newton :

#include<iostream>
#include<math.h>
#define EPS 10e-7
using namespace std;
// double f(double x){ return x * x * x + 12.0 * x + 5.0; }
// double fp(double x){ return 3 * x * x + 12.0; }
double f(double x){ return 0.5 * sin(x) + x - 1.0; }
double fp(double x){ return 0.5 * cos(x) + 1.0; }

int main()
{
int i = 0;
double a, b, c;
cout<<"a : "; cin>>a;
cout<<"b : "; cin>>b;
(f(a)*fp(a) < 0) ? c = a : c = b;
do { c = c - f(c) / fp(c); i++; } while(fabs(f(c)) >= EPS);
cout<<"Iter : "<<i<<endl; cout<<"Result : "<<c;
return 0;
}

Metoda Newton metoda 2 :

#include<iostream>
#include<math.h>
#define EPS 10e-4
using namespace std;
double f(double x){ return x * x * x + 12.0 * x + 5.0; }
double fp(double x){ return 3.0 * x * x + 12.0; }
double fp2(double x){ return 6.0 * x;}
int main()
{
int i = 0;
double a, b, c;
cout<<"a : "; cin>>a;
cout<<"b : "; cin>>b;
(fp(a)*fp(a) < 0) ? c = a : c = b;
do { c = c - fp(c) / fp2(c); i++; } while(fabs(f(c)) >= EPS);
cout<<"Iter : "<<i<<endl; cout<<"Result : "<<c;
return 0;
}

You might also like