matlab Newton method

% 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值