牛顿迭代法的具体内容不赘述
它的核心算法是:
k = 1;
x = x0;
x0 = x + e*2; % 为了让初启动时满足循环条件
while (abs(x-x0))>e && (k<=N) % 同时限定误差和最大循环次数
x0 = x;
x = x0 - f(x0)/df(x0); % 牛顿迭代法式子
k = k+1;
end
这是一个非常简单的牛顿法实现,根据上学期学的数值分析和这学期上的Matlab,把它用GUI简单包装了一下。
用户输入函数表达式、初值、最大容许误差、最大循环次数之后,就可以计算根。代码实现如下。
function varargout = Newton_TEST(varargin)
% NEWTON_TEST MATLAB code for Newton_TEST.fig
% NEWTON_TEST, by itself, creates a new NEWTON_TEST or raises the existing
% singleton*.
%
% H = NEWTON_TEST returns the handle to a new NEWTON_TEST or the handle to
% the existing singleton*.
%
% NEWTON_TEST('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in NEWTON_TEST.M with the given input arguments.
%
% NEWTON_TEST('Property','Value',...) creates a new NEWTON_TEST or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Newton_TEST_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Newton_TEST_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 Newton_TEST
% Last Modified by GUIDE v2.5 12-Mar-2020 23:03:08
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Newton_TEST_OpeningFcn, ...
'gui_OutputFcn', @Newton_TEST_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 NO