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!