Matlab牛顿迭代法求方程的根(GUI)

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

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
  • 12
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Matlab中,可以使用牛顿法和迭代法方程。 牛顿法的基本思想是利用函数在当前点的切线来逼近函数的,然后出切线与x轴交点作为下一个迭代点,直到满足收敛条件为止。 下面是使用Matlab实现牛顿法方程的代码: ```matlab function [x, fx, iter] = newton_method(f, df, x0, tol, maxiter) % f: 待解的方程 % df: f的导数 % x0: 初值 % tol: 精度要 % maxiter: 最大迭代次数 % x: 方程 % fx: 函数值 % iter: 迭代次数 iter = 0; x = x0; fx = f(x); while abs(fx) > tol && iter < maxiter x = x - fx / df(x); fx = f(x); iter = iter + 1; end if iter == maxiter disp('迭代次数达到最大值,未找到解'); end end ``` 迭代法的基本思想是利用某种迭代公式不断逼近函数的,直到满足收敛条件为止。常用的迭代公式有简单迭代法和牛迭法。 下面是使用Matlab实现简单迭代法方程的代码: ```matlab function [x, fx, iter] = simple_iter_method(f, g, x0, tol, maxiter) % f: 待解的方程 % g: 迭代公式 % x0: 初值 % tol: 精度要 % maxiter: 最大迭代次数 % x: 方程 % fx: 函数值 % iter: 迭代次数 iter = 0; x = x0; fx = f(x); while abs(fx) > tol && iter < maxiter x = g(x); fx = f(x); iter = iter + 1; end if iter == maxiter disp('迭代次数达到最大值,未找到解'); end end ``` 需要注意的是,迭代公式的选取对于迭代法的收敛性有很大影响,不同的问题需要选用不同的迭代公式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值