MATLAB Answers

Error in Newton-Raphson Method

11 views (last 30 days)
Natalie Spalding
Natalie Spalding on 22 Feb 2021
Answered: Alan Stevens on 22 Feb 2021
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

Answers (1)

Alan Stevens
Alan Stevens on 22 Feb 2021
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)

Community Treasure Hunt

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

Start Hunting!