Hi Guys, I need help in writing code for finding difference between two successive values of a variable in for loop.
    4 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
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
0 comentarios
Respuestas (1)
  Walter Roberson
      
      
 el 2 de Jun. de 2016
        hint:
oldval = inf;
while true
   newval = rand();               %some calculation to create a new value
   if abs(oldval - newval) < tol
     break;
   end
   oldval = newval;
 end
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

