While loop isn't working
Show older comments
Hello,
My function is as follows:
function root = NM(f, f1, x_0, epsilon, iteration)
x = zeros(1, 50);
x(1) = x_0;
count = 0;
while abs(f(x)) < epsilon
count = count + 1;
if count > iteration
break
end
x = x - f(x)/f1(x);
end
root = x;
end
Here's how I tried to test my function:
clear
syms t
x_0 = 2*pi/3;
epsilon = 0.0001;
iteration = 30;
v = @(t) sin(t);
vprime = matlabFunction(diff(v(t)));
root1 = NM(v, vprime, x_0, epsilon, iteration)
The loop does not seem to work, as the result I got was the initial value 2pi/3. What have I done wrong?
4 Comments
dpb
on 23 Aug 2020
I see no reference to function NM anywhere in the test script??? You call something named NewtonsMethod instead with at least one undefined argument f.
As for the while construct presuming you do somehow have a way in which you did call NM, use the debugger and step through the function looking carefully at the logical test after also reading very carefully the definition of TRUE for if and while logical tests in MATLAB.
StillANovice
on 23 Aug 2020
Adam Danz
on 23 Aug 2020
You could probably see what's wrong by investigating the variables in debug mode and it will probably be much faster than the time it will take to explain things to use and wait for a response.
Accepted Answer
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!