MATLAB Answers

# Error in Newton-Raphson Method

11 views (last 30 days)
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
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Answers (1)

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)
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!