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!