Sei sulla pagina 1di 4

Método de Crout

function respuesta = Metodo_Crout( A,b )


n=length(b);
boo=1;
for i=1:n
d=A(1:i,1:i);
if det(d)==0
boo=0;
end
end
B=cat(2,A,b);
U=eye(n);
L=zeros(n);
c=zeros(n,1);
if boo==0
x=strcat('No se puede aplicar este método porque hay un determinante igual
a cero');
warndlg('No se puede aplicar este método porque hay un determinante igual
a cero');
elseif (rank(A)~=rank(B))
x=strcar('Hay un numero infinito de soluciones');
warndlg('Hay un numero infinito de soluciones');
else
L(:,1)=A(:,1);
U(1,2)=A(1,2)/L(1,1);
for i=2:n
L(i:n,i)=A(i:n,i)-L(i:n,1:(i-1))*U(1:(i-1),i);
if i~=n
for j=1:i
U(j,i+1)=(A(j,i+1)-L(j,1:(j-1))*U(1:j-1,i+1))/L(j,j);
end
end
end
c(1)=b(1)/L(1,1);
for i=2:n
c(i)=(b(i)-L(i,1:i-1)*c(1:i-1))/L(i,i);
end

x=zeros(n,1);
x(n)=c(n);
for i=n-1:-1:1
x(i)=c(i)-U(i,i+1:n)*x(i+1:n);
end
end
respuesta=struct('L',L,'U',U,'c',c,'x',x,'boo',boo);
end

Pantalla Crout
function varargout = Pantalla_Crout(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Pantalla_Crout_OpeningFcn, ...
'gui_OutputFcn', @Pantalla_Crout_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function Pantalla_Crout_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = Pantalla_Crout_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function Dir_A_Callback(hObject, eventdata, handles)
[filename,pathname]=uigetfile({'*.txt','File Selector'});
fullpathname=strcat(pathname,filename);
set(handles.E_Dir_A,'String',fullpathname);
function E_Dir_A_Callback(hObject, eventdata, handles)
function E_Dir_A_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function Dir_b_Callback(hObject, eventdata, handles)
[filename,pathname]=uigetfile({'*.txt','File Selector'});
fullpathname=strcat(pathname,filename);
set(handles.E_Dir_b,'String',fullpathname);
function E_Dir_b_Callback(hObject, eventdata, handles)
function E_Dir_b_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function Dir_x_Callback(hObject, eventdata, handles)
[file,path]=uiputfile('*.txt','Guardar X como');
fullpathname=strcat(path,file);
set(handles.E_Dir_x,'String',fullpathname);
function E_Dir_x_Callback(hObject, eventdata, handles)
function E_Dir_x_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function Dir_L_Callback(hObject, eventdata, handles)
[file,path]=uiputfile('*.txt','Guardar X como');
fullpathname=strcat(path,file);
set(handles.E_Dir_L,'String',fullpathname);
function E_Dir_L_Callback(hObject, eventdata, handles)
function E_Dir_L_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function Dir_U_Callback(hObject, eventdata, handles)
[file,path]=uiputfile('*.txt','Guardar X como');
fullpathname=strcat(path,file);
set(handles.E_Dir_U,'String',fullpathname);
function E_Dir_U_Callback(hObject, eventdata, handles)
function E_Dir_U_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function Calcular_Callback(hObject, eventdata, handles)
miArchivo1=get(handles.E_Dir_A,'String');
miArchivo2=get(handles.E_Dir_b,'String');
A=importdata(miArchivo1);
b=importdata(miArchivo2);
R=Metodo_Crout(A,b);
set(handles.Tabla_A,'data',A);
set(handles.Tabla_b,'data',b);
set(handles.Tabla_x,'data',R.x);
set(handles.Tabla_L,'data',R.L);
set(handles.Tabla_U,'data',R.U);
set(handles.Tabla_c,'data',R.c);
txt=get(handles.E_Dir_x,'string');
Guardar_Matriz(R.x,txt);
txt=get(handles.E_Dir_L,'string');
Guardar_Matriz(R.L,txt);
txt=get(handles.E_Dir_U,'string');
Guardar_Matriz(R.U,txt);
txt=get(handles.E_Dir_c,'string');
Guardar_Matriz(R.c,txt);
n=length(R.x);
cla()
axes(handles.Grafica);
title(strcat('Solucion de un sistema de ecuaciones de
',num2str(n),'x',num2str(n)));
switch n
case 2
Grafica_2_Ecuaciones(A,b,R.x);
drawnow
case 3
Grafica_3_Ecuaciones(A,b,R.x);
drawnow
case 4
Grafica_4_Ecuaciones(A,b,R.x);
drawnow
otherwise
warndlg('Grafica no disponible','¡Advertencia, el numero de ecuaciones
no es posible de graficar');
end
hold off
function Regresar_Callback(hObject, eventdata, handles)
close;
Pantalla_Sistema_Ecuaciones;
function Dir_c_Callback(hObject, eventdata, handles)
[file,path]=uiputfile('*.txt','Guardar X como');
fullpathname=strcat(path,file);
set(handles.E_Dir_c,'String',fullpathname);
function E_Dir_c_Callback(hObject, eventdata, handles)
function E_Dir_c_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

Potrebbero piacerti anche