How do I use fsolve with an array input?
Show older comments
I have modelled the three equations below on MATLAB using the fsolve function where N is equal to two. The code associated with the problem is also below. The equations analyse the properties of two components, namely MB and C. There are two types of inputs, scalar and arrays and there are two issues I am facing.
The first issue pertains to the number of solutions I receive. I am expecting to receive four outcomes which would be represented by four columns in the "Solution", however, I only receive three. I initially assumed that because I had three equations, fsolve would only provide me with three solutions, however, upon removing one of the equations, this made no difference to the final result. The three solutions I receive are satisfactory but I just require the fourth (column) now! There are further comments in the code to clarify.
The second issue (less important) relates to writing the sum function. I am attempting to use Fsolve for a summation equation, however, I have no clue with regards to how to formulate this. At the moment, I have simply resorted to using an addition because I only have two components. How would I go about doing this.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
k_MB = 22.213; n_MB = 0.198;
k_C = 35.975; n_C = 0.234;
%%%%%%%%%%%%%%%%%%%%%%% ARRAY INPUTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%
AC = [3.983 6.558 7.683 5.817]; V = [0.029 0.030 0.032 0.030];
Ce_MB = [0.330 0.230 0.188 0.262]; Ce_C = [0.272 0.180 0.141 0.207];
qT = [1.386 0.919 0.868 1.024];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
options=optimset('MaxFunEvals',100000e+30,'MaxIter',100000e+30,'TolFun',.00001);
F = @(x) [(1 - ((((x(3)./((((x(1).*n_MB)./k_MB).^(1/n_MB)) + (x(2).*AC)))))+((x(4)./((((x(1).*n_C)./k_C).^(1/n_C)) + (x(2).*AC))))));...
(x(1)./x(2)) - (((((x(3)./(((((x(1).*n_MB)./k_MB).^(1/n_MB)) + (x(2).*AC)))*(1/n_C))))+((x(4)./(((((x(1).*n_C)./k_C).^(1/n_C)) + (x(2).*AC))).*(1/n_C)))));...
((((x(2) - qT)./qT)+((x(3)-Ce_MB)./Ce_MB)).^2)+ ((((x(2) - qT)./qT)+((x(4)-Ce_C)./Ce_C)).^2)];
% from Function F I hope to derive x(1), x(2), x(3) and x(4), however, at the moment it only provides x(1), x(2) and x(4).
%%%%%%%%%%%%%% SOLUTIONS %%%%%%%%%%%%%%%%%%%%
X0 = [Ce_MB;qT;Ce_C]'; % Initial guess, it won't let me do four initial guesses.
Solution = fsolve(F,X0)
Spreading_Pressure = Solution(:,1);
qT_Calculated = Solution(:,2);
C_MB = Solution(:,3)
C_C = Solution(:,4); % The function will not provide this as the Solution only has three columns.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15 Comments
darova
on 10 Apr 2020
I don't understand why your equations are so long? Are summing those value manually?
Also it's unclear which variable is which from the picture
Can you explain?
Mohammed Rahman
on 11 Apr 2020
Torsten
on 11 Apr 2020
You need four equations to determine four unknowns, not only three.
darova
on 11 Apr 2020
I don't see this variable on the picture at all: qT = x(2)
Do i have something wrong with my eyes?
Torsten
on 11 Apr 2020
qRj instead of qT
Torsten
on 11 Apr 2020
You obviously set c0i = cRj in your code and remove the absolute values in the expression for S_3 .
Is it this what you want ?
Mohammed Rahman
on 12 Apr 2020
darova
on 12 Apr 2020
- Is this a good indication that there is something more fundamental wrong with the code?
Definitely YES. Look at your equations. Are you sure they are correct?
Here is how i'd write your equations:
ftmp1 = phini./Ki).^(1/ni) + qRj*mj./Lj;
f1 = 1 - sum(c0i./ftmp1);
f2 = phi/qRj - sum(c0i./ftmp1./ni);
ftmp2 = abs((qRj-qMj)./qMj) + abs((cRj-cMj)./cMj)
f3 = sum( ftmp.^2 )
F = [f1 f2 f3];
They should be maximum simple and clear!
Mohammed Rahman
on 12 Apr 2020
darova
on 12 Apr 2020
Ok. i think we are closer to success now
Ki = k_MB and k_C
ni = n_MB and n_C
c0i = C0_MB and C0_C
But who a these? It remains unclear for me
phini = ?
qRj = ?
qMj = ?
mj = ?
Lj = ?
phi = ?
cRj = ?
cMj = ?
Mohammed Rahman
on 12 Apr 2020
darova
on 12 Apr 2020
Can you fill these constants (put there values)?
phini = ?
qRj = ?
qMj = ?
mj = ?
Lj = ?
phi = ?
cRj = ?
cMj = ?
Mohammed Rahman
on 12 Apr 2020
Edited: Mohammed Rahman
on 12 Apr 2020
darova
on 12 Apr 2020
What is
?
cMj = Ce_MB and Ce_C % KNOWN
cRj = CR_MB and CR_C % UKNOWN
Mohammed Rahman
on 12 Apr 2020
Accepted Answer
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



