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

1 次查看(过去 30 天)
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.

回答(1 个)

Ivan van der Kroon
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

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by