Вы находитесь на странице: 1из 11

“AÑO DEL BUEN SERVICIO AL CIUDADANO”

UNIVERSIDAD NACIONAL DE HUANCAVELICA


ESCUELA ACADÉMICA PROFESIONAL
DE INGENIERÍA CIVIL

CÁTEDRA : METODOS NUMERICOS


CATEDRÁTICO : IGN. SAMUEL VARGAS, Crispín
INTEGRANTES : LAPA GOMEZ, Alberto
: BENDEZU FLORES, Kaori
: ANCALLE AGUILAR, Marleny
: CANALES VILLA DENYS, Herminio
: ORTIZ ARROYO Edgar Alan
: CCANTO LAURENTE, Brahiam

CICLO : IV

HVCA – 2017
MÉTODOS NUMÉRICOS
1.-MÉTODO NEWTON RAPHSON

#include <QCoreApplication>
#include <iostream>
#include <iomanip> // setprecision
#include <cmath>

#define PRECISION 10
#define MAX_ITERACIONES 100
#define INTERVALOS 6

using namespace std;

void tabula(double a, double b, int intervalos);


// Muestra un # tabulado de intervalos
double f(double x); // Retorna el valor de la
función evaluada en x
double f_derivada(double x); // Retorna la derivada de
la función evaluada en x

int main(int argc, char *argv[])


{
QCoreApplication a(argc, argv);

double c;
double b;
double tolerancia; // Tolerancia
double x0; // Primera aproximación
double x1; // Siguiente aproximación
double error; // Diferencia entre dos
aproximaciones sucesivas: x1 - x0
int iteracion; // # de iteraciones
bool converge = true;

cout << setprecision(PRECISION); // Se


establece la precisión
cout << "\nCalculo de las raices de una funcion
aplicando el metodo de Newton - Raphson\n";
cout << "\nIngrese el intervalo inicial [a,b]:" <<
endl;

// Se ingresa el intervalo
cout << "\na = ";
cin >> c;

cout << "b = ";


cin >> b;

// Se tabulan los valores de f para INTERVALOS


intervalos
tabula(c, b, INTERVALOS);

// Se pide elegir una aproximación inicial


cout << "\nEscoja el punto inicial adecuado: x0
= ";
cin >> x0;

// Se pide ingresar la tolerancia


cout << "Tolerancia = ";
cin >> tolerancia;

// Iteraciones

// Se imprimen los valores de la primera


aproximación
cout << "\nAproximacion inicial:\n";
cout << "x0 = " << x0 << "\n"
<< "f(x0) = " << f(x0) << "\n"
<< "f'(x0) = " << f_derivada(x0) << endl;

iteracion = 1;
do {

if (iteracion > MAX_ITERACIONES) {


converge = false; // Se sobrepasó la máxima
cantidad de iteraciones permitidas
break;

} else {
x1 = x0 - f(x0) / f_derivada(x0); //
Cálculo de la siguiente aproximación
error = fabs(x1 - x0); // El error es la
diferencia entre dos aproximaciones sucesivas

// Se imprimen los valores de la siguiente


aproximación x1, f(x1), f_derivada(x1), error
cout << "\nIteracion #" << iteracion <<
endl;
cout << "x" << iteracion << " = " << x1 <<
"\n"
<< "f(x" << iteracion << ") = " << f(x1)
<< "\n"
<< "f'(x" << iteracion << ") = " <<
f_derivada(x1) << "\n"
<< "error = " << error << endl;

// La diferencia entre dos aproximaciones


sucesivas es también conocida como error.
// La condición de terminación consiste en
que que el error debe ser <= que la tolerancia dada
// Si se cumple la condición de
terminación, se ha encontrado la raiz aproximada
buscada.
if (error <= tolerancia) { // Condición de
terminación
converge = true;
break;

// Si no se cumple el criterio de
terminación, se pasa a la siguiente iteración
} else {
x0 = x1;
iteracion++;
}
}

} while (1);

// Respuesta final
if (converge) {
cout << "\n\nPara una tolerancia de " <<
tolerancia << " la raiz de f es: " << x1 << endl;

} else {
cout << "\n\nSe sobrepasó la máxima cantidad
de iteraciones permitidas" << endl;
}

cin.get();
cin.get();
return a.exec();
}
void tabula(double a, double b, int intervalos)
{
int puntos = intervalos + 1;

double ancho = (b - a) / intervalos;


cout << "\n\tx\t\tf(x) " << endl;
for (int i = 0; i < puntos; i++) {
cout << "\t" << a << "\t\t" << f(a) << endl;
a = a + ancho;
}
}

double f(double x)
{ double f;
f= x * exp(cos(x)) / 1.5 - 1;// funcion principal
return f;
}
double f_derivada(double x)
{double df;
df=exp(cos(x)) * (1 - x * sin(x)) / 1.5; //
derivada de la funcion principal
return df;
}

2.- Método del Punto Fijo

#include <QCoreApplication>
#include<QDebug>
#include"math.h"

float cFx(float x);


float DerivFx(float x);

int main(int argc, char *argv[])


{
QCoreApplication a(argc, argv);

float x0,Fx,dFx;
int N=40;

x0 = 0;

for (int i = 0; i < N; ++i) {


Fx= cFx(x0);
dFx=DerivFx(x0);
x0=dFx;

qDebug("el resultado :%.10f",x0);

}
return a.exec();
}

//creando la funcion principar


float cFx(float x){
float Fx;
Fx=0.5*sin(x)-x +1;
qDebug(" fx :%.10f",Fx);
return Fx;
}
//creando la derivada de funcion principar
float DerivFx(float x ){
float dFx;
dFx=0.5*sin(x)+1;
qDebug("gx:%.10f",dFx);
return dFx;
}
3.- Método de la Secante

