While loop isn't working
显示 更早的评论
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 个评论
dpb
2020-8-23
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
2020-8-23
Adam Danz
2020-8-23
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.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!