Sei sulla pagina 1di 10

#include <iostream>

#include <math.h>

using namespace std;

double pi = 3.1415926535897;

double f(double x)
{
return pow(x,3)*tan(x+2*pi)-pow(x,2)+2;
}

double g(double x)
{
return x*cos(exp(x)+1)-pow(x,3);
}

double h(double x)
{
return x*exp(x)+100*log(pow(x,2)-10)+5;
}

double biseccion(double a, double b, double (*f)(double x))


{
double tolerancia = 0.00000000001;
double r = (a+b)/2;
cout << "a \t b \t r \t f(r)"<< endl;
while (!(-tolerancia < f(r) && f(r) < tolerancia))
//while(b-a > tolerancia)
{
cout << a << "\t" << b << "\t" << r << "\t" << f(r) << endl;
if (f(a)*f(r) < 0)
b = r;
else
a = r;
r = (a+b)/2;
}
return r;
}

int main()
{
double r1 = biseccion(3,4,f);
cout << r1 << endl;
cout << f(r1) << endl << endl;
double r2 = biseccion(-2,1,g);
cout << r2 << endl;
cout << g(r2) << endl << endl;
double r3 = biseccion(3.22,3.23,h);
cout << r3 << endl;
cout << h(r3) << endl << endl;
return 0;
}

Falsa posicion

#include <iostream>
#include <math.h>

using namespace std;

double pi = 3.1415926535897;

double f(double x)
{
return x*x-2;
}

double falsapos(double a, double b, double (*f)(double x))


{
double tolerancia = 0.00000000001;
double r = a-f(a)*(b-a)/(f(b)-f(a));
cout << "a \t b \t r \t f(r)"<< endl;
while (!(-tolerancia < f(r) && f(r) < tolerancia))
{
cout << a << "\t" << b << "\t" << r << "\t" << f(r) << endl;
if (f(a)*f(r) < 0)
b = r;
else
a = r;
r = a-f(a)*(b-a)/(f(b)-f(a));
}
return r;
}

int main()
{
double r = falsapos(1,2,f);
cout << r << endl;
cout << f(r) << endl << endl;
return 0;
}

Metodo de newton

#include <iostream>
#include <math.h>

using namespace std;

double pi = 3.1415926535897;
double Derivada1(double (*f)(double x), double a, double dx)
{
double y0 = f(a);
double y = f(a + dx);
double m = (y-y0)/dx;
return m;
}

double Derivada2(double (*f)(double x), double a, double dx)


{
double y1 = f(a+dx);
double y2 = f(a-dx);
return (y1-y2)/2*dx;
}

double f(double x)
{
return pow(x,3)+3*pow(x,2)-5;
}

double df(double x)
{
return 3*pow(x,2)+6*x;
}

double g(double x)
{
return x-exp(cos(5*x-1))*atan(pow(x,3)+2*x-4);
}

double dg(double x)
{
return 1+5*exp(cos(5*x-1))*sin(-1+5*x)*atan(pow(x,3)+2*x-4)-exp(cos(5*x-
1))*(3*pow(x,2)+2)/(pow(x,6)+4*pow(x,4)+4*pow(x,2)-8*pow(x,3)-16*x+17);
}

double newton_raphson(double r, double (*f)(double x))


{
double tolerancia = 0.00000000001;
cout << "r \t f(r)"<< endl;
while (!(-tolerancia < f(r) && f(r) < tolerancia))
{
cout << r << "\t" << f(r) << endl;
r = r - f(r)/Derivada1(f,r,tolerancia);
}
return r;
}

double newton_raphson_d(double r, double (*f)(double x), double (*df)(double x))


{
double tolerancia = 0.00000000001;
cout << "r \t f(r)"<< endl;
while (!(-tolerancia < f(r) && f(r) < tolerancia))
{
cout << r << "\t" << f(r) << endl;
r = r - f(r)/df(r);
}
return r;
}

int main()
{
double r1 = newton_raphson(1, f);
cout << endl << "r =\t" << r1 << endl;
cout << "f(r) =\t"<< f(r1) << endl << endl;

double r2 = newton_raphson_d(1, f, df);


cout << endl << "r =\t" << r2 << endl;
cout << "f(r) =\t"<< f(r2) << endl << endl;

double r3 = newton_raphson(1.2, g);


cout << endl << "r =\t" << r3 << endl;
cout << "f(r) =\t"<< g(r3) << endl << endl;

double r4 = newton_raphson_d(1.2, g, dg);


cout << endl << "r =\t" << r4 << endl;
cout << "f(r) =\t"<< g(r4) << endl << endl;
return 0;
}

Metodo de secante

#include <iostream>
#include <math.h>
#include <cmath>

using namespace std;

double pi = 3.1415926535897;

double f(double x)
{
return pow(x,2)-2;
}

double secante(double r0, double r1, double (*f)(double x))


{
double r2 = r0 - f(r0)*(r0-r1)/(f(r0)-f(r1));
double tolerancia = 0.00000000001;
cout << "r0 \t r1 \t f(r)"<< endl;
while (!(-tolerancia < f(r2) && f(r2) < tolerancia))
{
cout << r0 << "\t" << r1 << "\t" << f(r2) << endl;
r2 = r0 - f(r0)*(r0-r1)/(f(r0)-f(r1));
if (abs(r2 - r1) < abs(r2 - r0)) r0 = r2;
else r1 = r2;
}
return r2;
}

int main()
{
double r = secante(1.5, 2, f);
cout << endl << "r =\t" << r << endl;
cout << "f(r) =\t"<< f(r) << endl;
return 0;
}

Metodo de punto fijo

#include <iostream>
#include <math.h>

using namespace std;

double pi = 3.1415926535897;

double f(double x)
{
return x*x-2;
}

double g(double x)
{
return (2-x*x)/10+x;
}

double puntofijo(double r, double (*f)(double x), double (*g)(double x))


{
double tolerancia = 0.00000000001;
cout << "r \t g(r) \t f(r)"<< endl;
while (!(-tolerancia < f(r) && f(r) < tolerancia))
//while (!(g(r)-tolerancia < r && r < g(r)+tolerancia)) //Mas rapido, menos preciso
{
cout << r << "\t" << g(r) << "\t" << f(r) << endl;
r = g(r);
}
return r;
}

int main()
{
double r = puntofijo(2,f,g);
cout << endl << "r = " << r << endl
<< "f(r) = " << f(r) << endl;
return 0;
}

Convergencia

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <cmath>

using namespace std;

double pi = 3.1415926535897;

double x_sucesion(double k)
{
return pow((1+1/k),k);
}

double y_sucesion(double n)
{
return (3*pow(n,4)+n-10)/(2*pow(n,4)-n+5);
}

double z_sucesion(double n)
{
return cos((n+pi)/((pow(n,2)+pi)));
}

double ordenConvergencia(double (*x)(double), double L, int K)


{
double beta;
cout << "k\tbeta" << endl;
for (int k=2; k <= K; k += 100)
{
beta = log(abs(x(k+1)-L)/abs(x(k)-L))/log(abs(x(k)-L)/abs(x(k-1)-L));
//cout << k << "\t" << beta << endl;
printf("%3.0f\t%2.10f\n",(double)k,beta);
}
return beta;
}

int main()
{
cout << ordenConvergencia(x_sucesion, exp(1), 10000) << endl << endl;
cout << ordenConvergencia(y_sucesion, 3/2, 10000) << endl << endl;
cout << ordenConvergencia(z_sucesion, 1, 10000) << endl << endl;
return 0;
}

jacobi

#include <iostream>
#include <vector>

using namespace std;

typedef vector<vector<double>> matriz;

void imprimirVector(vector<double> v)
{
for (int i=0; i <v.size(); i++)
cout << v[i] << " ";
cout << endl;
}

void mostrarMatriz(matriz A)
{
for (int i=0; i < A.size(); i++)
{
for (int j=0; j < A[i].size(); j++)
cout << A[i][j] << " ";
cout << endl;
}
}

vector<double> jacobi(matriz A, vector<double> b, vector<double> X0, int N)


{
int n = A.size();
vector<double> x;
cout << "0:\t"; imprimirVector(X0);
for(int m = 0; m < N; m++)
{
x = X0;
for(int i = 0; i<n; i++)
{
double s = 0;
for(int k = 0; k<n; k++)
{
s += A[i][k]*x[k];
}
X0[i] = (b[i] - s + A[i][i]*X0[i])/A[i][i];
}
cout << m+1 <<":\t"; imprimirVector(X0);
}
return X0;
}

int main()
{
matriz A = {{2, -1, 0},
{-1, 3, -1},
{0, -1, 2}};
vector<double> b = {1, 8, -5};
vector<double> X0 = {0, 0, 0};
vector<double> X = jacobi(A, b, X0, 20);
cout << endl <<"X:\t"; imprimirVector(X);
return 0;
}

Descompusion lu
#include <iostream>
#include <vector>
#include <math.h>
#include <algorithm>

using namespace std;

typedef vector<vector<double>> matriz;

void mostrarMatriz(matriz A)
{
for (int i=0; i < A.size(); i++)
{
for (int j=0; j < A[i].size(); j++)
cout << A[i][j] << " ";
cout << endl;
}
}

void imprimirVector(vector<double> v)
{
for (int i=0; i <v.size(); i++)
cout << v[i] << " ";
cout << endl;
}

vector<double> sust_regre(matriz A, vector<double> b)


{
int n = A.size();
vector<double> X(n, 0);
for (int i = n-1; i >= 0; i--)
{
double s = 0;
for (int j = i+1; j < n; j++)
s += A[i][j]*X[j];
X[i] = (b[i]-s)/A[i][i];
}
return X;
}

vector<double> sust_progre(matriz A, vector<double> b)


{
int n = A.size();
vector<double> X(n, 0);
for (int i = 0; i < n; i++)
{
double s = 0;
for (int j = 0; j < i; j++)
s += A[i][j]*X[j];
X[i] = (b[i]-s)/A[i][i];
}
return X;
}

vector<double> get_U(matriz &A)


{
vector<double> multiplicadores;
int n = A.size();
for (int j=0; j < n-1; j++)
{
for (int i = j+1; i < n; i++)
{
double m = - A[i][j]/A[j][j];
multiplicadores.push_back(-m);
for (int k = 0; k < A[i].size(); k++)
A[i][k] += m*A[j][k];
}
}
return multiplicadores;
}

matriz get_L(vector<double> multiplicadores, int n)


{
matriz A(n, vector<double>(n,0));
for (int i=0; i < n; i++)
for (int j=0; j < n; j++)
{
if (i==j)
A[i][j] = 1;
else if (i>j)
{
A[i][j] = multiplicadores.front();
multiplicadores.erase(multiplicadores.begin(), multiplicadores.begin()+1);
}
}
return A;
}

vector<matriz> descomposicion_LU(matriz A)
{
vector<matriz> LU;
int n = A.size();
vector<double> multiplicadores = get_U(A);
LU.push_back(get_L(multiplicadores, n));
LU.push_back(A);
return LU;
}

vector<double> calc_descomposicion_LU(matriz A, vector<double> b)


{
vector<matriz> LU = descomposicion_LU(A);
cout << "L:" << endl; mostrarMatriz(LU[0]); cout << endl;
cout << "U:" << endl; mostrarMatriz(LU[1]); cout << endl;
vector<double> Y = sust_progre(LU[0], b);
vector<double> X = sust_regre(LU[1], Y);
return X;
}

int main()
{
matriz A = {{3, -7, -2}, {-3, 5, 1}, {6, -4, 0}};
vector<double> vb = {-7, 5, 2};
cout << "A:" << endl; mostrarMatriz(A); cout << endl;
vector<double> X = calc_descomposicion_LU(A, vb);
cout << "X: ";
imprimirVector(X);
return 0;
}

Potrebbero piacerti anche