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 CenterFile Exchange에서 Conversion 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