Hi Guys, I need help in writing code for finding difference between two successive values of a variable in for loop.

1 回表示 (過去 30 日間)
Actually, I am writing code for bisection method. Following is the part of code which repeats iterations of the method.
disp('iter a b s=(a+b)/2 y(s) yd')
% for loop starts here
for k=1:N
c=(a+b)/2;
if (f(c)==0)
root=c;
return
elseif (f(a)*f(c)<0)
b=c;
else
a=c;
end
s = (a+b)/2;
y = f(s);
%yd=y(k)-y(k-1);
iter = k;
out=[iter, a , b, s, y, yd];
fprintf(1,'%.0f %.05f %.05f %.05f %.05f %.05f\n',out') %
end
I want my stop the iteration process when difference between the values of the function at two successive approximations is less than a specified tol value. I don't know what to add in the above code to do so. I appreciate any help in fixing this problem. Thanks! Regards Shah

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 6 月 2 日
hint:
oldval = inf;
while true
newval = rand(); %some calculation to create a new value
if abs(oldval - newval) < tol
break;
end
oldval = newval;
end

カテゴリ

Help Center および File ExchangeUtilities for the Solver についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by