Fzero using for loop

2 ビュー (過去 30 日間)
Jose Grimaldo
Jose Grimaldo 2020 年 4 月 9 日
回答済み: Stephen23 2020 年 4 月 10 日
I have an anonymous function, im trying to find each fzero value as variable x in the anonymous function (AC) goes from 0:1:10, how can I do that?
Eq1 = @(A) 2A + A^2 - 5A
Eq2 = @(A) 3A + A^3 - 2A
B=5;
AC = @(A) B - 2 * integral(Eq1,5,A) - x * integral(Eq2,5,A);
j=1;
for x=0:1:10
Z(j)=fzero(AC(x),0)
j=j+1
end

採用された回答

Stephen23
Stephen23 2020 年 4 月 10 日
You need to parameterize the function:
Note that it is usually simpler and more robust to loop over indices rather than looping over data, e.g.:
Eq1 = @(A) 2*A + A.^2 - 5*A;
Eq2 = @(A) 3*A + A.^3 - 2*A;
B = 5;
AC = @(A,x) B - 2*integral(Eq1,5,A) - x*integral(Eq2,5,A);
X = 0:1:10;
N = numel(X);
Z = nan(0,N);
for k = 1:N
Z(k) = fzero(@(A)AC(A,X(k)),0);
end
Giving:
>> Z
Z =
5.2309 5.033 5.0178 5.0122 5.0092 5.0074 5.0062 5.0054 5.0047 5.0042 5.0038

その他の回答 (1 件)

darova
darova 2020 年 4 月 10 日
AC = @(A,x) B - 2 * integral(Eq1,5,A) - x * integral(Eq2,5,A);
%%
Z(j) = fzero(@(A)AC(A,x),0)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by