Why does my for-loop substitute only one value throughout 31 iterations?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Dursman Mchabe
el 22 de Oct. de 2018
Editada: Dursman Mchabe
el 22 de Oct. de 2018
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 comentarios
Respuesta aceptada
dpb
el 22 de Oct. de 2018
Editada: dpb
el 22 de Oct. de 2018
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 comentarios
dpb
el 22 de Oct. de 2018
Editada: dpb
el 22 de Oct. de 2018
...
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?
Más respuestas (0)
Ver también
Categorías
Más información sobre Interpolation of 2-D Selections in 3-D Grids en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!