# Why am I getting error "Array indices must be positive" and error in syms, when using eval function

1 回表示 (過去 30 日間)
Maisie Williams 2022 年 2 月 9 日

Why am I getting errors when using the eval function?
syms x
f(x) = 3.5*x*(1-x)
f(x) =
f2 = f(f(x))
f2 =
eqn2 = f2 == x;
sol= (solve(eqn2,x))
sol =
eval(f2(sol(2)))
Array indices must be positive integers or logical values.

Error in sym/subsref (line 997)
R_tilde = builtin('subsref',L_tilde,Idx);
I have used the same steps to evaluate f with no errors.
Any pointers would be really appreciated.

### 採用された回答

Highphi 2022 年 2 月 9 日
I think it's because you need to make f, f2, and eqn2 functions of x (even though I drop eqn2)
syms x f f2 % << here
f(x) = 3.5*x*(1-x) % and see f(x) instead of just f
f(x) =
f2(x) = f(f(x)) % etc
f2(x) =
% eqn2(x) = f2(x) == x %% not necessary
% sol = solve(eqn2, x) %% not necessary
sol = solve(f2(x) == x, x)
sol =
eval(f2(sol(2)))
ans = 0.4286
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Maisie Williams 2022 年 2 月 10 日
Thanks so much!

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

### カテゴリ

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

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by