牛顿迭代法的具体内容不赘述
它的核心算法是:

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 EDITgui_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});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before Newton_TEST is made visible.function Newton_TEST_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 Newton_TEST (see VARARGIN)handles.FUN = [];handles.X0 = [];% Choose default command line output for Newton_TESThandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes Newton_TEST wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.function varargout = Newton_TEST_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 structurevarargout{1} = handles.output;function editFUN_Callback(hObject, eventdata, handles)% hObject    handle to editFUN (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of editFUN as text%        str2double(get(hObject,'String')) returns contents of editFUN as a doublehandles.FUN = get(hObject,'String');guidata(hObject, handles);% --- Executes during object creation, after setting all properties.function editFUN_CreateFcn(hObject, eventdata, handles)% hObject    handle to editFUN (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction editX0_Callback(hObject, eventdata, handles)% hObject    handle to editX0 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of editX0 as text%        str2double(get(hObject,'String')) returns contents of editX0 as a doublehandles.X0 = str2num(get(hObject,'String'));guidata(hObject, handles);% --- Executes during object creation, after setting all properties.function editX0_CreateFcn(hObject, eventdata, handles)% hObject    handle to editX0 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction editError_Callback(hObject, eventdata, handles)% hObject    handle to editError (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of editError as text%        str2double(get(hObject,'String')) returns contents of editError as a doublehandles.ERROR = str2num(get(hObject,'String'));guidata(hObject, handles);% --- Executes during object creation, after setting all properties.function editError_CreateFcn(hObject, eventdata, handles)% hObject    handle to editError (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction editN_Callback(hObject, eventdata, handles)% hObject    handle to editN (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of editN as text%        str2double(get(hObject,'String')) returns contents of editN as a doublehandles.N = str2num(get(hObject,'String'));guidata(hObject, handles);% --- Executes during object creation, after setting all properties.function editN_CreateFcn(hObject, eventdata, handles)% hObject    handle to editN (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');end% --- Executes on selection change in listboxK.function listboxK_Callback(hObject, eventdata, handles)% hObject    handle to listboxK (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hints: contents = cellstr(get(hObject,'String')) returns listboxK contents as cell array%        contents{get(hObject,'Value')} returns selected item from listboxK% --- Executes during object creation, after setting all properties.function listboxK_CreateFcn(hObject, eventdata, handles)% hObject    handle to listboxK (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: listbox controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');end% --- Executes on selection change in listboxX.function listboxX_Callback(hObject, eventdata, handles)% hObject    handle to listboxX (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hints: contents = cellstr(get(hObject,'String')) returns listboxX contents as cell array%        contents{get(hObject,'Value')} returns selected item from listboxX% --- Executes during object creation, after setting all properties.function listboxX_CreateFcn(hObject, eventdata, handles)% hObject    handle to listboxX (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: listbox controls usually have a white background on Windows.%       See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');end% --- Executes on button press in pushbuttonRUN.function pushbuttonRUN_Callback(hObject, eventdata, handles)% hObject    handle to pushbuttonRUN (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)claf = handles.FUN;x0 = handles.X0;N = handles.N;e = handles.ERROR;f = eval(['@(x)',f]); % 将用户输入的str类型转化为有意义的匿名函数df = eval(['@(x)',char(diff(sym(f)))]); % 计算微分node = line(x0,f(x0),'Marker','o','MarkerSize',8,'MarkerFaceColor','r','MarkerEdgeColor','r');connectline = line(x0,f(x0),'linewidth',1,'color','b'); %用来画连接的线k = 1;x = x0;x0 = x+e*2;K = [];X = [];xcon = x0;ycon = f(x0);while (abs(x-x0))>e && (k<=N)x0 = x;x = x0 - f(x0)/df(x0);xcon = [xcon,x];  % 用将新的x值同旧值连接ycon = [ycon,f(x)];K = [{num2str(k)};K];X = [{num2str(x)};X];% 将循环结果呈现在界面set(handles.listboxK,'string',K);set(handles.listboxX,'string',X);set(node,'xdata',x,'ydata',f(x));set(connectline,'xdata',xcon,'ydata',ycon);pause(0.5)  % 为了能让人眼看到过程,用pause暂停0.5sk = k+1;endhold onxline = sort(xcon);[xx,yy] = fplot(f,[min(xline),max(xline)]);  %画出模拟曲线plot(xx,yy,'--k');

用一个的高次方程举例是这样

纯纯纯纯小白,发上来是希望可以互相交流,以及督促自己继续联系其他的数值方法。

Matlab牛顿迭代法求方程的根(GUI)相关推荐

  1. 用牛顿迭代法求方程的根

    用牛顿迭代法求方程的根(C语言) 题目要求:牛顿迭代法是一种重要的基本的求方程根的方法.现有方程为axˆ3+bxˆ2+cx+d=0,系数a,b,c,d的值一次为1,2,3,4,由主函数输入.求x在1附 ...

  2. 用牛顿迭代法求方程的根matlab,牛顿迭代法求方程根的MATLAB程序

    function [x_reality,n_reality] = Newt( f_name,x_start,tolerance,n_limit) %% %牛顿迭代法(切线法)求解方程f_name = ...

  3. 用牛顿迭代法求方程的根matlab,牛顿迭代法求方程解 程序如下

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 function [x_reality,n_reality] = Newt( f_name,x_start,tolerance,n_limit) %% % ...

  4. 牛顿迭代法例题 matlab,牛顿迭代法求方程根的MATLAB程序

    function [x_reality,n_reality] = Newt( f_name,x_start,tolerance,n_limit) %% %牛顿迭代法(切线法)求解方程f_name = ...

  5. 牛顿迭代法求方程的根

    牛顿迭代法(牛顿-拉弗森方法) 五次及以上多项式方程没有根式解(就是没有像二次方程那样的万能公式),这个是被伽罗瓦用群论做出的最著名的结论.没有根式解不意味着方程解不出来,数学家也提供了很多方法,牛顿 ...

  6. 1087 习题5-14 牛顿迭代法求方程的根

    题目描述 用牛顿迭代法求下面方程在输入初值点附近的根: 2x3-4x2+3x-6=0 要求前后两次求出的x的差的绝对值小于10-6 牛顿迭代法公式如下: 将给定给定方程写成f(x)=0的形式,在给定初 ...

  7. java牛顿法求方程根_C程序习题-用牛顿迭代法求方程的根[6.12]

    用牛顿迭代法求下面方程在1.5附近的根.2X3– 4X2+3X- 6 = 0 看到这个题目,我便开始百度,看什么是牛顿迭代法.看了上面的解释,我还是一头雾水.无从下手.不知所云.看着上面写的推到公式, ...

  8. 【清橙A1094】【牛顿迭代法】牛顿迭代法求方程的根

    问题描述 给定三次函数f(x)=ax3+bx2+cx+d的4个系数a,b,c,d,以及一个数z,请用牛顿迭代法求出函数f(x)=0在z附近的根,并给出迭代所需要次数. 牛顿迭代法的原理如下(参考下图) ...

  9. 100个python算法超详细讲解:牛顿迭代法求方程根

    1.问题描述 编写用牛顿迭代法求方程根的函数.方程为ax 3 +bx 2 +cx+d=0,系数a. b.c.d由主函数输入,求x在1附近的一个实根.求出根后,由主函数输出. 2.问题分析 牛顿迭代法是 ...

最新文章

  1. apr提高tomcat的web性能
  2. 已解决:Unable to register authentication agent: GDBus.Error:org.freedesktop.PolicyKit1.Error.Failed:
  3. DevExpress的TreeList怎样设置数据源使其显示成单列树形结构
  4. CSRF的绕过与利用
  5. python可以做计量分析吗_技术分享 - python数据分析(2)——数据特征分析(上)...
  6. python--字符串为空
  7. python 代码行数统计工具_Python实现代码行数统计工具
  8. 30页PPT解析微服务架构与最佳实践
  9. SQLite学习笔记
  10. 计算机视觉有哪些商业用途​
  11. 搭建eova开发环境
  12. linux bzip指定名称,bzip2命令_Linux bzip2 命令用法详解:将文件压缩成bz2格式
  13. windows安装pdf虚拟打印机
  14. JQuery的选择器对控件ID含有特殊字符的解决方法
  15. 设置计算机开机密码的步骤,电脑设置开机密码的方法
  16. We will rock you (我们将震憾你)
  17. 各大浏览器的内核分别是什么?
  18. C语言实践项目:2019年个税计算器
  19. mysql校对集_MySQL校对集问题的教程
  20. js 实现简单todo效果

热门文章

  1. java实现使用代码压缩文件_Jenkins:使用SonarQube实现代码审查
  2. 前有雅迪,后有九号,小牛电动的“智能”故事能动听多久?
  3. 【详细版】用Markdown必备,Typora+PicGo+GitHub搭建免费图床
  4. spring-AOP
  5. 北风网Spark2.0学习笔记
  6. LED灯丝灯驱动电源芯片方案-输出短路保护特性 调光深度0.1% 外围元件少
  7. 中国科学家成功筛选拉沙病毒入侵抑制剂
  8. CAN详解--书籍、网站
  9. python之排序操作及heapq模块
  10. 最适合运动的耳机类型、音质好的运动耳机推荐