Cell elements must be character arrays. (Solver equations created in a loop)

조회 수: 9 (최근 30일)
I am trying to solve a system of linear equations numerically, where I have 3 variables and 50 equations. I am creating the euations in a foor loop. here is my code:
Temps_all = [500, 550, 600, 650, 700];
Shear_cal = [0.019, 0.038, 0.058, 0.067, 0.077, 0.096, 0.115, 0.135, 0.154, 0.231];
syms ln_A delta_E delta_X
Yield = [2, 4, 12, 33, 57, 2, 6, 14, 30, 59, 2, 8, 15, 34, 59, 3, 5, ...
11, 33, 55, 7, 8, 12, 44, 58, 6, 12, 32, 44, 58, 12, 15, 36, 58, ...
68, 15, 21, 41, 58, 75, 7, 18, 42, 53, 69, 11, 23, 31, 48, 72, 3, ...
4, 13, 40, 57];
Ar = 42;
kB = 0.001985875;
C = 6.941215543; % Convergence factor
eqns_all = [];
for shear = 1:length(Shear_cal)
for temp = 1:length(Temps_all)
eqn = ln_A -(delta_E-Ar*delta_X*Shear_cal(shear))*C/(kB*Temps_all(temp))...
- log(Yield((shear-1)*length(Temps_all)+temp)) == 0;
eqns_all = [eqns_all;{eqn}];
end
end
S_all = solve(eqns_all,[ln_A delta_E delta_X]);
The euations are created and appended to the equation list eqns_all. But when I run the script I get the following error at the last line where solver is being used. I am not fimiliar with character arrays so any help is greatly appreciated.
Error using char
Cell elements must be character arrays.
Error in solve>isOption (line 459)
b = ~isa(a, 'logical') && any(strcmpi(char(a), ...
Error in solve>getEqns (line 392)
while k <= numel(argv) && ~isOption(argv{k})
Error in solve (line 226)
[eqns,vars,options] = getEqns(varargin{:});
Error in Bell (line 34)
S_all = solve(eqns_all,[ln_A delta_E delta_X]);

채택된 답변

Walter Roberson
Walter Roberson 2020년 8월 11일
eqns_all = [eqns_all;{eqn}];
solve() cannot process a cell array of equations. You should leave out the {}
  댓글 수: 1
Karen Mohammadtabar
Karen Mohammadtabar 2020년 8월 11일
Thank you that works, also changing ";" to "," helps with correcting the dimensions

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Surya Talluri
Surya Talluri 2020년 8월 11일
I understand that you want to solve a system of 50 linear equations for 3 variables. I am not able to reproduce the error on my side.
If the error is persisting, you can try solving the linear equations using “equationsToMatrix” and “linsolve” functions.
[A, B] = equationsToMatrix(eqns_all, [ln_A delta_E delta_X]);
S_all = linsolve(A,B);
You can have a look at the documentation for further understanding - https://www.mathworks.com/help/symbolic/solve-a-system-of-linear-equations.html
  댓글 수: 1
Karen Mohammadtabar
Karen Mohammadtabar 2020년 8월 11일
Thank you but the error is solved by actually doing the following:
eqns_all = [eqns_all;{eqn}]; --> eqns_all = [eqns_all,eqn];

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by