Iterative solution to achieve convergence

Hello,
I would like to ask you to help me to correct this interative solution. I would like to achieve solution with precision with 3 decimal places. Unfortunately this does not work.
Best regards
Michal
tol = 3; % tolerance
for i = 1:1:10
f=i^2;
delta = tol-f;
j = i;
if abs(delta) >= 0.1;
for j = j-1:0.1:j;
f=j^2;
delta = tol-f;
k=j;
if abs(delta) >= 0.01;
end
end
end
end;

7 Comments

Could you explain what you are trying to do in your code ?
I must admit that I don't understand it.
Hello Torsten,
I am trying to calculate x in equation: f = x^2 in iterative way so that f was close to 3 with precision 3 decimal places from bottom.
For illustration I am writing here a code which does succesfully what I want. A problem with this code is that instead of f=n^2 I am starting external script and using this approach entire iteration becomes extremely slow and therefore can no be used. It requires too many cycles to converge to given precision. Therefore I am searching for faster way to achieve converence. I mean with "faster' to converge with smaller number of cycles. I think that a good solution could be the script which showed in the first message above, but it does not work as expected so I am asking you for help.
n = 1; % initial value
f=n^2; % funtion to calculate
tol = 3; % tolerance
accuracy = 0.0000001; % accuracy
while f <= tol
n = n+accuracy;
f=n^2;
end
n = n-accuracy;
f=n^2;
disp(['n = ' num2str(n,10)])
disp(['f = ' num2str(f,10)])
Hello,
I have corrected the code from the beginning. I would like to ask you to help me to correct it,make it densier and better looking. Apart from that this code has a problem because l , m and n are the same therefore it does not give solution which I want.
limit = 3; % maximum value for f in equation f =x^2
for i = 0:0.1:2;
f = i^2;
if f >= limit;
break
end
end
;
j=i-0.1;
f = j^2;
disp(['j = ' num2str(j,10)])
disp(['f = ' num2str(f,10)])
for j = j:1e-2:2;
f = j^2;
if f >= limit;
break
end
end
;
k=j-1e-2;
f = k^2;
disp(['k = ' num2str(k,10)])
disp(['f = ' num2str(f,10)])
for k = k:1e-3:2;
f = k^2;
if f >= limit;
break
end
end
;
l=k-1e-3;
f = l^2;
disp(['l = ' num2str(l,10)])
disp(['f = ' num2str(f,10)])
for l = l:1e-4:2;
f = l^2;
if f >= limit;
break
end
end
;
m=l-1e-4;
f = m^2;
disp(['m = ' num2str(m,10)])
disp(['f = ' num2str(f,10)])
for m = l:1e-5:2;
f = m^2;
if f >= limit;
break
end
end
;
n=m-1e-5;
f = n^2;
disp(['n = ' num2str(n,10)])
disp(['f = ' num2str(f,10)])
If your function is monotonic then consider using a binary search. Iterate with larger steps until you are able to bracket the solution, then reduce the step size and keep bracketing.
Hello Walter,
could you show me binary search on this little example f = x^2 (precision 3 decimal places)?
Hello, can anyone help me?
There are a number of posts showing binary search, several with complete code.
Finding a target value f(x) = t is often rewritten as g(x) = f(x) - t, and at that point you are looking for a zero crossing for g(x). The place where g(x) is 0 is the place where f(x) is the target value. Use any convenient root finding techniques.
The kind of situation where you might use iteration is a case where you are required to find the smallest x>x0 such that f(x) = t, and you are given a minimum distance between matching values but there may be a large number of matches. Figuring out whether there are an even number of matches in an interval can be awkward.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2020b

Asked:

on 17 May 2022

Commented:

on 18 May 2022

Community Treasure Hunt

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

Start Hunting!