Demo entry 6688044

Exp3.1

   

Submitted by anonymous on Jan 01, 2018 at 15:58
Language: Matlab. Code size: 13.1 kB.

function varargout = Exp3_1(varargin)
% EXP3_1 MATLAB code for Exp3_1.fig
%      EXP3_1, by itself, creates a new EXP3_1 or raises the existing
%      singleton*.
%
%      H = EXP3_1 returns the handle to a new EXP3_1 or the handle to
%      the existing singleton*.
%
%      EXP3_1('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in EXP3_1.M with the given input arguments.
%
%      EXP3_1('Property','Value',...) creates a new EXP3_1 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Exp3_1_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Exp3_1_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Exp3_1

% Last Modified by GUIDE v2.5 22-Dec-2017 19:56:33

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Exp3_1_OpeningFcn, ...
                   'gui_OutputFcn',  @Exp3_1_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
% End initialization code - DO NOT EDIT


% --- Executes just before Exp3_1 is made visible.
function Exp3_1_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Exp3_1 (see VARARGIN)

% Choose default command line output for Exp3_1
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Exp3_1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);

global isAuto;
global outputNumber;
global isDiminished;
isAuto = 1;
outputNumber = 1;
isDiminished = 0;
set(handles.uitable2, 'Data', '');


% --- Outputs from this function are returned to the command line.
function varargout = Exp3_1_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

% 生成矩阵Button回调函数
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global originA;
global b;
global A;
global currentColumn;
global matsize;
global isAuto;
global accuracy;
n = round(str2double(get(handles.edit1, 'String')));
matsize = n;
currentColumn = 1;
if (n < 2)
    MyOutput(handles.outputEdit, '矩阵规模应至少为2!');
    return;
end

accuracy = round(str2double(get(handles.edit3, 'String')));
if (accuracy < 0) || (accuracy > 18)
    MyOutput(handles.outputEdit, '精度应介于0~18位之间!');
    return;
end

% 输入矩阵
set(handles.edit1, 'Enable', 'off');
set(handles.edit3, 'Enable', 'off');
set(handles.radiobutton5, 'Enable', 'off');
set(handles.radiobutton6, 'Enable', 'off');
set(hObject, 'Enable', 'off');

% 生成题目要求的方程组
temp = [zeros(1,n); eye(n-1,n)];
originA = 6 * eye(n) + 8 * temp + temp';
b = [7; 15*ones(n-2,1); 14];
A = [originA, b];

% 随机生成解全为1的方程组时,使用该段
% rng(s);
% originA = rand(n);
% b = originA*ones(n,1);
% A = round([originA, b], accuracy);

set(handles.uitable2, 'Data', A);
addstr = ['矩阵生成成功,矩阵阶数:', num2str(n), ',输入矩阵的条件数为:', num2str(cond(originA))];
MyOutput(handles.outputEdit, addstr);
MyOutput(handles.outputEdit, ['当前执行 i = ', num2str(currentColumn)]);

% 判断首步是否可以继续
if (max(abs(A(1:matsize,1))) == 0)
    MyOutput(handles.outputEdit,'行列式为0,求解终止');
    return;
end

% 提示主元选取
if isAuto
    MyOutput(handles.outputEdit,'请选择单步运行或自动运行');
    set(handles.pushbutton2, 'Enable', 'on');
    set(handles.pushbutton3, 'Enable', 'on');
    set(handles.popupmenu1, 'Enable', 'off');
    set(handles.popupmenu1, 'String', '自动选取');
else
    MyOutput(handles.outputEdit,'请在列表中选择主元,并点击单步操作');
    set(handles.pushbutton2, 'Enable', 'on');
    set(handles.popupmenu1, 'Enable', 'on');
    str = {};
    for i = currentColumn:matsize
        if (A(i,currentColumn) ~= 0)
            str{length(str)+1, 1} = ['a(', num2str(i), ',', num2str(currentColumn), ') = ', num2str(A(i,currentColumn))];
        end
    end
    set(handles.popupmenu1, 'String', str);
end


% 单步操作Button回调函数
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global accuracy;
global isDiminished;
global isAuto;
global matsize;
global A;
global currentColumn;

% 回代
if isDiminished
    for i = matsize:-1:1
        for j = matsize:-1:i+1
            A(i,:) = A(i,:) - A(i,j)*A(j,:);
        end
        A(i,:) = A(i,:) / A(i,i);
        A = round(A, accuracy);
    end
    set(handles.uitable2, 'Data', A);
    MyOutput(handles.outputEdit, '回代结束,矩阵已完成求解');
    set(handles.pushbutton2, 'Enable', 'off');
    set(handles.pushbutton3, 'Enable', 'off');
    set(handles.pushbutton4, 'Enable', 'on');
    return;
end

if isAuto
    % 自动获取绝对值最大的元素
    currentRow = find(abs(A(currentColumn:matsize,currentColumn)) == max(abs(A(currentColumn:matsize,currentColumn))));
    currentRow = currentRow(1) + currentColumn - 1;
else
    % 手动获取元素
    index = get(handles.popupmenu1, 'Value');
    currentRow = currentColumn - 1;
    while index > 0
        currentRow = currentRow + 1;
        if (A(currentRow, currentColumn) ~= 0)
            index = index - 1;
        end
    end
end

MyOutput(handles.outputEdit, ['选取的主元为: a(', num2str(currentRow), ',', num2str(currentColumn), ') = ', num2str(A(currentRow, currentColumn))]);

% 交换
temp = A(currentColumn, :);
A(currentColumn, :) = A(currentRow, :);
A(currentRow, :) = temp;

% 消元
for i = currentColumn+1:matsize
    l = A(i, currentColumn) / A(currentColumn, currentColumn);
    if l ~= 0
        for j = currentColumn:matsize+1
            A(i,j) = A(i,j) - l*A(currentColumn,j);
        end
    end
