Why does my for-loop substitute only one value throughout 31 iterations?

조회 수: 1 (최근 30일)
Hi all, on the attached file, I am trying to use the for-loop to substitute values for every iteration. However, it seems like the for-loop only substitute one value. What could be the cause of that?
Kind Regards Dursman
  댓글 수: 3
Dursman Mchabe
Dursman Mchabe 2018년 10월 22일
Thanks for the response Adam. I was not sure where to define G, H, I, J and K. I expect x to change. When I change line 38 to
x(i) = fsolve(@(x) x(i) + 2.* G - ((H.* A.*x(i))/(x(i).^2 + A.*x(i) ...
I get an error message:
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Error in my_fsolve2 (line 38)
x(i) = fsolve(@(x) x(i) + 2.* G - ((H.* A.*x(i))/(x(i).^2 + A.*x(i)...
I'm so grateful of your help.
Dursman Mchabe
Dursman Mchabe 2018년 10월 22일
The current state of the code is:
function my_fsolve3()
% Constant Parameters
A = 6.24;
B = 5.68e-5;
C = 1.7e-3;
D = 6.55e-8;
E = 5.3e-8;
F = 9.46e-1;
t = [0;600;1200;1800;2400;3000;10200;17400;24600;31800;...
39000;46200;53400;60600;67800;75000;82200;89400;96600;103800;...
111000;118200;125400;132600;139800;147000;154200;161400;168600;175800;183000];
G = [0.00;4.88E-06;4.88E-06;5.11E-06;5.11E-06;5.35E-06;5.35E-06;5.36E-06;5.36E-06;5.07E-06;....
5.07E-06;4.93E-06;5.30E-06;5.30E-06;5.30E-06;9.46E-06;9.46E-06;1.13E-05;1.15E-05;1.10E-05;...
1.10E-05;1.04E-05;1.04E-05;1.04E-05;1.03E-05;1.04E-05;1.06E-05;1.13E-05;1.13E-05;1.13E-05;1.03E-05];
H = [0.00;3.34E-01;6.79E-01;1.04E+00;1.41E+00;6.00E+00;1.07E+01;1.56E+01;2.07E+01;2.59E+01;...
3.14E+01;3.67E+01;4.18E+01;4.66E+01;5.09E+01;5.51E+01;5.90E+01;6.23E+01;6.56E+01;6.87E+01;...
7.12E+01;7.36E+01;7.59E+01;7.78E+01;7.95E+01;8.11E+01;8.24E+01;8.24E+01;8.23E+01;8.21E+01;8.20E+01];
I = [0.00;4.88E-06;4.88E-06;5.11E-06;5.11E-06;5.35E-06;5.35E-06;5.36E-06;5.36E-06;5.07E-06;...
5.07E-06;4.93E-06;5.30E-06;5.30E-06;5.30E-06;9.46E-06;9.46E-06;1.13E-05;1.15E-05;1.10E-05;...
1.10E-05;1.04E-05;1.04E-05;1.04E-05;1.03E-05;1.04E-05;1.06E-05;1.13E-05;1.13E-05;1.13E-05;1.03E-05];
J = [1.78E-07;7.41E-06;9.33E-06;1.20E-05;1.05E-05;1.74E-05;3.72E-05;3.55E-05;1.00E-04;4.07E-02;...
2.45E-01;6.17E-01;1.32E+00;2.29E+00;2.34E+00;2.40E+00;1.82E+00;1.38E+00;2.09E+00;1.82E+00;...
1.58E+00;2.29E+00;1.62E+00;1.12E+00;8.91E-01;8.51E-01;7.59E-01;8.71E-01;1.12E+00;1.00E+00;8.51E-01];
K = [9.75;8.13;8.03;7.92;7.98;7.76;7.43;7.45;7.00;4.39;...
3.61;3.21;2.88;2.64;2.63;2.62;2.74;2.86;2.68;2.74;...
2.8;2.64;2.79;2.95;3.05;3.07;3.12;3.06;2.95;3.00;3.07];
for i = 1:length(t)
s(i) = fsolve(@(x) x(i) + 2.* G(i) - ((H(i).* A.*x(i))/(x(i).^2 + A.*x(i) + A.*B))- 2.*((H(i).*A.*B)/(x(i).^2 ...
+ A.*x(i) + A.*B))-((I(i).*C.*x(i))/(x(i).^2 + C.*x(i) + C.*D))- 2.*((I(i).*C.*D)/(x(i).^2 ...
+ C.*x(i) + C.*D))- E/x(i), F);
end
L = 6 - log10(s);
plot(t,L)
hold on
plot (t,K,'v')
xlabel('Time (sec)')
ylabel('pH')
hold off
legend('pH-Mod','pH-Exp')
end
Which gives an error message of
Index exceeds the number of array elements (1).
Error in
my_fsolve3>@(x)x(i)+2.*G(i)-((H(i).*A.*x(i))/(x(i).^2+A.*x(i)+A.*B))-2.*((H(i).*A.*B)/(x(i).^2+A.*x(i)+A.*B))-((I(i).*C.*x(i))/(x(i).^2+C.*x(i)+C.*D))-2.*((I(i).*C.*D)/(x(i).^2+C.*x(i)+C.*D))-E/x(i)
(line 36)
s(i) = fsolve(@(x) x(i) + 2.* G(i) - ((H(i).* A.*x(i))/(x(i).^2 + A.*x(i) + A.*B))- 2.*((H(i).*A.*B)/(x(i).^2 ...
Error in fsolve (line 242)
fuser = feval(funfcn{3},x,varargin{:});
Error in my_fsolve3 (line 36)
s(i) = fsolve(@(x) x(i) + 2.* G(i) - ((H(i).* A.*x(i))/(x(i).^2 + A.*x(i) + A.*B))- 2.*((H(i).*A.*B)/(x(i).^2 ...
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
>>
I trust that I should be able to resolve it in the next few days.

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

채택된 답변

dpb
dpb 2018년 10월 22일
편집: dpb 2018년 10월 22일
function my_fsolve2()
% Constant Parameters
A = 6.24;
B = 5.68e-5;
C = 1.7e-3;
D = 6.55e-8;
E = 5.3e-8;
F = 9.46e-1;
t = [0;600;1200;1800;2400;3000;10200;17400;24600;31800;...
39000;46200;53400;60600;67800;75000;82200;89400;96600;103800;
111000;118200;125400;132600;139800;147000;154200;161400;168600;175800;183000];
G = [0.00;4.88E-06;4.88E-06;5.11E-06;5.11E-06;5.35E-06;5.35E-06;5.36E-06;5.36E-06;5.07E-06;....
5.07E-06;4.93E-06;5.30E-06;5.30E-06;5.30E-06;9.46E-06;9.46E-06;1.13E-05;1.15E-05;1.10E-05;...
1.10E-05;1.04E-05;1.04E-05;1.04E-05;1.03E-05;1.04E-05;1.06E-05;1.13E-05;1.13E-05;1.13E-05;1.03E-05];
H = [0.00;3.34E-01;6.79E-01;1.04E+00;1.41E+00;6.00E+00;1.07E+01;1.56E+01;2.07E+01;2.59E+01;...
3.14E+01;3.67E+01;4.18E+01;4.66E+01;5.09E+01;5.51E+01;5.90E+01;6.23E+01;6.56E+01;6.87E+01;...
7.12E+01;7.36E+01;7.59E+01;7.78E+01;7.95E+01;8.11E+01;8.24E+01;8.24E+01;8.23E+01;8.21E+01;8.20E+01];
I = [0.00;4.88E-06;4.88E-06;5.11E-06;5.11E-06;5.35E-06;5.35E-06;5.36E-06;5.36E-06;5.07E-06;...
5.07E-06;4.93E-06;5.30E-06;5.30E-06;5.30E-06;9.46E-06;9.46E-06;1.13E-05;1.15E-05;1.10E-05;
1.10E-05;1.04E-05;1.04E-05;1.04E-05;1.03E-05;1.04E-05;1.06E-05;1.13E-05;1.13E-05;1.13E-05;1.03E-05];
J = [1.78E-07;7.41E-06;9.33E-06;1.20E-05;1.05E-05;1.74E-05;3.72E-05;3.55E-05;1.00E-04;4.07E-02;...
2.45E-01;6.17E-01;1.32E+00;2.29E+00;2.34E+00;2.40E+00;1.82E+00;1.38E+00;2.09E+00;1.82E+00;...
1.58E+00;2.29E+00;1.62E+00;1.12E+00;8.91E-01;8.51E-01;7.59E-01;8.71E-01;1.12E+00;1.00E+00;8.51E-01];
K = [9.75;8.13;8.03;7.92;7.98;7.76;7.43;7.45;7.00;4.39;...
3.61;3.21;2.88;2.64;2.63;2.62;2.74;2.86;2.68;2.74;...
2.8;2.64;2.79;2.95;3.05;3.07;3.12;3.06;2.95;3.00;3.07];
for i = 1:length(t)
s(i)=fsolve(@(x) x+2*G(i)-((H(i)*A*x)/(x^2+A*x+A*B))-2*((H(i)*A*B)/(x^2 ...
+ A*x+A*B))-((I(i)*C.*x)/(x^2+C*x+C*D))-2.*((I(i)*C*D)/(x^2 ...
+ C*x+C*D))-E/x, x(i));
end
L = 6 - log10(s);
plot(t,L)
hold on
plot (t,K,'v')
xlabel('Time (sec)')
ylabel('pH')
hold off
legend('pH-Mod','pH-Exp')
end
Just taking a stab at what you may have intended... fsolve only solves for a single equation solution at a time, not an array of solutions.
You were overwriting x with the result each pass, not saving a solution and passing the array of time-dependent values instead of solving for each solution ("s") each iteration.
  댓글 수: 3
dpb
dpb 2018년 10월 22일
편집: dpb 2018년 10월 22일
...
for i=1:numel(G)
fnS=@(x)x+2.*G(i)-((H(i).*A.*x)./(x.^2+A.*x+A.*B))-2.*((H(i).*A.*B)./(x.^2+C.*x+C.*D))-E./x;
x(i)=fsolve(fnS,1);
end
L=6-log10(x);
seems to work ok...presuming, of course, that the intent is to solve the equation for each set of coefficients as given in the arrays.
There's an apparent discontinuity in the solution plotted vs the t array; don't know if that's expected, indicative of a typo in one of the coefficient arrays, or just experimental noise/uncertainty/error...
Not sure what I, J, K are for as they don't show up in the solution anywhere; maybe they're some other part of the problem past this point?
Dursman Mchabe
Dursman Mchabe 2018년 10월 22일
편집: Dursman Mchabe 2018년 10월 22일
Thanks a lot dpb. I have since found the solution from your previous answer, the code works perfectly. Thanks once again.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by