How to solve equations with data stored in a cell arrays

3 views (last 30 days)
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!!!

Accepted Answer

Star Strider
Star Strider on 14 Mar 2018
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 Comments
MaAdVi
MaAdVi on 19 Mar 2018
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 on 19 Mar 2018
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!