0% found this document useful (0 votes)
65 views8 pages

Codigos Python

The document contains code examples and explanations of several numerical methods for solving systems of linear equations, including: 1) Cramer's rule for solving systems of linear equations. 2) Gaussian elimination to solve systems of linear equations. 3) Computing the inverse of a matrix using Gaussian elimination. 4) LU decomposition to solve systems of linear equations, including with a vector b. 5) The method of Lagrange to solve systems of linear equations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views8 pages

Codigos Python

The document contains code examples and explanations of several numerical methods for solving systems of linear equations, including: 1) Cramer's rule for solving systems of linear equations. 2) Gaussian elimination to solve systems of linear equations. 3) Computing the inverse of a matrix using Gaussian elimination. 4) LU decomposition to solve systems of linear equations, including with a vector b. 5) The method of Lagrange to solve systems of linear equations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

#Armando Hernández Cortez - Regla de Cramer

import numpy as np

def cramer (A,b):


n=len(b)
D=np.linalg.det(A)
x=np.zeros(n)
for k in range(n):
Ak=A.copy()
Ak[:,k]=b
Dk=np.linalg.det(Ak)
x[k]=Dk/D
print('x',k+1,'=',round(x[k],7))

A=np.array([ [2,-1,1],[0,2,-1],[1,-1,0] ])
b=np.array([3,1,1])
cramer(A,b)

-----------------------------------------------------------------------------------
#Armando Hernández Cortez - Eliminación de Gauss
def mostrarMatriz(matriz,orden):
for i in range(0,orden):
linea = "| "
for j in range(orden+1):
linea += str(matriz[i][j])+" "
linea += "| "
print(linea)

matriz = [[1,2,2,1], [3,1,2,5], [4,3,3,8]]


#¡Orden de la matriz!
orden=len(matriz)
mostrarMatriz(matriz,orden);
for j in range(0,orden+1):
for i in range(0, orden):
if i>j:
division=matriz[i][j]/matriz[j][j]
for k in range(0, orden+1):
matriz[i][k]=matriz[i][k]-division*matriz[j][k];
x = [0,0,0]
for i in range(orden,0,-1):
suma=0
for j in range(i,orden):
suma=suma+matriz[i-1][j]*x[j]
x[i-1]=((matriz[i-1][orden]-suma)/matriz[i-1][i-1])
for i in range(0,orden):
print("x"+str(i)+" = "+str(x[i]))

----------------------------------------------------------------------------------
Matriz Inversa
#include <iostream>
#include <windows.h>

using namespace std;

