How to solve for subscripted assignment mismatch error in for loop?

I am trying to solve an equation for a variable given another variable values and a parameter. Gamma is the given variable and I will solve for K. I tried using for loop, but I cannot get the solution due to an error: Subscripted assignment dimension mismatch.
c=5;
rho=0.06;
mu=0;
psi=1;
i=1;
sigma = 0.1;
gamma = 0:0.01:0.2;
beta_1 = 1/2 - mu/sigma^2 + sqrt((mu/sigma^2 - 1/2)^2 + 2*rho/sigma^2);
solK = zeros(1,21);
for n = 0:20
K = sym('K');
eqn = (beta_1-2)*psi*K/(c*rho) - (rho + beta_1*0.01*n*K)/(rho + 0.01*n*K)^2 == i/c;
solK(1,n+1) = solve(eqn, K);
end

4 件のコメント

Geoff Hayes
Geoff Hayes 2015 年 3 月 29 日
Anuudari - please copy and paste the full error message so that we know exactly at which line (in your above code) the error is being thrown. The problem is most likely with
solK(1,n+1) = solve(eqn, K);
and this could be because the result from
solve(eqn, K)
is a symbolic array and not a double/scalar. Try stepping through the code (using the debugger) to see what is happening at this line.
Anuudari Burenbat
Anuudari Burenbat 2015 年 3 月 30 日
Thanks, Geoff,
Yep the error is here: Subscripted assignment dimension mismatch.
Error in DRi (line 14)
solK(1,n+1) = solve(eqn, K);
Stalin, when I do just that it gets another error: In an assignment A(I) = B, the number of elements in B and I must be the same.
Geoff Hayes
Geoff Hayes 2015 年 3 月 31 日
Anuudari - what does solve(eqn, K) return? Put a breakpoint at this line and then run your code. When the debugger pauses here, run
solve(eqn, K)
in the Command Window. Look at the result and its type by using class. When you have done so, does it make sense to assign the above to
solK(1,n+1)
Anuudari Burenbat
Anuudari Burenbat 2015 年 4 月 2 日
I worked it out thanks Geoff!

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

回答 (1 件)

Stalin Samuel
Stalin Samuel 2015 年 3 月 29 日

0 投票

i am not sure but if you do like below error removed
solK(n+1) =solve(eqn, K);

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2015 年 3 月 29 日

コメント済み:

2015 年 4 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by