Anonymous function/ Loop

29 ビュー (過去 30 日間)
Alyssa
Alyssa 2016 年 2 月 18 日
コメント済み: Andrei Bobrov 2016 年 2 月 18 日
Hello,
I'm trying to solve an anonymous function with fzero but with one of the variable being a vector.
So if
w = linspace(0,1,500);
And my anonymous function is
anon = @(y) y*3-w*0.2;
Solving for each y when the equation is set to zero
answer = fzero(anon,1)
But I am getting the error:
Operands to the and && operators must be convertible to logical scalar values.
Error in fzero (line 308) elseif ~isfinite(fx) ~isreal(fx)
Error in Homework (line 39) answer = fzero(anon,1)
.
My guess would be a loop, but I'm unsure how to go about it. If someone were able to help me out, I would greatly appreciate it!!

採用された回答

Andrei Bobrov
Andrei Bobrov 2016 年 2 月 18 日
編集済み: Andrei Bobrov 2016 年 2 月 18 日
anon = @(y,w) y*3-w*0.2;
w = linspace(0,1,500);
answer = arrayfun(@(y)fzero(@(x)anon(x,y),1),w);
or
anon = @(y,w) y*3-w*0.2;
w = linspace(0,1,500);
answer = zeros(500,1);
for jj = 1:500
answer(jj) = fzero(@(x)anon(x,w(jj)),1);
end
  2 件のコメント
Alyssa
Alyssa 2016 年 2 月 18 日
Thank you for responding! My problem is a little trickier than that though, but I tried your method and am still getting an error.
B = 4;
a = 2*pi;
sigma = 0.1;
anon = @(y,rbar) y^2*8*(2/pi)* acos(exp(-((B/2)*((1-y)/rbar))))-sigma*a*(rbar*(0.3-0.2*rbar)-y);
rbar = linspace(0,1,500);
answer = arrayfun(@(y)fzero(@(x)anon(x,y),1),rbar);
Andrei Bobrov
Andrei Bobrov 2016 年 2 月 18 日
B = 4;
a = 2*pi;
sigma = 0.1;
anon = @(y,rbar) y^2*8*(2/pi)* acos(exp(-((B/2)*((1-y)/rbar))))-sigma*a*(rbar*(0.3-0.2*rbar)-y);
rbar = linspace( eps, 1,500);
answer = arrayfun(@(y)fzero(@(x)anon(x,y),1),rbar);

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by