COORD coord={0,0};
void gotoxy(int x, int y){
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

int main(){
short int i,j,k,n;

cout<<"Introduzca el numero de filas y columnas de la matriz nxn: ";


cin>>n;

float A[n][n], I[n][n], aux, pivote;

for(i=0; i<n; i++)


for (j=0; j<n; j++){
gotoxy(j*5,i+3);
cin>>A[i][j];
I[i][j]=0;

if(i==j)
I[i][j]=1;
}
//Reduccion por renglones

for(i=0; i<n; i++){

pivote= A[i][i];
for(k=0; k<n; k++){
A[i][k]= A[i][k]/pivote;
I[i][k]= I[i][k]/pivote;
}

for(j=0; j<n; j++){


if(i!=j){
aux= A[j][i];

for(k=0; k<n; k++){


A[j][k]= A[j][k]- aux * A[i][k];
I[j][k]= I[j][k]- aux * I[i][k];
}
}
}
}

for(i=0; i<n; i++)


for (j=0; j<n; j++){

gotoxy(j*10, i+10);
cout<<A[i][j];

gotoxy(30+j*10,i+10);
cout<<" "<<I[i][j];
}
return 0;
}
-----------------------------------------------------------------------------------
-------------------
DESCOMPOSICION LU
import numpy as np
print ("Descomposición LU, matrices cuadradas")
m=int(input("Introduce el número de renglones"))
matriz=np.zeros([m,m])
u=np.zeros([m,m])
l=np.zeroz([m,m])
print("Introduce los elementos de la matriz")
for r in range(0,m):
for c in range(0,m):
matriz[r,c]=(input("Elemento a["+str(r+1)+","+str(c+1)+"]"))
matriz[r,c]=float(matriz[r,c])
u[r,c]=matriz[r,c]
#Operaicones para hacer ceros debajo de la diagonal
for k in range(0,m):
for r in range(0,m):
if (k==r):
l[k,r]=1
if (k<r):
factor=(matriz[r,k]/matriz[k,k])
l[r,k]=factor
for c in range(0,m):
matriz[r,c]=matriz[r,c]-(factor*matriz[k,c])
u[r,c]=matriz[r,c]
print("Resultados")
print("Matriz L")
print(l)
print("Matriz U")
print(u)
-----------------------------------------------------------------------------------
-------------
DESCOMPOSICION LU CON UN VECTOR B
#include<iostream>
#include<stdio.h>

int main(){
int i=0,j=0,k=0,n=0;

std::cout << "\t\tDESCOMPOSICION LU";


std::cout << std::endl;
std::cout << std::endl;
std::cout << "El orden de la matriz debe ser mayor o igual que 2 y entero." <<
std::endl;
std::cout << "Ingresa el orden de la matriz: ";
std::cin >> n;

//Comprobacion de ingreso de un valor correcto para el orden de la matriz


if(n<=1){
std::cout << std::endl;
std::cout << "No ha ingresado un valor correcto. Cerrando el programa...";
std::cout << std::endl;
return 0;
} else {
float A[n][n]={0}, B[n]={0}, X[n]={0}, L[n][n]={0}, Y[n]={0}, U[n]
[n]={0},sum=0;
std::cout << "La matriz es de " << n << "x" << n <<".";
std::cout << std::endl << std::endl;

//Obtencion de los elementos de la matriz A de coeficientes


std::cout<<"Ingrese los coeficientes de la matriz A de coeficientes: ";
std::cout << std::endl;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
std::cout << "Ingrese el elemento A["<<i+1<<"]["<<j+1<<"]: ";
std::cin >> A[i][j];
}
}
std::cout << std::endl;

//Obtencion de los elementos de la matriz B


std::cout << "Ingrese la matriz B:" << std::endl;
for(i=0;i<n;i++){
std::cout << "Ingrese el elemento B["<<i+1<<"]: ";
std::cin >> B[i];
}

//Descomposicion LU

//Llenado de ceros y unos en posiciones conocidas


for(i=0;i<n;i++){
for(j=0;j<n;j++)
if(i>j){
U[i][j]=0; //Ceros debajo de la diagonal para la matriz U
}
else if(i==j){
L[i][j]=1; //Unos en la diagonal de L
} else{
L[i][j]=0; //Ceros encima de la diagonal para la matriz L
}
}

for(i=0;i<n;i++){
for(j=0;j<n;j++){
sum=0;
if(i<=j){ //Es decir, solo se llenan los elementos de la
diagonal y encima de ella para U
for(k=0;k<n;k++){
if(k!=i){
sum=sum+L[i][k]*U[k][j];
}else {
U[i][j]=A[i][j]-sum; //Llenado de elementos
restantes de U
}
}
} else{ //Es decir, solo se llenan los elementos debajo
de la diagonal para L
for(k=0;k<n;k++){
if(k!=j){
sum=sum+L[i][k]*U[k][j];
}else {
L[i][j]=(A[i][j]-sum)/U[j][j]; //Llenado de elementos
restantes de L
}
}
}
}
}

printf("\n\n L =");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)

printf ("%10.2f",L[i][j]);
printf("\n ");
}

printf("\n\n U =");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf ("%10.2f",U[i][j]);
printf("\n ");
}

Y[0]=B[0]/L[0][0];
for(i=1;i<n;i++)
{
sum=0;
for(j=0;j<i;j++)
{
sum=sum+Y[j]*L[i][j];

}
Y[i]=B[i]-sum;
}
printf("\n\n Y =");
for(i=0;i<n;i++)
{
printf ("%10.2f",Y[i]);
printf("\n ");
}

