码迷,mamicode.com
首页 > 其他好文 > 详细

matlab Newton method

时间:2017-05-21 21:41:29      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:use   isp   enter   rate   eth   script   line   style   compute   

% Matlab script to illustrate Newton‘s method
% to solve a nonlinear equation

% this particular script finds the square root of a number M
% (input by the user)

% note that the function we are trying to zero is f(x) = x^2 - M.
% its derivative is f‘(x) = 2*x.
% these functions are hard-coded in the script.

format long

% get user input
M = input(‘Please enter the number whose square root you want: ‘)
x0 = input(‘Please enter starting guess: ‘)

% iteration counter
k = 1
% compute first Newton iterate to enter loop
x = x0 - (x0^2-M)/(2*x0)
disp(‘Hit return to continue‘)
pause 

while abs(x-x0) > eps*abs(x),
    % reset guess to old iterate
    x0 = x;
    % increment iteration counter
    k = k + 1
    % compute and display Newton iterate
    x = x0 - (x0^2-M)/(2*x0)
    disp(‘Hit return to continue‘)
    pause 
end

matlab Newton method

标签:use   isp   enter   rate   eth   script   line   style   compute   

原文地址:http://www.cnblogs.com/yfceshi/p/6885967.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!