3 个答案:

答案 0 :(得分:6)

你可以使用这样的实现:

function x = newton(f,dfdx,x0,tolerance)

err = Inf;

x = x0;

while abs(err) > tolerance

xPrev = x;

x = xPrev - f(xPrev)./dfdx(xPrev);

% stop criterion: (f(x) - 0) < tolerance

err = f(x); %

% stop criterion: change of x < tolerance

% err = x - xPrev;

end

并传递函数及其派生的函数句柄。这种衍生物可以通过一些不同的方法获得:手工分化,象征性区分或自动区分。您也可以在数字上执行区分,但这很慢并且需要您使用修改后的实现。所以我假设你已经以任何合适的方式计算了导数。然后你可以调用代码:

f = @(x)((x-4).^2-4);

dfdx = @(x)(2.*(x-4));

x0 = 1;

xRoot = newton(@f,@dfdx,x0,1e-10);

答案 1 :(得分:3)

没有办法代数获取m文件中定义的函数句柄或函数的派生词。您必须通过在多个点评估函数并近似导数来数字。

>> syms x %# Create a symbolic variable x

>> f = (x-4)^2-4; %# Create a function of x to find a root of

>> xRoot = 1; %# Initial guess for the root

>> g = x-f/diff(f); %# Create a Newton-Raphson approximation function

>> xRoot = subs(g,'x',xRoot) %# Evaluate the function at the initial guess

xRoot =

1.8333

>> xRoot = subs(g,'x',xRoot) %# Evaluate the function at the refined guess

xRoot =

1.9936

>> xRoot = subs(g,'x',xRoot) %# Evaluate the function at the refined guess

xRoot =

2.0000

在几次迭代之后,您可以看到xRoot的值接近真根的值(即2)。您还可以将函数求值放在while循环中,其条件可以检查每个新猜测与之前猜测之间的差异有多大,当该差异足够小时停止(即已找到根):

xRoot = 1; %# Initial guess

xNew = subs(g,'x',xRoot); %# Refined guess

while abs(xNew-xRoot) > 1e-10 %# Loop while they differ by more than 1e-10

xRoot = xNew; %# Update the old guess

xNew = subs(g,'x',xRoot); %# Update the new guess

end

xRoot = xNew; %# Update the final value for the root

答案 2 :(得分:0)

% Friday June 07 by Ehsan Behnam.

% b) Newton's method implemented in MATLAB.

% INPUT:1) "fx" is the equation string of the interest. The user

% may input any string but it should be constructable as a "sym" object.

% 2) x0 is the initial point.

% 3) intrvl is the interval of interest to find the roots.

% returns "rt" a vector containing all of the roots for eq = 0

% on the given interval and also the number of iterations to

% find these roots. This may be useful to find out the convergence rate

% and to compare with other methods (e.g. Bisection method).

%

function [rt iter_arr] = newton_raphson(fx, x, intrvl)

n_seeds = 10; %number of initial guesses!

x0 = linspace(intrvl(1), intrvl(2), n_seeds);

rt = zeros(1, n_seeds);

% An array that keeps the number of required iterations.

iter_arr = zeros(1, n_seeds);

n_rt = 0;

% Since sometimes we may not converge "max_iter" is set.

max_iter = 100;

% A threshold for distinguishing roots coming from different seeds.

thresh = 0.001;

for i = 1:length(x0)

iter = 0;

eq = sym(fx);

max_error = 10^(-12);

df = diff(eq);

err = Inf;

x_this = x0(i);

while (abs(err) > max_error)

iter = iter + 1;

x_prev = x_this;

% Iterative process for solving the equation.

x_this = x_prev - subs(fx, x, x_prev) / subs(df, x, x_prev);

err = subs(fx, x, x_this);

if (iter >= max_iter)

break;

end

end

if (abs(err) < max_error)

% Many guesses will result in the same root.

% So we check if the found root is new

isNew = true;

if (x_this >= intrvl(1) && x_this <= intrvl(2))

for j = 1:n_rt

if (abs(x_this - rt(j)) < thresh)

isNew = false;

break;

end

end

if (isNew)

n_rt = n_rt + 1;

rt(n_rt) = x_this;

iter_arr(n_rt) = iter;

end

end

end

end

rt(n_rt + 1:end) = [];

iter_arr(n_rt + 1:end) = [];

