# Using an iterative method with scalar functions/function handles (help me fix this code)

4 views (last 30 days)
Felix on 7 Jul 2011
Hello,
I have a function that accepts and returns a scalar. If I code like this, minres returns an incorrect result, stating that the iteration gave a relative residual of 0:
f = @(x)(x.*x);
b = 10;
res = minres(f, b);
The result changes if I specify an initial guess, otherwise 10 in this case. I was assuming that I'm doing something wrong with function handles, but if b = [10;1;5] for example, the relative residual is evaluated correctly (even though there's no convergence). Any comments, ideas?
I am using version R2011a.
Best regards and thanks in advance
edit: In the meantime, I have found a work-around by shifting the function and using fzero to find the root. fzero isn't mentioned in the doc for any of the iterative solvers or even function handles, sadly, so it took me a few hours to stumble over this command.
I'm leaving the question up because the behavior of the code still seems strange to me.

Ivan van der Kroon on 7 Jul 2011
In the doc of fzero 'Find root of continuous function of one variable', which you clearly need here.
Doc of minres 'find a minimum norm residual solution x to the system of linear equations', as your function is not linear it will not work. You can try
f = @(x)(2.*x);
b = 10;
res = minres(f, b)
minres converged at iteration 1 to a solution with relative residual 0.
res =
5

### Categories

Find more on Matrix Indexing 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!