Demo entry 6688025

Exp4.1

   

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

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

% Last Modified by GUIDE v2.5 28-Dec-2017 02:22:05

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

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

% Update handles structure
guidata(hObject, handles);

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

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


% --- Outputs from this function are returned to the command line.
function varargout = Exp4_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 F;
global dF;
global x;
global x0;
global F0;
global dF0;
global B;
global p;
global TolX;
global TolFun;
global totalSteps;
global totalTime;
global equationNo;

% 输入合法性判断
TolX = -str2double(get(handles.edit1, 'String'));
TolFun = -str2double(get(handles.edit1, 'String'));
if TolX > 0 || TolFun > 0 || TolX < -18 || TolFun < -18
    MyOutput(handles.outputEdit, '收敛终止条件填写范围为0~18!')
    return;
end

% 矩阵生成
x = sym('x', [3,1]);
if equationNo == 1
    F1 = 12*x(1) - x(2)^2 - 4*x(3)-7;
    F2 = x(1)^2 + 10*x(2) - x(3)-11;
    F3 = x(2)^3 + 10*x(3) - 8;
    x0 = [1, 1, 1]';
else
    F1 = 3*x(1) - cos(x(2)*x(3)) - 1/2;
    F2 = x(1)^2 - 81*(x(2)+0.1)^2 + sin(x(3)) + 1.06;
    F3 = exp(-x(1)*x(2)) + 20*x(3) + 1/3*(10*pi-3);
    x0 = [0, 0, 0]';
end
x0 = [10, 10, 10]';
F = [F1; F2; F3];
dF = jacobian(F, x);
F0 = double(subs(F, x, x0));
dF0 = double(subs(dF, x, x0));
B = inv(dF0);
p = realmax;
totalSteps = 0;
totalTime = 0;

% 界面操作
set(handles.edit1, 'Enable', 'off');
set(handles.edit6, 'Enable', 'off');
set(handles.radiobutton5, 'Enable', 'off');
set(handles.radiobutton6, 'Enable', 'off');
set(handles.radiobutton9, 'Enable', 'off');
set(handles.radiobutton10, 'Enable', 'off');
set(handles.pushbutton1, 'Enable', 'off');
set(handles.pushbutton2, 'Enable', 'on');
set(handles.pushbutton3, 'Enable', 'on');
set(handles.pushbutton4, 'Enable', 'on');
MyOutput(handles.outputEdit, '矩阵生成成功!请选择单步迭代(将无视终止条件)或自动迭代进行求解。');
set(handles.uitable2, 'Data', x0);
set(handles.uitable5, 'Data', F0);


% 单步迭代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 F;
global dF;
global x;
global x0;
global F0;
global dF0;
global B;
global p;
global totalSteps;
global totalTime;
global iterationFun;

% 根据选择的方法,进行单次迭代操作
tstart = clock;
if iterationFun == 1
    p = double(dF0) \ double(F0);
    x0 = x0 - p;
    F0 = double(subs(F, x, x0));
    dF0 = double(subs(dF, x, x0));
else
    p = -B * F0;
    x1 = x0 + p;
    F1 = double(subs(F, x, x1));
    q = F1 - F0;
    B = B + ((p - B*q) * p' * B) / (p' * B * q);
    x0 = x1;
    F0 = F1;
end
totalTime = totalTime + etime(clock, tstart);
totalSteps = totalSteps + 1;
set(handles.uitable2, 'Data', x0);
set(handles.uitable5, 'Data', F0);
MyOutput(handles.outputEdit, ['单步迭代完成,总迭代次数: ', num2str(totalSteps), ',总耗时: ', sprintf('%.0f', totalTime * 1000), '毫秒']);


% 自动迭代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 F;
global dF;
global x;
global x0;
global F0;
global dF0;
global B;
global totalSteps;
global totalTime;
global iterationFun;
global TolX;
global TolFun;
global p;

% 判断精度是否已达到
if (norm(p) <= 10^TolX) || (norm(F0) <= 10^TolFun)
    MyOutput(handles.outputEdit, '求解已达到精度要求,无需再次迭代');
end

% 根据选择的方法,进行自动迭代操作,满足以下任意条件时终止:
% 1. 自变量变化小于TolX
% 2. 函数值小于TolFun
% 3. 迭代次数大于maxIteration
maxIteration = 200;
iters = 0;
tstart = clock;
if iterationFun == 1
    while (norm(p) > 10^TolX) && (norm(F0) > 10^TolFun) && (iters < maxIteration)
        p = double(dF0) \ double(F0);
        x0 = x0 - p;
        F0 = double(subs(F, x, x0));
        dF0 = double(subs(dF, x, x0));
        iters = iters + 1;
    end
else
    while (norm(p) > 10^TolX) && (norm(F0) > 10^TolFun) && (iters < maxIteration)
        p = -B * F0;
        x1 = x0 + p;
        F1 = double(subs(F, x, x1));
        q = F1 - F0;
        B = B + ((p - B*q) * p' * B) / (p' * B * q);
        x0 = x1;
        F0 = F1;
        iters = iters + 1;
    end
end

% 输出结果
if iters >= maxIteration
    MyOutput(handles.outputEdit, '超出最大迭代次数,迭代可能不收敛');
end
totalTime = totalTime + etime(clock, tstart);
totalSteps = totalSteps + iters;
set(handles.uitable2, 'Data', x0);
set(handles.uitable5, 'Data', F0);
MyOutput(handles.outputEdit, ['自动迭代完成,总迭代次数: ', num2str(totalSteps), ',总耗时: ', sprintf('%.0f', totalTime * 1000), '毫秒']);


% 输出结果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 F0;
global x0;
MyOutput(handles.outputEdit, '******************************************');
for i = 3:-1:1
    MyOutput(handles.outputEdit, ['x', num2str(i), ' = ', sprintf('%.18f', x0(i))]);
end
MyOutput(handles.outputEdit, '****************Solution******************');
MyOutput(handles.outputEdit, ['残差范数为: norm2(Fx) = ', num2str(norm(F0))]);


% 重新开始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 equationNo;
global iterationFun;
global totalSteps;
set(handles.edit1, 'Enable', 'on');
set(handles.edit6, 'Enable', 'on');
set(handles.radiobutton5, 'Enable', 'on');
set(handles.radiobutton6, 'Enable', 'on');
set(handles.radiobutton9, 'Enable', 'on');
set(handles.radiobutton10, 'Enable', 'on');
set(handles.radiobutton5, 'Value', 1);
set(handles.radiobutton9, 'Value', 1);
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;
equationNo = 1;
iterationFun = 1;


% --- 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 equationNo;
if get(hObject, 'Value') == 1
    equationNo = 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 equationNo;
if get(hObject, 'Value') == 1
    equationNo = 2;
end


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

% --- Executes on button press in radiobutton10.
function radiobutton10_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton10 (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 radiobutton10
global iterationFun;
if get(hObject, 'Value') == 1
    iterationFun = 2;
end

This snippet took 0.03 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).