#include <QCoreApplication>
#include<QDebug>
#include"math.h"
using namespace std;
float fun (float x);

int main(int argc, char *argv[])


{
QCoreApplication a(argc, argv);

float error=0.00001,// error maximo


x2, // la raiz de la ecuacio no lineal
Ea;// error

float x0=0,// primer valor


x1=4; // segundo valor

qDebug("Digite el primer valor:%.1f",x0);

qDebug("Digite el segundo valor:%.1f",x1);


Ea=error+1;

while (Ea>error){
x2=(x1-((x0-x1)/(fun(x0)-fun(x1))*fun(x1)));//
f0rmula del metodo secante
qDebug (":%.10f",x2);
Ea=(fabs (x1-x2));
x0=x1;
x1=x2;
}
qDebug ("la raiz es:%.10f",x2);
return a.exec();
}

//creando la funcion principar

float fun(float x)
{float f;
f=x*x*x+2*x*x+10*x-20; //funcion principal
return f;
}

4.- Método de Posición Falsa

#include <QCoreApplication>
#include <iostream>
#include <iomanip> // setprecision
#include <cmath> // fabs

#define PRECISION 6
#define INTERVALOS 10

using namespace std;

void tabula(double a, double b);


double f(double x);
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
cout << setprecision(PRECISION);
cout << "\nCalculo de las raices de una
funcion aplicando el metodo de la falsa posicion\n";
cout << "\nIngrese el intervalo inicial
[a,b]:" << endl;

double c, b, tolerancia;

cout << "\na = ";


cin >> c;

cout << "b = ";


cin >> b;
tabula(c, b);

cout << "\nEscoja el intervalo adecuado" <<


endl;
cout << "\na = ";
cin >> c;

cout << "b = ";


cin >> b;

double xr; // La solución aproximada


double xa = 0; // Solución anterior

if (f(c) * f(b) > 0) {


cout << "\nNo se puede aplicar el metodo
de la falsa posicion\n";
cout << "porque f(" << c << ") y f(" << b
<< ") tienen el mismo signo" << endl;

} else {
cout << "Tolerancia = ";
cin >> tolerancia;

cout <<
"\na\tb\tx\tf(a)\t\tf(b)\t\tf(x)\n" << endl;
do {
xr = b - f(b) * ((b - c) / (f(b) -
f(c)));

cout << c<< "\t" << b << "\t" << xr <<


"\t" << f(c) << "\t" << f(b) << "\t" << f(xr) << endl;

// if (fabs(f(xr)) <= tolerancia) {


if (fabs(xr - xa) / fabs(xr) <=
tolerancia) {
cout << "\n\nPara una tolerancia
de " << tolerancia << " la raiz de f es: " << xr <<
endl;
break;

} else {
xa = xr; // Se guarda el valor de
la aproximación anterior
if (f(xr) * f(c) > 0) {
c = xr;
} else if (f(xr) * f(b) > 0) {
b = xr;
}
}

} while (1);
}

cin.get();
cin.get();
return a.exec();
}

void tabula(double c, double b)


{
int puntos = INTERVALOS + 1;

double ancho = (b - c) / INTERVALOS;

cout << "\n\tx\tf(x) " << endl;


for (int i = 0; i < puntos; i++) {
cout << "\t" << c << "\t" << f(c) << endl;
c = c + ancho;
}
}

double f(double x)
{double f;
f= exp(-1 * x) - cos(x); // funcion principal
return f;
}
5.- Método de Bisección

#include <QCoreApplication>
#include <iostream>
#include <iomanip> // setprecision
#include <cmath>

#define PRECISION 6

using namespace std;

void tabula(float c, float b);


float f(float x);

int main(int argc, char *argv[])


{
QCoreApplication a(argc, argv);

cout << setprecision(PRECISION);


cout << "\nCalculo de las raices de una funcion
aplicando el metodo de la biseccion\n";
cout << "\nIngrese el intervalo inicial [a,b]:"
<< endl;
float c, b, tolerancia;
cout << "\na = ";
cin >> c;

cout << "b = ";


cin >> b;

tabula(c, b);

cout << "\nEscoja el intervalo adecuado" << endl;


cout << "\na = ";
cin >> c;

cout << "b = ";


cin >> b;

float xr;

if (f(c) * f(b) > 0) {


cout << "\nNo se puede aplicar el metodo de la
biseccion\n";
cout << "porque f(" << c << ") y f(" << b <<
") tienen el mismo signo" << endl;

} else {
cout << "Tolerancia = ";
cin >> tolerancia;

cout << "\na\tb\tx\tf(c)\t\tf(b)\t\tf(x)\n" <<


endl;
do {

xr = (c + b) / 2.0;
cout << c << "\t" << b << "\t" << xr <<
"\t" << f(c) << "\t" << f(b) << "\t" << f(xr) << endl;

if (fabs(f(xr)) <= tolerancia) {


cout << "\n\nPara una tolerancia de " <<
tolerancia << " la raiz de f es: " << xr << endl;
break;
} else {
if (f(xr) * f(c) > 0) {
c = xr;
} else if (f(xr) * f(b) > 0) {
b = xr;
}
}

} while (1);
}

cin.get();
cin.get();
return a.exec();
}
#define INTERVALOS 10
void tabula(float c, float b)
{
int puntos = INTERVALOS + 1;

float ancho = (b - c) / INTERVALOS;

cout << "\n\tx\tf(x) " << endl;


for (int i = 0; i < puntos; i++) {
cout << "\t" << c << "\t" << f(c) << endl;
c = c+ ancho;
}
}

float f(float x){


float f;
f=exp(-1 * x) - cos(3 * x) - 0.5;
return f;
}

Вам также может понравиться