Demo entry 6688039

Exp3.3

   

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

function varargout = Exp3_3(varargin)
% Exp3_3 MATLAB code for Exp3_3.fig
%      Exp3_3, by itself, creates a new Exp3_3 or raises the existing
%      singleton*.
%
%      H = Exp3_3 returns the handle to a new Exp3_3 or the handle to
%      the existing singleton*.
%
%      Exp3_3('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in Exp3_3.M with the given input arguments.
%
%      Exp3_3('Property','Value',...) creates a new Exp3_3 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Exp3_3_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Exp3_3_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_3

% Last Modified by GUIDE v2.5 27-Dec-2017 19:57:43

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Exp3_3_OpeningFcn, ...
                   'gui_OutputFcn',  @Exp3_3_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_3 is made visible.
function Exp3_3_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_3 (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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

global outputNumber;
global iterationFun;
outputNumber = 1;
iterationFun = 1;
set(handles.uitable2, 'Data', '');
set(handles.uitable5, 'Data', '');


% --- Outputs from this function are returned to the command line.
function varargout = Exp3_3_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 matSize;
global H;
global b;
global x;
global iterationFun; % 1,2,3,4分别代表LU法,J法,GS法和SOR法
global accuracy;
global totalSteps;
global warningFlag;

% 判断输入是否合法
matSize = round(str2double(get(handles.edit1, 'String')));
if (matSize < 2)
    MyOutput(handles.outputEdit, '矩阵规模应至少为2!');
    return;
end
accuracy = round(str2double(get(handles.edit5, 'String')));
if (accuracy < 0) || (accuracy > 18)
    MyOutput(handles.outputEdit, '精度应介于0~18位之间!');
    return;
end

% 矩阵生成
H = hilb(matSize);
b = H * ones(matSize, 1);
x = zeros(matSize, 1);

% 界面操作
totalSteps = 0;
set(handles.edit1, 'Enable', 'off');
set(handles.radiobutton5, 'Enable', 'off');
set(handles.radiobutton6, 'Enable', 'off');
set(handles.radiobutton7, 'Enable', 'off');
set(handles.radiobutton8, 'Enable', 'off');
set(handles.edit5, 'Enable', 'off');
set(handles.pushbutton1, 'Enable', 'off');
if iterationFun == 1
    set(handles.pushbutton2, 'Enable', 'on');
else
    set(handles.pushbutton3, 'Enable', 'on');
end
set(handles.pushbutton4, 'Enable', 'on');
addstr = ['矩阵生成成功,矩阵阶数:', num2str(matSize), ',输入矩阵的条件数为:', num2str(cond(H))];
MyOutput(handles.outputEdit, addstr);
set(handles.uitable2, 'Data', x);
set(handles.uitable5, 'Data', H*x - b);
warningFlag = 0;


% 直接求解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)

% LU分解直接求解
global H;
global b;
global x;
global matSize;
global accuracy;
[L, U, P] = lu(H);
L = round(L, accuracy);
U = round(U, accuracy);
P = round(P, accuracy);
x = round(U \ round(L \ round(P*b, accuracy), accuracy), accuracy);
MyOutput(handles.outputEdit, '直接法求解完毕!');
set(handles.uitable2, 'Data', x);
set(handles.uitable5, 'Data', H*x - b);
set(handles.pushbutton2, 'Enable', 'off');


% 迭代求解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 warningFlag;
global warningNumber;
global H;
global b;
global x;
global steps;
global totalSteps;
global iterationFun;
global accuracy;

% 此处实现功能如下:
% 若迭代次数小于0,则提示错误;
% 若迭代次数大于1E6,则提示较长耗时,再次点击后确认操作,否则不进行迭代。

steps = round(str2double(get(handles.edit3, 'String')));
% 迭代次数应大于0
if steps < 0
    warningFlag = 0;
    warningNumber = -1;
    MyOutput(handles.outputEdit, '迭代次数应大于0!');
    return;
end
% 是否为二次确认耗时迭代操作
if warningFlag
    if steps ~= warningNumber
        warningFlag = 0;
        warningNumber = -1;
    end
end

% 若为首次发生,则给出提示,否则直接进行迭代
if steps > 1E6 && ~warningFlag
    MyOutput(handles.outputEdit, '迭代次数大于10^6时,求解可能需要花费数秒,如确认求解,请再次点击''迭代求解''按钮');
    warningFlag = 1;
    warningNumber = steps;
    return;
else
    warningFlag = 0;
    warningNumber = -1;
end

% 分解
D = diag(diag(H));
U = -triu(H, 1);
L = -tril(H, -1);

% 生成迭代矩阵
switch iterationFun
    case 2 % J法
        B = round(D \ (L + U), accuracy);
        f = round(D \ b, accuracy);
    case 3 % GS法
        B = round((D - L) \ U, accuracy);
        f = round((D - L) \ b, accuracy);
    case 4 % SOR法
        % 此处需要先对w进行优化,寻找最小值
        w = fminbnd(@(x)vrho((D - x*L) \ ((1-x)*D + x*U)), 0, 2);
        MyOutput(handles.outputEdit, ['最优超松弛因子为: w = ', num2str(w)]);
        B = round((D - w*L) \ ((1-w)*D + w*U), accuracy);
        f = round(w * ((D - w*L) \ b), accuracy);
end

%收敛性判断
if vrho(B) >= 1
    MyOutput(handles.outputEdit, '所选择的迭代方法不收敛,求解终止');
    set(handles.pushbutton3, 'Enable', 'off');
    return;
end

% 迭代
for i = 1:steps
    x = round(B * x + f, accuracy);
end
totalSteps = totalSteps + steps;

% 界面刷新
MyOutput(handles.outputEdit, ['迭代结束,当前总迭代次数: ', num2str(totalSteps), ' 次']);
set(handles.uitable2, 'Data', x);
set(handles.uitable5, 'Data', H*x - b);


% 输出结果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 H;
global matSize;
global accuracy;
global x;
global b;
MyOutput(handles.outputEdit, '******************************************');
style = ['%.',num2str(accuracy), 'f'];
for i = matSize:-1:1
    MyOutput(handles.outputEdit, ['x', num2str(i), ' = ', sprintf(style, x(i))]);
end
MyOutput(handles.outputEdit, '****************Solution******************');
MyOutput(handles.outputEdit, ['残差范数为: norm2(Hx-b) = ', num2str(norm(H*x - 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 iterationFun;
if get(hObject, 'Value') == 1
    set(handles.edit3, 'Enable', 'off');
    iterationFun = 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 iterationFun;
if get(hObject, 'Value') == 1
    set(handles.edit3, 'Enable', 'on');
    iterationFun = 2;
end

% --- Executes on button press in radiobutton7.
function radiobutton7_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton7 (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 radiobutton7
global iterationFun;
if get(hObject, 'Value') == 1
    set(handles.edit3, 'Enable', 'on');
    iterationFun = 3;
end

% --- Executes on button press in radiobutton8.
function radiobutton8_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton8 (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 radiobutton8
global iterationFun;
if get(hObject, 'Value') == 1
    set(handles.edit3, 'Enable', 'on');
    iterationFun = 4;
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 iterationFun;
global totalSteps;
set(handles.edit1, 'Enable', 'on');
set(handles.edit3, 'Enable', 'off');
set(handles.radiobutton5, 'Enable', 'on');
set(handles.radiobutton5, 'Value', 1);
set(handles.radiobutton6, 'Enable', 'on');
set(handles.radiobutton7, 'Enable', 'on');
set(handles.radiobutton8, 'Enable', 'on');
set(handles.edit5, 'Enable', 'on');
set(handles.pushbutton1, 'Enable', 'on');
set(handles.pushbutton2, 'Enable', 'off');
set(handles.pushbutton3, 'Enable', 'off');
set(handles.pushbutton4, 'Enable', 'off');
set(handles.outputEdit, 'String', '0. 已清除当前信息');
set(handles.uitable2, 'Data', '');
set(handles.uitable5, 'Data', '');
totalSteps = 0;
outputNumber = 1;
iterationFun = 1;

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).