Index exceeds array bounds
3 views (last 30 days)
Show older comments
Can anyone help me please? I'm confused, the warning said "Index exceeds array bounds", but I think the array for fzero input didn't exceed array bounds. Is there any solutions? I'll appreciate any of your help. Thank you!
function kinetik
kguess = [0.5 0.1];
konstanta = fzero(@fobj,kguess);
k1 = konstanta (1);
k2 = konstanta (2);
end
function phi = fobj(K)
k1 = K(1);
k2 = K(2);
tspan = [0 3.5];
CFFAdata = 0.096; %konsentrasi final FFA
CMEOdata = 1.636; %konsentrasi final MEO
initial = [2.15 2.39 0 0]; %data nilai awal
[tmodel,Cmodel] = ode23s(@fun,tspan,initial,[],K);
CFFAmodel = Cmodel (length(Cmodel),1);
CMEOmodel = Cmodel (length(Cmodel),3);
phi = (CFFAmodel-CFFAdata)+(CMEOmodel-CMEOdata);
end
function dCdt = fun(t,c,K)
%c(1)= FFA
%c(2)= ME
%c(3)= MEO
%c(4)= S-ester
dCdt = zeros(4,1);
dCdt(1) = -(K(1)+K(2))*c(1)*c(2);
dCdt(2) = -( K(1)+K(2))*c(1)*c(2);
dCdt(3) = K(1)*c(1)*c(2);
dCdt(4) = K(2)*c(1)*c(2);
end
0 Comments
Answers (1)
Riya
on 3 Jun 2025
Hi Azarany,
I understand that you are encountering the "Index exceeds array bounds" error when attempting to use “fzero” in your “kinetic” function to estimate kinetic parameters.
The issue occurs from using “fzero” with a vector input. MATLAB’s “fzero” function is designed strictly for scalar-valued functions and scalar inputs. However, in your implementation, the objective function “fobj” expects a 2-element vector (“K = [k1, k2]”), which “fzero” cannot handle, hence leading to the index error.
To solve this issue, I recommend using “fsolve” instead of “fzero”. The “fsolve” function is built to handle nonlinear equations and supports vector inputs. Kindly refer to the following corrected version of your code:
function kinetik
kguess = [0.5 0.1]; % Initial guess for [k1, k2]
options = optimoptions('fsolve','Display','iter');
konstanta = fsolve(@fobj, kguess, options);
k1 = konstanta(1);
k2 = konstanta(2);
fprintf('k1 = %.4f\n', k1);
fprintf('k2 = %.4f\n', k2);
end
These changes ensure that your input vector “[k1, k2]” is handled appropriately during optimization, and the error related to indexing is resolved.
For further reference, you can refer to the following MATLAB's official documentation on “fsolve” and “fzero”:
0 Comments
See Also
Categories
Find more on Matrix Indexing 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!