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 CenterFile Exchange에서 Utilities for the Solver에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by