matlab如何编newton-raphson,Matlab中的Newton Raphsons方法?相关推荐

  1. c 与matlab混编,C与MATLAB混合编程之调用MATLAB引擎

    Visual Studio调用MATLAB需要的配置已经在<Visual Studio调用MATLAB的配置>一文中给出了详细的步骤,本文将介绍混合编程的示例. 在C中调用MATLAB,据 ...

  2. 噪点检测matlab,基于噪点检测的中值滤波图像去噪方法

    摘  要: 图像去噪是图像处理中一个非常重要的环节.针对传统中值滤波方法存在的不足,提出一种新的基于噪点检测的自适应中值滤波图像去噪方法.该方法通过自适应地改变滤波窗口的大小,局部检测并判断极值点是否 ...

  3. QT matlab 混编-qt调用matlab生成dll

    前言 环境: win10 Qt5.12 mingW(64位) matlab2019a(64位)----mingw w64 主要参考的文章:1. https://blog.csdn.net/Justic ...

  4. c语言matlab混编max函数,Matlab与C++混编 – engin.h

    #include #include #include "engine.h" using namespace std; void main() { Engine *ep; //定义M ...

  5. 【语音编码】基于matlab ADPCM编解码(Matlab代码实现)

  6. java中的方法在哪里_Java中的本机方法是什么,应在哪里使用?

    小编典典 Java中的本机方法是什么,应在哪里使用? 一旦看到一个小例子,就很清楚了: Main.java : public class Main { public native int intMet ...

  7. 解决了java+matlab混编+web(jsp)调用Matlab,网页中显示Figure,详细实例

    例子简介 网上java+matlab混编的挺多,大多数实例也都能实现出来,但是将matlab生成出来Figure显示在jsp页面中并配合WebFigure实现的例子却很少,所以我将自己的尝试结果详细的 ...

  8. m 文件 dll matlab 中调用_Java与MatLab混编

    该篇介绍Java与MatLab的混合编程 环境:Windows10,Jdk 1.8.0_261 64位,MatLab r2018b 64位 先安装MatLab MatLab安装 一.打开MatLab, ...

  9. matlab振动频谱分析是不是要,VB和Matlab混编实现振动信号的频谱分析

    VB和Matlab混编实现振动信号的频谱分析 3 0 物理测试 2 O O 4年第 2期 V B和 Ma t l a b混编实现振动信号的频谱分析 谭轶平, 王振清 ( 北京航空航天大学材料学院,北京 ...

  10. 码长6075的qc-ldpc编译码的MATLAB误码率仿真

    1.算法简介 为满足高的数据需求,提出一种新的QC-LDPC码.该校验矩阵的校验部分为近似下三角结构,上对角线下面的非零元素可以任意放置,因此是一种半确定的结构.这种结构的码设计灵活,性能也极高.通过 ...

最新文章

  1. 亿级商品详情页架构演进技术解密
  2. HttpServletResponse对象(一)
  3. 修改mysql   默认字符集 , 默认引擎
  4. 从一个工程师到管理员的经验分享
  5. SQL查询语句基础构成
  6. linux如何使用uboot的命令,Uboot常用命令使用
  7. 计算机专业410分能上哪些大学,2021年高考410分能报什么学校
  8. 查询表授权给谁了_SQL Server 全局临时表竞争条件漏洞利用
  9. 酷比魔方iwork8刷机shell_酷比魔方iwork8超级版双系统刷机教程,remix+Windows10,序列号i1开头数字结尾...
  10. 【教你在win7中开启四声道效果】
  11. 协议分析_qvod_获取快播视频的下载地址_20120203
  12. 2016年世界各国gdp排名 人均gdp排名 人口排名
  13. Lowe‘s EDI 855 采购订单确认报文详解
  14. 【luogu3403】跳楼机 [同余最短路]
  15. 上传图片,使用很简单的办法上传图片
  16. 洛谷P3354 [IOI2005]Riv 河流 题解
  17. 惠普无线鼠标没有反应
  18. java开源项目jeecgboot全解析
  19. 用js写卡牌游戏(四)
  20. hdu(杭电oj)第一页题目题解

热门文章

  1. 浅谈游戏《神秘海域4:盗贼末路》
  2. 【Java基础】· 面向对象编程(下)习题详解
  3. 鼠标左键双击计算机打开属性,鼠标左键双击变成属性怎么解决?
  4. python前三周学习心得
  5. 计算机报名503,503 service temporarily unavailable是什么意思【解决方法】
  6. FBI教你破解身体语言
  7. Html 特殊符号 让版权符号更美观
  8. 查看英伟达NVIDIA显卡型号
  9. 用计算机弹九八k的乐谱,完整版儿童歌曲简谱大全.docx
  10. 计算机桌面图标底纹,怎样去除桌面图标下的底色[XP系统]【图文教程】