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

1 回表示 (過去 30 日間)
Felix
Felix 2011 年 7 月 7 日
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
Ivan van der Kroon 2011 年 7 月 7 日
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 Center および File ExchangeMathematics についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by