Vous êtes sur la page 1sur 11

ELIMINACION DE GAUSS

fprintf(' ELIMINACION GAUSSIANA SIMPLE (SOLUCIÓN POR ETAPAS)\n\n\n');


A=input('Ingrese la matriz A = \n');
b=input('\nIngrese el vector b, correspondite a los terminos independientes
b=\n');
[n,m]=size(A);
C=[A,b];
fprintf('\nLa Matriz C, que corresponte a la matriz aumentada [Ab] es = \n');
disp(C);
if n==m
for k=1:(n-1)%instruccion iterativa que permite repetir pasos un numero
%determinado de veces
fprintf('\n ETAPA %g=\n\n',k)
fprintf('\nLa matriz correspondiente a esta etapa antes del
proceso:\n')
disp(C)
fprintf('\nLos Multiplicadores correpondientes a esta etapa son:\n')
for i=(k+1):n
m(i,k)=C(i,k)/C(k,k); %formula para hallar los multiplicadores
fprintf('\nm(%g,%g)=',i,k)
disp(m(i,k));
for j=k:(n+1)
C(i,j)= C(i,j) - m(i,k)*C(k,j); %formula de la nueva fila
end
end
fprintf('\nLa matriz correspondiente a esta etapa despues del
proceso:\n')
disp(C)
end
for i=n:-1:1
suma=0;
for p=(i+1):n
suma = suma + C(i,p)*X(p);
end
X(i)=(C(i,n+1)-suma)/C(i,i);
%formula de la susticion regresiva y solucion de las variables
end
else %funcion asignada del if, en caso de que este sea falso
fprintf('\nERROR: La matriz NO es cuadrada\n');
end
fprintf('\n\n\nSOLUCIÓN:\n');
fprintf('\n\nLa matriz Ab final:\n');
disp(C)
fprintf('\n\nLa solucion de X1 hasta Xn es:\n');
%a continuacion de utiliza una instruccion for, para mostrar el usuario,
%los resultados de una manera mas ordena
for i=1:n
Xi=X(1,i);
fprintf('\nX%g=',i)
disp(Xi);
end
GAUSS JORDAN

fprintf('Dame la matriz aumentada\n\n');


f=input('Cuantas filas tiene la matriz: ');
c=input('Cuantas columnas tiene la matriz: ');
for k=1:c
for j=1:f
fprintf('fila : %x\n',j)
fprintf('columna : %x',k)
r=input('Numero de esta fila y columna: ');
a(j,k)=r;
j=j+1;
end
k=k+1;
end
a

for k=1:c-1
a(k,:)=a(k,:)/a(k,k);
for j=k+1:f
a(j,:)=a(j,:)-a(k,:)*a(j,k);
j=j+1;
a
pause
end
k=k+1;
a

end
for k=f:-1:2
for j=k-1:-1:1
a(j,:)=a(j,:)-a(k,:)*a(j,k);
j=j-1;
a

end
k=k-1;
a

end
NEWTON RAPHSON NO LINEALES
function F=F(X)
x=X(1); y=X(2);
F=[x.^2+y-37 x-y.^2-5];

function JF=JF(X)
x=X(1); y=X(2);
JF=[2*x 1;1 -2*y];

function raphson(F,JF,X,es,iteracionesmaximas)
Y=feval(F,X);
for k=1:iteracionesmaximas
J=feval(JF,X);
XS=X-(inv(J)*Y')';
error1=abs((XS(1)-X(1))/XS(1))*100;
error2=abs((XS(2)-X(2))/XS(2))*100;
disp(['Iteracion',num2str(k)]);
disp('X(1) X(2)'); disp(XS);
disp('error1 error2');
disp([num2str(error1),') ' ,num2str(error2)]);
disp('----------------');
if(error1<=es)&&(error2<=es)
break
end
Y=feval(F,XS);
X=XS;
end

SUSTITUCIONES SUCESIVAS

function metodosucesivo()
syms x y
f(x,y)=input('ingrese la funcion despejada f: ');
g(x,y)=input('ingrese la funcion despejada g: ');
x=input('ingrese valor inicial x: ');
y=input('ingrese valor inicial y: ');
max=input('ingrese el maximo de iteraciones: ');
n=input('ingrese n: ');
Es=0.5*10^(2-n);
i=1;
while (i<=max)
a=x;
b=y;
x=f(x,y);
y=g(x,y);
Eax=abs((x-a)/x)*100;
Eay=abs((y-b)/y)*100;
fprintf('%d\t%1.7f\t%1.7f\t\t%f\t%f\n',i,x,y,Eax,Eay);

if(Eax<Es&&Eay<Es)
i=max+1;
end
i=i+1;
end
fprintf('x es:%1.7f y es:%1.7f con un error x de: %f con un error y de %f
\n',x,y,Eax,Eay);
end
GAUSS SEIDEL
function GS
clc
fprintf('\t\t\t\tMETODO GAUSS - SEIDEL 1/3\n');
disp('-------Para un sistema de 3 ecuaciones con 3 incognitas-------');
%lectura de las ecuaciones representadas en matrices, ejemplo: 1X+2Y+3Z=4 [1 2 3 4]
A=input('digite la matriz de la ecuación 1:\nEjemplo\n[a b c d]\n');
B=input('digite la matriz de la ecuación 2:\nEjemplo\n[a b c d]\n');
C=input('digite la matriz de la ecuación 3:\nEjemplo\n[a b c d]\n');
%terminos para evaluar
Y=input('DIGITE LA SUPOSICION PARA Y\n');
Z=input('DIGITE LA SUPOSICION PARA Z\n');
Tol=input('la tolerancia\n');
%despejar la ecuacion y evaluar en X Y Z
X1=(A(1,4)-A(1,2)*Y-A(1,3)*Z)/A(1,1)
Y1=(B(1,4)-B(1,1)*X1-B(1,3)*Z)/B(1,2)
Z1=(C(1,4)-C(1,1)*X1-A(1,2)*Y1)/C(1,3)
eaX=100;
eaY=100;
eaZ=100;
i=1;
while (eaX>Tol && eaY>Tol && eaZ>Tol)
X=X1;
Y=Y1;
Z=Z1;
X1=(A(1,4)-A(1,2)*Y-A(1,3)*Z)/A(1,1);
Y1=(B(1,4)-B(1,1)*X1-B(1,3)*Z)/B(1,2);
Z1=(C(1,4)-C(1,1)*X1-A(1,2)*Y1)/C(1,3);
eaX=abs((X1-X)/X1)*100;
eaY=abs((Y1-Y)/Y1)*100;
eaZ=abs((Z1-Z)/Z1)*100;
i=i+1;
end
fprintf('el resultado de X1=\n %.6f\n\n',X1);
fprintf('el resultado de Y1=\n %.6f\n\n',Y1);
fprintf('el resultado de Z1=\n %.6f\n\n',Z1);
fprintf('el error absoluto de X1=\n %.6f\n\n',eaX);
fprintf('el error absoluto de Y2=\n %.6f\n\n',eaY);
fprintf('el error absoluto de Z3=\n %.6f\n\n',eaZ);
fprintf('numero de iteraciones\n %f\n\n',i);
end

function gseidel(A,B,X,es,iteracionesmaximas)
for k=1:iteracionesmaximas
XS(1)=(B(1)-A(1,2)*X(2)-A(1,3)*X(3))/A(1,1);
XS(2)=(B(2)-A(2,1)*X(2)-A(2,3)*X(3))/A(2,2);
XS(3)=(B(3)-A(3,1)*X(2)-A(3,2)*X(2))/A(3,3);

error1=abs((XS(1)-X(1))/XS(1))*100;
error2=abs((XS(2)-X(2))/XS(2))*100;
error3=abs((XS(3)-X(3))/XS(3))*100;

disp(['Iteracion',num2str(k)]);
disp('X(1) X(2) X(3)'); disp(XS);
disp('error1 error2 error3');
disp([num2str(error1),' ' ,num2str(error2),' ' ,num2str(error3)]);
disp('----------------');
if (error1<=es)&&(error2<=es)&&(error3<=es)
break
end
X=XS;
end
Euler
function [t,w]=euleR1(f,a,b,ya,n)
h=(b-a)/n;
t=zeros(1,n+1);
w=zeros(1,n+1);
t(1)=a;
w(1)=ya;
for i=1:n
w(i+1)=w(i)+h*feval(f,t(i),w(i));
t(i+1)=a+h*i;
end
m=w';
disp(m);
end

fprintf('\n \tRESOLUCION DE ECUACIONES DIFERENCIALES POR MEDIO METODO DE


EULER\n')
f=input('\nIngrese la ecuacion diferencial de la forma: dy/dx=f(x,y)\n','s');
x0=input('\nIngrese el primer punto x0:\n');
x1=input('\nIngrese el segundo punto x1:\n');
y0=input('\nIngrese la condicion inicial y(x0):\n');
n=input('\nIngrese el numero de pasos n:\n');
h=(x1-x0)/n;
xs=x0:h:x1;
y1=y0;
fprintf('\n''it x0 x1 y1');
for i=1:n
it=i-1;
x0=xs(i);
x=x0;
x1=xs(i+1);
y=y0;
y1=y0+h*eval(f);
fprintf('\n%2.0f%10.6f%10.6f%10.6f\n',it,x0,x1,y1);
y0=y1;
end
fprintf('\n El punto aproximado y(x1) es = %10.6f\n',y1);
RK3
fprintf('\n \tRESOLUCION DE ECUACIONES DIFERENCIALES POR MEDIO RUNGE-KUTTA DE
ORDEN 3\n')
f=input('\n Ingrese la ecuacion diferencial\n','s');
x0=input('\n Ingrese el primer punto x0:\n');
x1=input('\n Ingrese el segundo punto x1:\n');
y0=input('\n Ingrese la condicion inicial y(x0):\n');
n=input('\n Ingrese el numero de pasos n:\n');
h=(x1-x0)/n;
xs=x0:h:x1;
fprintf('\n''it x0 y(x1)');
for i=1:n
it=i-1;
x0=xs(i);
x=x0;
y=y0;
k1=h*eval(f);
x=x0+h/2;
y=y0+k1/2;
k2=h*eval(f);
x=x0+h/2;
y=y0+k2/2;
k3=h*eval(f);
y0=y0+(k1+2*k2+k3)/6;
fprintf('\n%2.0f%10.6f%10.6f\n',it,x0,y0);
end
fprintf('\n El punto aproximado y(x1) es = %8.6f\n',y0);

HEUN

fprintf('\n \tRESOLUCION DE ECUACIONES DIFERENCIALES POR MEDIO HEUN\n')


f=input('\n Ingrese la ecuacion diferencial dy/dx=\n','s');
x0=input('\n Ingrese el primer punto x0:\n');
x1=input('\n Ingrese el segundo punto x1:\n');
y0=input('\n Ingrese la condicion inicial y(x0):\n');
n=input('\n Ingrese el numero de pasos n:\n');
h=(x1-x0)/n;
xs=x0:h:x1;
fprintf('\n''it x0 y(x1)');
for i=1:n
it=i-1;
x0=xs(i);

x=xs(i+1);
y=y0+k1;
k2=h*eval(f);
y0=y0+(k1+k2)/2;
fprintf('\n%2.0f%10.6f%10.6f\n',it,x0,y0);
end
fprintf('\n El punto aproximado y(x1) es = %8.6f\n',y0);
SISTEMA EDO
syms x y1 y2
f1=input('f1: ');
f2=input('f2: ');
f1(x,y1,y2)=f1;
f2(x,y1,y2)=f2;
n=input('numero de Ecuaciones: ');
Yi=input('valores iniciales Yi: ');
xi=input('valor inicial xi: ');
xf=input('valor final xf: ');
dx=input('tamaño de paso: ');
xout=input('intervalo de salida: ');
x=xi;
m=1;
Xp(m)=x;
fprintf('\n\n X \t\t\t Y1 \t\t\t Y2 \t\t\t Y1(real) \t\t Y2(real) \t\t
Error1 \t\t Error2\n');
for i=1:n
Yp(i,m)=Yi(i);
y(i)=Yi(i);
end

while x<xf
xend=x+xout;
if xend>xf;
xend=xf;
end
h=dx;
[x, y]=Integrator4(x,y,n,h,xend,f1,f2);
m=m+1;
Xp(m)=x;
for i=1:n
Yp(i,m)=y(i);
end
end
%inicia el programa
syms y1(x) y2(x)
[y1(x), y2(x)]=dsolve(diff(y1)==-y1/10,diff(y2)==(y1-y2)/10, y1(0)==10,
y2(0)==10);
x=[0:0.5:3];
Y1=y1(x)';
Y1=double(Y1);
Y2=y2(x)';
Y2=double(Y2);
m=length(x);
%
for k=1:length(Xp)
Er1(k)=Y1(k)-Yp(1,k);
Er2(k)=Y2(k)-Yp(2,k);

fprintf('\n\t%1.7f\t\t%1.7f\t\t%.7f\t\t%.7f\t\t%1.7f\t\t%1.7f\t\t%1.7f\n',Xp(
k),Yp(1,k),Yp(2,k),Y1(k),Y2(k),Er1(k),Er2(k));
end

function [x,y]=Integrator4(x,y,n,h,xend,f1,f2)
while x<xend
if (x-xend)<h
h=xend-x;
end
[x,y]=Rk4(x,y,n,h,f1,f2);
end
end

function [x,y]=Rk4(x,y,n,h,f1,f2)

[k1]=Derivs4(x,y,f1,f2);
for i=1:n
Ym(i)=y(i)+k1(i)*h/2;
end

[k2]=Derivs4(x+h/2,Ym,f1,f2);
for i=1:n
Ym(i)=y(i)+k2(i)*h/2;
end

[k3]=Derivs4(x+h/2,Ym,f1,f2);
for i=1:n
Ye(i)=y(i)+k3(i)*h;
end

[k4]=Derivs4(x+h,Ye,f1,f2);

slope=(k1+2*(k2+k3)+k4)/6;
y=y+slope*h;
x=x+h;
end

function [k]=Derivs4(x,y,f1,f2)
k(1)=f1(x,y(1),y(2));
k(2)=f2(x,y(1),y(2));
end

Integration
function trapecio ()
fprintf('\t\t\t\t\t "Cálculo de integrales por la regla del trapecio"
\t\t\t\t\t\n\n');
a=input('Ingrese el límite inferior (a): ');
b=input('Ingrese el límite superior (b): ');
n=input('Ingrese el número de intervalos (n): ');
h=(b-a)/n;
x=a:h:b;
f=funcion(x);
I=h*(sum(f)-0.5*(f(1)+f(length(f))));
fprintf('\n\n El valor de la integral es: \n I=%12.6f\n',I);
plot(x,f);
hold on;
stem(x,f);
x1=a:0.01:b;
y1=funcion(x1);
plot(x1,y1,'r');
xlabel('x');
ylabel('f(x)');
title('Regla del trapecio');
grid;

unction simpson13 ()
fprintf('\t\t\t\t\t "Cálculo de integrales por la regla de Simpson 1/3"
\t\t\t\t\t\n\n');
a=input('Ingrese el valor del límite inferior (a): ');
b=input('Ingrese el valor del límite superior (b): ');
n=input('Ingrese un número par de intervalos (n): ');
h=(b-a)/n;
x=a:h:b;
f=funcion(x);
if n>2
I=h/3*(f(1) + 4*sum(f(2:2:n+1))+ 2*sum(f(3:2:n-1)) + f(n+1) );
else
I=h/3*(f(1) + 4*f(2) + f(3));
end
fprintf('\n\n El valor de la integral es: \n I= %12.6f \n',I);
h2=(b-a)/100;
xc=a+(0:100)*h2;
fc=funcion(xc);
plot(xc,fc,'r');
hold on;
plot(x,f);
stem(x,f);

function simpson38
clc;
fprintf('\t\t\t\t\t "Cálculo de integrales por la regla de Simpson 1/3"
\t\t\t\t\t\n\n');
a=input('Ingrese el valor del límite inferior (a): ');
b=input('Ingrese el valor del límite superior (b): ');
n=3;
h=(b-a)/n;
x = a:h:b;
f = funcion(x);
I=3*h/8*(f(1) + 3*f(2) + 3*f(3) + f(4));
fprintf('\n\n El valor de la integral es: \n I= %12.6f\n',I);
h2=(b-a)/100;
xc = a+(0:100)*h2;
fc = funcion(xc);
plot(xc,fc,'r');
hold on;
plot(x,f);
stem(x,f);

function romberg ()
fprintf('\t\t\t\t\t "Integración de Romberg" \n\n');
funcion=input('Ingrese la función: \n f(x)= ','s');
a=input('Ingrese el valor del límite inferior (a): ');
b=input('Ingrese el valor del límite superior (b): ');
n=input('Ingrese un número par de intervalos (n): ');
h=(b-a);
M=1;
J=0;
R=zeros(n,n);
x=a;
f1=eval(funcion);
x=b;
f2=eval(funcion);
R(1,1)=h*(f1+f2)/2;
while (J<(n-1))
J=J+1;
h=h/2;
s=0;
for p=1:M
x=a+h*(2*p-1);
f3=eval(funcion);
s=s+f3;
end
R(J+1,1)=(1/2)*(R(J,1))+h*s;
M=2*M;
for k=1:J
R(J+1,k+1)=R(J+1,k)+(R(J+1,k)-R(J,k))/(4^k-1);
end
end
R;
fprintf('La aproximación buscada es: %10.15f\n\n',R(J+1,J+1));

Vous aimerez peut-être aussi