Index exceeds the number of array elements (1)

조회 수: 7 (최근 30일)
Dursman Mchabe
Dursman Mchabe 2018년 10월 22일
댓글: Dursman Mchabe 2018년 10월 22일
Hi everyone, I know this error is self-explanatory. In line 34 of the attached code, I indexed to the end of the independent variable, thinking that it will prevent the error. There seems to be still yet another small think that lead to this error
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 37)
x(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 37)
x(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.
>>
Debugging points me to line 37.
Please help.

채택된 답변

Stephan
Stephan 2018년 10월 22일
편집: Stephan 2018년 10월 22일
Hi,
x(i)
will not work - x has size one - because it is your optimization variable - try this instead:
% 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];
%preallocate sol
sol = zeros(1,numel(t));
%turn off fsolve display messages
options = optimoptions('fsolve', 'Display','off');
for i = 1:length(t)
sol(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, F, options);
end
L = 6 - log10(sol);
plot(t,L,'r--')
hold on
plot (t,K,'kd')
xlabel('Time (sec)')
ylabel('pH')
hold off
legend('pH-Mod','pH-Exp')
Best regards
Stephan
  댓글 수: 1
Dursman Mchabe
Dursman Mchabe 2018년 10월 22일
Thanks a lot, Stephan. It works perfectly.

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by