Syms function no longer works in a for loop which stores data

1 回表示 (過去 30 日間)
Carlos Dolz
Carlos Dolz 2020 年 12 月 2 日
回答済み: Steven Lord 2020 年 12 月 2 日
x= NaN(2,1);
syms F q L;
for i = 1:2
for j = 1:2
K(i,j) = F*(L^(i+j+1))*((((i+1)*(j+1))/(i+j+1))+ ((i*j)/(i+j-1))) - (((2*i*j)+i+j)/(i+j)); % K matrix for the second iteration
x(i,1) = q*(L^(i+2))*((1/(i+3)) - (1/(i+2))); % F vector for the second iteration
end
end
% Solving of the second linear equation of form Kc+F=0 to obtain {c2}
c2 = linsolve(K,-x);
I am getting an error saying it cant convert from sym to double whenever I stry to store K in a matrix and F in its vector. How can this be fixed?
Cheers

回答 (1 件)

Steven Lord
Steven Lord 2020 年 12 月 2 日
If you try to do something like the following:
%{
syms x
F = 0;
F(2) = x;
%}
What numeric value should be stored in F(2) after that code runs? The answer is that x is symbolic and cannot be converted into a numeric value so this will throw an error. If you want to store symbolic data into an array preallocate that array to be a symbolic array from the start.
syms x
F = sym(0);
F(2) = x
F = 
G = sym(zeros(1, 10));
for k = 1:10
G(k) = x.^k;
end
G
G = 

カテゴリ

Help Center および File ExchangeConversion Between Symbolic and Numeric についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by