X[n-1]=Y[n-1]/U[n-1][n-1];
for(i=n-2;i>=0;i--){
sum=0;
for(j=n-1;j>i;j--)
sum=sum+X[j]*U[i][j];
X[i]=(Y[i]-sum)/U[i][i];
}

printf("\nLa solucion es :\n");


for(i=0;i<n;i++)
printf("\n x[%d] = %.2f ",i+1,X[i]);
printf("\n\n");
}
}
-----------------------------------------------------------------------------------
--
METODO DE LAGRANGE
#include<iostream>
#include<stdio.h>
int main(){
int i=0,j=0,k=0,n=0;

std::cout << "\t\tDESCOMPOSICION LU";


std::cout << std::endl;
std::cout << std::endl;
std::cout << "El orden de la matriz debe ser mayor o igual que 2 y entero." <<
std::endl;
std::cout << "Ingresa el orden de la matriz: ";
std::cin >> n;

//Comprobacion de ingreso de un valor correcto para el orden de la matriz


if(n<=1){
std::cout << std::endl;
std::cout << "No ha ingresado un valor correcto. Cerrando el programa...";
std::cout << std::endl;
return 0;
} else {
float A[n][n]={0}, B[n]={0}, X[n]={0}, L[n][n]={0}, Y[n]={0}, U[n]
[n]={0},sum=0;
std::cout << "La matriz es de " << n << "x" << n <<".";
std::cout << std::endl << std::endl;

//Obtencion de los elementos de la matriz A de coeficientes


std::cout<<"Ingrese los coeficientes de la matriz A de coeficientes: ";
std::cout << std::endl;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
std::cout << "Ingrese el elemento A["<<i+1<<"]["<<j+1<<"]: ";
std::cin >> A[i][j];
}
}
std::cout << std::endl;

//Obtencion de los elementos de la matriz B


std::cout << "Ingrese la matriz B:" << std::endl;
for(i=0;i<n;i++){
std::cout << "Ingrese el elemento B["<<i+1<<"]: ";
std::cin >> B[i];
}

//Descomposicion LU

//Llenado de ceros y unos en posiciones conocidas


for(i=0;i<n;i++){
for(j=0;j<n;j++)
if(i>j){
U[i][j]=0; //Ceros debajo de la diagonal para la matriz U
}
else if(i==j){
L[i][j]=1; //Unos en la diagonal de L
} else{
L[i][j]=0; //Ceros encima de la diagonal para la matriz L
}
}

for(i=0;i<n;i++){
for(j=0;j<n;j++){
sum=0;
if(i<=j){ //Es decir, solo se llenan los elementos de la
diagonal y encima de ella para U
for(k=0;k<n;k++){
if(k!=i){
sum=sum+L[i][k]*U[k][j];
}else {
U[i][j]=A[i][j]-sum; //Llenado de elementos
restantes de U
}
}
} else{ //Es decir, solo se llenan los elementos debajo
de la diagonal para L
for(k=0;k<n;k++){
if(k!=j){
sum=sum+L[i][k]*U[k][j];
}else {
L[i][j]=(A[i][j]-sum)/U[j][j]; //Llenado de elementos
restantes de L
}
}
}
}
}

printf("\n\n L =");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)

printf ("%10.2f",L[i][j]);
printf("\n ");
}

printf("\n\n U =");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf ("%10.2f",U[i][j]);
printf("\n ");
}

Y[0]=B[0]/L[0][0];
for(i=1;i<n;i++)
{
sum=0;
for(j=0;j<i;j++)
{
sum=sum+Y[j]*L[i][j];

}
Y[i]=B[i]-sum;
}
printf("\n\n Y =");
for(i=0;i<n;i++)
{
printf ("%10.2f",Y[i]);
printf("\n ");
}
X[n-1]=Y[n-1]/U[n-1][n-1];
for(i=n-2;i>=0;i--){
sum=0;
for(j=n-1;j>i;j--)
sum=sum+X[j]*U[i][j];
X[i]=(Y[i]-sum)/U[i][i];
}

printf("\nLa solucion es :\n");


for(i=0;i<n;i++)
printf("\n x[%d] = %.2f ",i+1,X[i]);
printf("\n\n");
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy