如何在Matlab中使用Newton-Raphson方法找到方程根? [英] How to use Newton-Raphson method in matlab to find an equation root?

查看:304
本文介绍了如何在Matlab中使用Newton-Raphson方法找到方程根?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MATLAB的新用户.我想使用牛顿-拉夫森方法找到使f(x) = 0的值.我试图编写代码,但是似乎很难实现Newton-Raphson方法.这是我到目前为止的内容:

I am a new user of MATLAB. I want to find the value that makes f(x) = 0, using the Newton-Raphson method. I have tried to write a code, but it seems that it's difficult to implement Newton-Raphson method. This is what I have so far:

function x = newton(x0, tolerance)
    tolerance = 1.e-10;
    format short e;
    Params = load('saved_data.mat');
    theta = pi/2;
    zeta = cos(theta);
    I = eye(Params.n,Params.n);
    Q = zeta*I-Params.p*Params.p';

    % T is a matrix(5,5)
    Mroot = Params.M.^(1/2);  %optimization
    T = Mroot*Q*Mroot;

    % Find the eigenvalues
    E = real(eig(T));

    % Find the negative eigenvalues
    % Find the smallest negative eigenvalue
    gamma = min(E);  

    % Now solve for lambda
    M_inv = inv(Params.M);  %optimization
    zm = Params.zm;

    x = x0;
    err = (x - xPrev)/x;

    while abs(err) > tolerance
        xPrev = x;
        x = xPrev - f(xPrev)./dfdx(xPrev);

        % stop criterion: (f(x) - 0) < tolerance
        err = f(x);
   end 

   % stop criterion: change of x < tolerance % err = x - xPrev;

end

以上功能的用法如下:

% Calculate the functions
Winv = inv(M_inv+x.*Q);

f = @(x)( zm'*M_inv*Winv*M_inv*zm);

dfdx = @(x)(-zm'*M_inv*Winv*Q*M_inv*zm);

x0 = (-1/gamma)/2;

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

推荐答案

问题不是特别清楚.但是,您是否需要实现找到自己的根本角色?如果不是,则只需使用Matlab的内置函数 fzero (不是基于Newton-Raphson).

The question isn't particularly clear. However, do you need to implement the root finding yourself? If not then just use Matlab's built in function fzero (not based on Newton-Raphson).

如果您确实需要自己实施Newton-Raphson方法,那么我建议使用

If you do need your own implementation of the Newton-Raphson method then I suggest using one of the answers to Newton Raphsons method in Matlab? as your starting point.

编辑:以下内容无法回答您的问题,只是有关编码样式的注释.

The following isn't answering your question, but is just a note on coding style.

将程序分成可重用的块很有用.在这种情况下,您的根本发现应该与功能构造分开.我建议在一个单独的文件中编写Newton-Raphson方法,然后从脚本中调用此方法,在脚本中定义函数及其派生函数.然后您的来源看起来像这样:

It is useful to split your program up into reusable chunks. In this case your root finding should be separated from your function construction. I recommend writing your Newton-Raphson method in a separate file and call this from the script where you define your function and its derivative. Your source would then look some thing like:

% Define the function (and its derivative) to perform root finding on:
Params = load('saved_data.mat');
theta = pi/2;
zeta  = cos(theta);
I = eye(Params.n,Params.n);
Q = zeta*I-Params.p*Params.p';

Mroot = Params.M.^(1/2);
T = Mroot*Q*Mroot; %T is a matrix(5,5)

E = real(eig(T)); % Find the eigen-values

gamma = min(E);   % Find the smallest negative eigen value

% Now solve for lambda (what is lambda?)
M_inv = inv(Params.M);
zm = Params.zm;

Winv = inv(M_inv+x.*Q);

f = @(x)( zm'*M_inv*Winv*M_inv*zm);
dfdx = @(x)(-zm'*M_inv*Winv*Q*M_inv*zm);

x0 = (-1./gamma)/2.;

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

newton.m中,您将实现Newton-Raphson方法,该方法将您定义的函数处理方式(fdfdx)作为参数.使用问题中给出的代码,看起来像

In newton.m you would have your implementation of the Newton-Raphson method, which takes as arguments the function handles you define (f and dfdx). Using your code given in the question, this would look something like

function root = newton(f, df, x0, tol)

    root = x0; % Initial guess for the root

    MAXIT = 20; % Maximum number of iterations

    for j = 1:MAXIT;

        dx = f(root) / df(root);
        root = root - dx

        % Stop criterion:
        if abs(dx) < tolerance
            return
        end

    end

    % Raise error if maximum number of iterations reached.
    error('newton: maximum number of allowed iterations exceeded.')

end 

请注意,我避免使用无限循环.

Notice that I avoided using an infinite loop.

这篇关于如何在Matlab中使用Newton-Raphson方法找到方程根?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