How to solve equations with data stored in a cell arrays

조회 수: 3 (최근 30일)
MaAdVi
MaAdVi 2018년 3월 14일
댓글: Star Strider 2018년 3월 19일
Hi I am trying to solve the following equation: Eqn = (a/2)*log((Rs+a)/(Rs-a))== Ind; I can solve it for individual values of Ind using the functions solve as follows:
Ind = 13;
Rs = 5;
syms a
Eqn = (a/2)*log((Rs+a)/(Rs-a))== Ind;
Sol = solve(Eqn,a);
The problem comes when Ind data are stored in a cell array cell(1,n) and each cell contains (m x n) arrays. I would like to solve the equation for the first column of the array contained at each cell and organize the solution also in a cell array where sol{i}(1:end) comes from Ind{i}(1:end,1) I have tried:
Ind = cell(1,10);
Sol = cell(1,10);
Eqn = cell(1,10);
for i = 1:10
Ind{i} = (0:3:15)';
end
Rs = 5;
for i = 1:10
syms a
Eqn{i} = (a/2)*log((Rs+a)/(Rs-a))== Ind{i}
Sol{i} = solve(Eqn{i},a);
end
But of course it does not work. I have also tried other combination and even indexing the data with two for loops taking i and j for numbering cell number and row but I just have no idea how to treat these kind of data. Thanks a lot in advance for any advice!!!

채택된 답변

Star Strider
Star Strider 2018년 3월 14일
I would simply do this:
syms a
Ind = 13;
Rs = 5;
Ind = 0:3:15;
for i = 1:numel(Ind)
Sol{i} = solve((a/2)*log((Rs+a)/(Rs-a))== Ind(i),a);
end
  댓글 수: 4
MaAdVi
MaAdVi 2018년 3월 19일
Ok cool! thanks
I have tried this
syms a
for i=1:numFiles
for j=1:length(Ind{i})
Sol{i}(j) = solve((a/2)*log((Rs+a)/(Rs-a))== Ind{i}(j),a);
end
end
But it does not work... Then I have tried
for i=1:numFiles
for j=1:length(Ind{i})
Sol{j,i} = solve((a/2)*log((Rs+a)/(Rs-a))== Ind{i}(j),a);
end
end
And more or less works (I get the solutions for all set of data) although it gives me more cells than I wanted since I just want sol = cell(1,10). What am I missing?
Thank you!
Star Strider
Star Strider 2018년 3월 19일
My pleasure.
If you only want ‘Sol{1,10}’, assign it to another variable and use that in the rest of your calculations:
Result = Sol{1,10};
You might also find the ‘matlabFunction’ function helpful if you want to convert ‘Result’ into an anonymous function. You can then use it to provide numeric results in the rest of your code.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by