end

% 近似与刷新
A = round(A, accuracy);
set(handles.uitable2, 'Data', A);
if (currentColumn < matsize - 1)
    currentColumn = currentColumn + 1;
    MyOutput(handles.outputEdit, ['矩阵已刷新,当前执行 i = ', num2str(currentColumn)]);
    
    % 判断是否可以继续
    if (max(abs(A(currentColumn:matsize,currentColumn))) == 0)
        MyOutput(handles.outputEdit,'行列式为0,求解终止');
        set(handles.pushbutton2, 'Enable', 'off');
        set(handles.pushbutton3, 'Enable', 'off');
        return;
    end
    
    % 提示主元选取
    if isAuto
        MyOutput(handles.outputEdit,'请选择单步运行或自动运行');
    else
        MyOutput(handles.outputEdit,'请在列表中选择主元,并点击单步操作');
        str = {};
        for i = currentColumn:matsize
            if (A(i,currentColumn) ~= 0)
                str{length(str)+1, 1} = ['a(', num2str(i), ',', num2str(currentColumn), ') = ', num2str(A(i,currentColumn))];
            end
        end
        if (get(handles.popupmenu1, 'Value') > length(str))
            set(handles.popupmenu1, 'Value', 1);
        end
        set(handles.popupmenu1, 'String', str);
    end
else
    if (A(matsize,matsize) == 0)
        MyOutput(handles.outputEdit,'行列式为0,求解终止');
        set(handles.pushbutton2, 'Enable', 'off');
        set(handles.pushbutton3, 'Enable', 'off');
        return;
    else
        MyOutput(handles.outputEdit, '矩阵已刷新,即将执行回代过程');
        isDiminished = 1;
    end
end

% 自动运行Button回调函数
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global matsize
global currentColumn
global A
global accuracy

while currentColumn < matsize
    % 选主元
    currentRow = find(abs(A(currentColumn:matsize,currentColumn)) == max(abs(A(currentColumn:matsize,currentColumn))));
    currentRow = currentRow(1) + currentColumn - 1;
    
    % 交换
    temp = A(currentColumn, :);
    A(currentColumn, :) = A(currentRow, :);
    A(currentRow, :) = temp;

    % 消元
    for i = currentColumn+1:matsize
        l = A(i, currentColumn) / A(currentColumn, currentColumn);
        if l ~= 0
            for j = currentColumn:matsize+1
                A(i,j) = A(i,j) - l*A(currentColumn,j);
            end
        end
    end
    
    % 判断是否可以继续
    A = round(A, accuracy);
    currentColumn = currentColumn + 1;
    if (max(abs(A(currentColumn:matsize,currentColumn))) == 0)
        set(handles.uitable2, 'Data', A);
        MyOutput(handles.outputEdit,'行列式为0,求解终止');
        set(handles.pushbutton2, 'Enable', 'off');
        set(handles.pushbutton3, 'Enable', 'off');
        return;
    end 
end

% 判断最后元素是否为0
A = round(A, accuracy);
if (A(matsize,matsize) == 0)
    MyOutput(handles.outputEdit,'行列式为0,求解终止');
    set(handles.pushbutton2, 'Enable', 'off');
    set(handles.pushbutton3, 'Enable', 'off');
    return;
end
    
% 回代
for i = matsize:-1:1
    for j = matsize:-1:i+1
        A(i,:) = A(i,:) - A(i,j)*A(j,:);
    end
    A(i,:) = A(i,:) / A(i,i);
    A = round(A, accuracy);
end
set(handles.uitable2, 'Data', A);
MyOutput(handles.outputEdit, '自动运行结束,矩阵已完成求解');
set(handles.pushbutton2, 'Enable', 'off');
set(handles.pushbutton3, 'Enable', 'off');
set(handles.pushbutton4, 'Enable', 'on');

% 输出结果Button回调函数
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global accuracy;
global matsize;
global originA;
global b;
global A;
MyOutput(handles.outputEdit, '******************************************');
style = ['%.',num2str(accuracy), 'f'];
for i = matsize:-1:1
    MyOutput(handles.outputEdit, ['x', num2str(i), ' = ', sprintf(style, A(i,matsize+1))]);
end
MyOutput(handles.outputEdit, '****************Solution******************');
MyOutput(handles.outputEdit, ['残差范数为: norm2(Ax-b) = ', num2str(norm(originA*A(:,matsize+1) - b))]);


% --- Executes on button press in radiobutton5.
function radiobutton5_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton5
global isAuto;
if get(hObject, 'Value') == 1
    isAuto = 1;
end


% --- Executes on button press in radiobutton6.
function radiobutton6_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton6
global isAuto;
if get(hObject, 'Value') == 1
    isAuto = 0;
end


% 重新开始Button回调函数
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% 一切归位
global outputNumber;
global isDiminished;
set(handles.edit1, 'Enable', 'on');
set(handles.edit3, 'Enable', 'on');
set(handles.uitable2, 'Data', '');
set(handles.pushbutton1, 'Enable', 'on');
set(handles.pushbutton2, 'Enable', 'off');
set(handles.pushbutton3, 'Enable', 'off');
set(handles.pushbutton4, 'Enable', 'off');
set(handles.radiobutton5, 'Enable', 'on');
set(handles.radiobutton6, 'Enable', 'on');
set(handles.popupmenu1, 'Value', 1);
set(handles.popupmenu1, 'String', '请选择主元');
set(handles.popupmenu1, 'Enable', 'off');
set(handles.outputEdit, 'String', '0. 已清除当前信息');
outputNumber = 1;
isDiminished = 0;

This snippet took 0.03 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).