Iterative solution to achieve convergence
Show older comments
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
Torsten
on 17 May 2022
Could you explain what you are trying to do in your code ?
I must admit that I don't understand it.
M Teaxdriv
on 18 May 2022
Edited: M Teaxdriv
on 18 May 2022
M Teaxdriv
on 18 May 2022
Edited: M Teaxdriv
on 18 May 2022
Walter Roberson
on 18 May 2022
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.
M Teaxdriv
on 18 May 2022
M Teaxdriv
on 18 May 2022
Walter Roberson
on 18 May 2022
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.
Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!