1 view (last 30 days)

function [r k] = root_finder(f,x0,kmax,tol)

x1 = x0; %initial x1

%for loop for x1 from 2 to kmax as intial value x1 is given

for i = 2 : kmax

%evaluating function f @x1 and comparing with tolerance given

if tol > abs(feval(f,x1))

r = x1; % if |f(xk)| < tol then we have to stop

k = i;

break;

else

x1 = x1 + 0.01; %incrementing x1 with 0.01

end

end

end %function ends

Ayush Gupta
on 11 Sep 2020

The history of tolerance and corresponding x values can be stored if we treat them as vectors and in each iteration of for loop the value at that point is stored. Refer to the following code to see how it works:

function [r, k] = root_finder(f,x0,kmax,tol)

x1 = x0; %initial x1

%for loop for x1 from 2 to kmax as intial value x1 is given

for i = 2 : kmax

%evaluating function f @x1 and comparing with tolerance given

r(i) = abs(feval(f,x1));

k(i) = x1;

% if |f(xk)| < tol then we have to stop

if tol >r(i)

break;

end

x1 = x1 + 0.01; %incrementing x1 with 0.01

end

end %function ends

Opportunities for recent engineering grads.

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

Start Hunting!
## 1 Comment

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/590650-i-am-trying-to-store-the-values-of-each-iteration-of-0-01-in-my-for-loop-into-a-matrix-could-anyone#comment_1001422

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/590650-i-am-trying-to-store-the-values-of-each-iteration-of-0-01-in-my-for-loop-into-a-matrix-could-anyone#comment_1001422

Sign in to comment.