“FACULTAD DE C.C.Q.Q”
PROGRAMAS EN MATLAB
Programas Finales
1. REGLA DEL TRAPECIO
2. 1/3 SE SIMPSON
3. DIFERENCIACION NUMERICA
df=[f(x+h)-f(x)]/h;
fprintf(' x = %4.4f\n',df);
e=((x-df)/(df))*100;
fprintf('El error en porcentaje es= \n');
disp(e)
% Aproximacion por diferncias hacia Atras
case 2
df=[f(x)-f(x-h)]/h;
fprintf(' x = %4.4f\n',df);
e=((x-df)/(df))*100;
fprintf('El error en porcentaje es= \n');
disp(e)
% Aproximacion por diferncias Central
case 3
df=[f(x+h)-f(x-h)]/(2*h);
fprintf(' x = %4.4f\n',df);
e=((x-df)/(df))*100;
UNIVERSIDAD DE CUENCA
“FACULTAD DE C.C.Q.Q”
df=[3*f(x)-4*f(x-h)+f(x-2*h)]/(2*h);
fprintf(' x = %4.4f\n',df);
e=((x-df)/(df))*100;
fprintf('El error en porcentaje es= \n');
disp(e)
% Aproximacion por diferncias hacia Adelante para la segunda
derivada
case 6
df=[f(x)-2*f(x+h)+f(x+2*h)]/(h^2);
fprintf(' x = %4.4f\n',df);
e=((x-df)/(df))*100;
fprintf('El error en porcentaje es= \n');
disp(e)
% Aproximacion por diferncias hacia Atras para la segunda
derivada
case 7
df=[f(x)-2*f(x-h)+f(x-2*h)]/(h^2);
fprintf(' x = %4.4f\n',df);
e=((x-df)/(df))*100;
fprintf('El error en porcentaje es= \n');
disp(e)
% Aproximacion por diferncias Central para la segunda derivada
case 8
df=[f(x+h)-2*f(x)+f(x-h)]/(h^2);
fprintf(' x = %4.4f\n',df);
e=((x-df)/(df))*100;
fprintf('El error en porcentaje es= \n');
disp(e)
end
else
disp('ingrese un valor entre 1 y 8')
DiferenciacionNumerica
end
4. METODO DE GAUSS
for j=k:(n+1)
C(i,j)= C(i,j) - m(i,k)*C(k,j); %formula de la nueva
fila
end
end
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\nLa solucion :\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
5. INVERSA DE LA MATRIZ
%INVERSA DE LA MATRIZ
clc
clear all
fprintf(' INVERSA DE UNA MATRIZ \n');
UNIVERSIDAD DE CUENCA
“FACULTAD DE C.C.Q.Q”
for k=1:n
for j=1:m
B(1,j)=A(1,j)/A(1,k);
end
for i=2:n
for j=1:m
B(i,j)=B(1,j)*A(i,k);
end
end
for i=1:(n-1)
for j=1:m
A(i,j)=A((i+1),j)-B((i+1),j);
end
end
for j=1:m
A(n,j)=B(1,j);
end
end
C=zeros(n);
a=0;
for i=1:n
a=a+1;
b=0;
for j=(n+1):m
b=b+1;
C(a,b)=A(i,j);
end
end
fprintf('La inversa de la matriz es: \n')
disp(C)
6. LU
%METODO LU
clc
clear
format long
fprintf(' METODO LU: \n');
A=input ('Ingrese la matriz: \n ');
b=input ('\n Ingrese el vector de terminos independientes:\n');
[n,m]=size(A);
C=[A,b];
fprintf ('\nSu matriz aumentada C= \n');
disp(C)
if n==m
for k=1:n
L(k,k)=1;
suma=0;
for p=1:k-1
suma=suma+L(k,p)*u(p,k);
end
u(k,k)=(A(k,k)-suma);
for i=k+1:n
suma=0;
for r=1:k-1
suma=suma+L(i,r)*u(r,k);
end
L(i,k)=(A(i,k)-suma)/u(k,k);
end
for j=k+1:n
suma=0;
for s=1:k-1
suma=suma+L(k,s)*u(s,j);
end
u(k,j)=(A(k,j)-suma);
end
end
memoriau=1;
memoriaL=1;
for i=1:n
memoriau=memoriau*u(i,i);
end
producto=memoriaL*memoriau;
if producto~=0
for i=1:n
suma=0;
for p=1:i-1
suma=suma+L(i,p)*z(p);
end
z(i)=(b(i)-suma)/L(i,i);
end
for i=n:-1:1
suma=0;
for p=(i+1):n
suma=suma+u(i,p)*x(p);
end
x(i)=(z(i)-suma)/u(i,i);
end
else
fprintf('\nEl determinante es igual a cero, por lo tanto el
sistema tiene infinita o ninguna solucion\n')
end
UNIVERSIDAD DE CUENCA
“FACULTAD DE C.C.Q.Q”
end