using fzero with arrayfun searching for zeros inside an interval

2 ビュー (過去 30 日間)
Thomas
Thomas 2020 年 12 月 6 日
コメント済み: Thomas 2020 年 12 月 9 日
I have a function f = @(x) -x.^2+4 with a zero at -2 and +2
using fzero(f, [-3, 1.9]) I get the (correct) zero inside the interval [-3, 1.9] , which is -2
when I now have not one but multiple intervals where I want to detect zeros, let's say: [-3, 1.9] and [1.9, +3],
fzero(f, [-3, 1.9; 1.95, 3]) returns an error, telling me, that the second argument must be scalar or a vector of length 2 (= interval).
When I now try to use arrayfun in order to successively apply my intervals to fzero I get:
arrayfun(@(x) feval("fzero" , f, x), [-3 1.9; 1.95 3])
ans =
-2 2
2 2
because fzero does not take intervals, but single values.
However I would like to use intervals and the expected result should be
-2
2
I do not want to use a for loop because of performance issues.
Now the question: Can anyone help how to make "fzero" run on multiple intervals using "arrayfun" (or any other trick) ?

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 12 月 6 日
Create a cell array and then use cellfun()
f = @(x) -x.^2+4;
C = {[-3 1.9]; [1.95 3]};
sol = cellfun(@(x) fzero(f, x), C)
  1 件のコメント
Thomas
Thomas 2020 年 12 月 9 日
Thank you very much - this works fine!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDownloads についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by