Está en la página 1de 6

Sistema de Ecuaciones Lineales

Metodo de Gauss-Sidel
clc
clear
disp('Metodo Gauss-sidel')
x1=0;
x2=0;
x3=0;
x1=((7.85 + 0.1*x2 + 0.2*x3)/3);
x2=((-19.3 - 0.1*x1 + 0.3*x3)/7);
x3=((71.4 - 0.3*x1 + 0.2*x2)/10);
error = 0.05;
xa1=x1;
xa2=x2;
xa3=x3;
e1=5;
e2=5;
e3=5;
i = 1;
while ((error < e1) || (error < e2) || (error < e3 ))
xa1=x1;
xa2=x2;
xa3=x3;
x1=((7.85 + 0.1*x2 + 0.2*x3)/3);
x2=((-19.3 - 0.1*x1 + 0.3*x3)/7);
x3=((71.4 - 0.3*x1 + 0.2*x2)/10);
e1=abs((x1-xa1)/x1)*100;
e2=abs((x2-xa2)/x2)*100;
e3=abs((x3-xa3)/x3)*100;
i=i+1;
fprintf('%0d \t %1f \t %3f \t %5f \t %8f \t %10f \t %12f
\n',i,x1,x2,x3,e1,e2,e3);
end
fprintf('cant. iteraciones: %f \n\n', i);
fprintf('x1 = %f \n\n', x1);
fprintf('x2 = %f \n\n', x2);
fprintf('x3 = %f \n\n', x3);
Metodo de Gauss-Jordan
clear all
clc
disp('Metodo Gauss-Jordan')
A=input('Ingrese la Matriz de Coeficientes: ');
b=input('Ingrese la Matriz de Terminos independientes: ');
A = [A b];% Se crea la matriz aumentada
n = size(A,1);
for i=1:n
A(i ,:) = A(i,:)/A(i, i );% Dividir renglon entre el pivote
for j=1:n
if i~=j
A(j ,:) = A(j,:)-A(i,:)*A(j, i );% Hacer ceros en la columna i
end
end
end
A(:,n+1)

Metodo de Eliminacion Gauss


clear
disp('Metodo de Eliminacion de Gauss')
a=input('INGRESE LA MATRIZ AUMENTADA a=');
[n,m]=size(a);
bol=0;
for i=1:n-1
for p=i:n
if a(p,i)~=0
break;
end
end
if p==n
disp('EL SISTEMA TIENE INFINITAS SOLUCIONES O NO EXISTE SOLUCION');
bol=1;
break;
end
if p~=i
disp('pivoteando')
aux=a(i,:);
a(i,:)=a(p,:);
a(p,:)=aux;
end
for j=i+1:n
m(j,i)=a(j,i)/a(i,i);
a(j,:)=a(j,:)-m(j,i)*a(i,:);
end
end
a
if bol==0
if a(n,n)==0 & a(n,n+1)==0
disp('EL SISTEMA TIENE INFINITAS SOLUCIONES');
fprintf('PROCEDIMIENTO TERMINADO SIN EXITO\n');
else if a(n,n)==0 & a(n,n+1)~=0
disp('EL SISTEMA NO TIENE SOLUCION');
fprintf('PROCEDIMIENTO TERMINADO SIN EXITO\n');
else
x(n)=a(n,n+1)/a(n,n);
for i=n-1:-1:1
s=0;
for j=i+1:n
s=s+a(i,j)*x(j);
end
x(i)=(a(i,n+1)-s)/a(i,i);
end
disp('la solucion aproximada del sistema es')
for i=1:n
fprintf('\n x(%1d)=%6.3f',i,x(i));
end
fprintf('\nPROCEDIMIENTO TERMINADO SATISFACTORIAMENTE\n');
end
end
else
fprintf('PROCEDIMIENTO TERMINADO SIN EXITO\n');
end

Ecuaciones No Lineales
Metodo de Sustituciones Sucesivas
clc
clear all
disp('Metodo Sustituciones Sucesivas')
x1=input('Valor inicial de x: ');
y1=input('Valor inicial de y: ');
error=input('Error especificado: ');
e1=100;
e2=100;
i=1;
while ((error < e1) || (error < e2))
x=x1;
y=y1;
f=y+(x^2)-.5;
g=(x^2)-5*x*y;
e1=abs((x-xa1)/x)*100;
e2=abs((y-ya1)/y)*100;
i=i+1;
end
fprintf('Iteraciones: %d\n', i);
fprintf('x = %f\t ea(x) %5.2f\n',x,e1);
fprintf('y = %f\t ea(y) %5.2f\n',y,e2);
Metodo de Newton

function [sol,iter,jac]=newtonsi(A,x0,es,itermax)
i=0;
jac=jacobian(A);
vars=findsym(A);
iter=[x0' 0 0];
e1=1;
e2=1;
while ((es < e1) || (es < e2)) && i<itermax
y0=x0;
fx0=subs(A,vars,x0);
dfx0=subs(jac,vars,x0);
X=dfx0\(-fx0);
x0=x0+X;
e=abs(((x0-y0)./x0).*100);
e1=e(1);
e2=e(2);
iter=[iter;x0' e1 e2];
i=i+1;
end
if i<itermax
sol=x0;
else
sol='No converge';
end

Ecuaciones Diferenciales
Metodo de Euler
clear all
clc
disp('Metodo de Euler - f(x,y)')
syms x y
f=input('Ingrese la funcion: ');
x1=input('Desde:');
x2=input('Hasta: ');
x0=input('Valor inicial [x]: ');
h=input('Tamao de paso h: ');
vars=findsym(f);
Y(1)=x0(2);
a=((x2-x0(1))/h)+1;
n=0;
for i=2:a
Y(i)=Y(i-1)+subs(f,vars,x0)*h;
x0=[x1+(h*(i-1)),Y(i)];
n=n+1;
fprintf('iteracion:%3i\n Valor siguiente:%5.4f\n',n,Y(i))
end
clear all
clc
disp('Metodo de Euler - f(x)')
syms x
f=input('Ingrese la funcion: ');
x1=input('Desde:');
x2=input('Hasta: ');
x0=input('Valor inicial [x]: ');
h=input('Tamao de paso h: ');
Y(1)=x0(2);
a=((x2-x1)/h)+1;
n=0;
for i=2:a
Y(i)=Y(i-1)+subs(f,x0(1))*h;
x0=[x1+(h*(i-1)),Y(i)];
n=n+1;
fprintf('iteracion:%3i\n Valor siguiente:%5.4f\n',n,Y(i))
end

Metodo de Heun
clear all
clc
disp('Metodo de Heun - f(x,y)')
syms x y
f=input('Ingrese la funcion: ');
x1=input('Desde:');
x2=input('Hasta: ');
x0=input('Valor inicial [x]: ');

h=input('Tamao de paso h: ');


es=input('Error especificado: ');
g=subs(f,x,x+1);
F=subs(g,y,y+h*f);
vars=findsym(f);
Y(1)=x0(2);
a=((x2-x0(1))/h)+1;
n=0;
ea=0;
for i=2:a
while ea<es
Y(i)=Y(i-1)+(subs(f,vars,x0)+subs(F,vars,x0))*h/2;
x0=[x1+(h*(i-1)),Y(i)];
n=n+1;
ea=(1-Y(i-1)/Y(i))*100;
end
fprintf('iteracion:%3i\n Valor siguiente:%5.4f\n',n,Y(i))
end
Metodo de Rugen Kutta
clear all
clc
disp('Metodo de RK4 - f(x)')
syms x
f=input('Ingrese la funcion: ');
x0=input('Valor inicial []: ');
x1=input('Desde: ');
y1=input('Hasta: ');
h=input('Tamao de paso: ');
K1=f;
K2=subs(f,x,x+h/2);
K4=subs(f,x,x+h);
k1=subs(K1,x0(1))
k2=subs(K2,x0(1))
k3=subs(K2,x0(1))
k4=subs(K4,x0(1))
a=((y1-x1)/h)+1;
Y(1)=x0(2);
n=0;
for i=2:a
Y(i)=Y(i-1)+(1/6)*(k1+2*k2+2*k3+k4)*h;
n=n+1;
fprintf('iteracion:%3i\n Valor siguiente:%5.4f\n',n,Y(i))
end
clear all
clc
disp('Metodo de RK4 - f(x,y)')
syms x y
f=input('Ingrese la funcion: ');
x0=input('Valor inicial []: ');
x1=input('Desde: ');
y1=input('Hasta: ');
h=input('Tamao de paso: ');
vars=findsym(f);
K1=f;

f1=subs(f,x,x+h/2);
f2=subs(f,x,x+h)
K2=subs(f1,y,y+K1*h/2);
K3=subs(f1,y,y+K2*h/2);
K4=subs(f2,y,y+K3*h);
k1=subs(K1,vars,x0)
k2=subs(K2,vars,x0)
k3=subs(K3,vars,x0)
k4=subs(K4,vars,x0)
a=((y1-x1)/h)+1;
Y(1)=x0(2);
n=0;
for i=2:a
Y(i)=Y(i-1)+(1/6)*(k1+2*k2+2*k3+k4)*h;
n=n+1;
fprintf('iteracion:%3i\n Valor siguiente:%5.4f\n',n,Y(i))
end

También podría gustarte