Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
#include <math.h>
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;
}
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>
double pi = 3.1415926535897;
double f(double x)
{
return x*x-2;
}
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>
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 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);
}
int main()
{
double r1 = newton_raphson(1, f);
cout << endl << "r =\t" << r1 << endl;
cout << "f(r) =\t"<< f(r1) << endl << endl;
Metodo de secante
#include <iostream>
#include <math.h>
#include <cmath>
double pi = 3.1415926535897;
double f(double x)
{
return pow(x,2)-2;
}
int main()
{
double r = secante(1.5, 2, f);
cout << endl << "r =\t" << r << endl;
cout << "f(r) =\t"<< f(r) << endl;
return 0;
}
#include <iostream>
#include <math.h>
double pi = 3.1415926535897;
double f(double x)
{
return x*x-2;
}
double g(double x)
{
return (2-x*x)/10+x;
}
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>
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)));
}
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>
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;
}
}
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>
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<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;
}
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;
}