Error in Newton-Raphson Method

1 回表示 (過去 30 日間)
Natalie Spalding
Natalie Spalding 2021 年 2 月 22 日
回答済み: Alan Stevens 2021 年 2 月 22 日
Given:
Why is this code running so long? I'm not getting the expected value, which is just over 5, any help is appreciated.
%% Problem 3b. Newton-Raphson Method (Needs Work)
clear all
clc
R=15/2;
v=500;
h=8;
i=1;
while (1)
fh=(pi^R*h^2-pi/(3*h^3-v));
fph=2*pi*R*h-pi*h^2;
hnew=h-fh/fph;
err=abs((hnew-h)/hnew);
if err<(1*10^(-8))break,end
if hnew<0
h=(hnew+h)/2;
else
h=hnew;
end
i=i+1;
end
i
h

回答 (1 件)

Alan Stevens
Alan Stevens 2021 年 2 月 22 日
Like this
R=15/2;
v=500;
h=8;
i=1;
err = 1;
while err>10^-8 && i<100
hold = h;
fh = pi*h^2*(3*R - h)/3 - v;
fph=pi*(2*h*R - h^2);
h=h-fh/fph;
err=abs((h-hold)/h);
i=i+1;
end
disp(h)

カテゴリ

Help Center および File ExchangeNewton-Raphson Method についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by