How do I use fsolve with an array input?

5 vues (au cours des 30 derniers jours)
Mohammed Rahman
Mohammed Rahman le 10 Avr 2020
Commenté : darova le 7 Mai 2020
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 commentaires
darova
darova le 12 Avr 2020
What is ?
cMj = Ce_MB and Ce_C % KNOWN
cRj = CR_MB and CR_C % UKNOWN
Mohammed Rahman
Mohammed Rahman le 12 Avr 2020
Coi is the same as CRj.

Connectez-vous pour commenter.

Réponse acceptée

darova
darova le 12 Avr 2020
Here is my attempt without success. No solution found. Is there a mistake i made?
function main
opt = optimset('display','on');
[res_MB,fMB] = fsolve(@(x)func(x,1),ones(1,3),opt);
[res_C,fC] = fsolve(@(x)func(x,2),ones(1,3),opt);
[res_MB(:) fMB(:)]
function res = func(x,ind)
Ce_MB = [0.330 0.230 0.188 0.262];
Ce_C = [0.272 0.180 0.141 0.207];
k_MB = 22.213;
k_C = 35.975;
n_MB = 0.198;
n_C = 0.234;
if ind == 1
cMj = Ce_MB;
Ki = k_MB;
ni = n_MB;
else
cMj = Ce_C;
Ki = k_C;
ni = n_C;
end
qMj = [1.386 0.919 0.868 1.024];
mj = [3.983 6.558 7.683 5.817];
Lj = 0.02;
%%%%%%%% UNKNOWN %%%%%%%%%%%%%%%%%%
% cRj = CR_MB and CR_C % Just to iterate this is two separate arrays.
% phi
% qRj
phi = x(1);
qRj = x(2);
cRj = x(3);
c0i = x(3);
ftmp1 = (phi.*ni./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( ftmp2.^2 );
res = [f1 f2 f3];
  20 commentaires
Mohammed Rahman
Mohammed Rahman le 7 Mai 2020
Hi Darova,
Apologies about popping up again, however, I was wondering if you could help me! This is another variation of your answer, however, my classmate and I are having massive issues with regards to the variable mj (Line 4). In our solve function; Phi_qRJ (Line 15), we would like to solve for each element of the mj array, is this possible? Whenever we try to use the arrayfun with fsolve we keep getting "matrix dimesions do not not agree".
mj is used in:
  • Line 6 (qAC)
  • Line 27 (Z2)
  • Line 28 (Z3)
In essence I would like the code to use one value of mj at a time, rather than using the entire array. I appeciate any help you can offer!
function IAST_2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% INPUTS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
global mj Lj qAC K n C0i Phi qRj
mj = [0.1,0.2,0.3,0.4,0.5]; % Mass of Activated Carbon (g)
Lj = 0.1; % Volume of Solution (dm3)
qAC = mj./Lj; % Activated Carbon Loading in Solution (g/dm3)
K = [49.1103, 69.49];
n = [0.2095, 0.19727];
C0i = [1,1];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% SOLVE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
opt = optimset('display','on');
x0 = ones(1,2);
Phi_qRj = fsolve(@(x)G1_G2(x),x0,opt);
Phi = Phi_qRj(:,1)
qRj = Phi_qRj(:,2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Equations_G1_G2 = G1_G2(x)
global mj Lj qAC K n C0i Phi qRj
Phi = x(1);
qRj = x(2);
Z1 = ((Phi*n)./K).^(1./n)
Z2 = (qRj * qAC)
Z3 = C0i./(Z1+Z2);
Z4 = C0i./(Z1+Z2).*(1./n);
G1 = sum(Z3) + -1;
G2 = sum(Z4) + -(Phi/qRj);
Equations_G1_G2 = [G1 G2]';
darova
darova le 7 Mai 2020
Try for loop

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by