# using fzero to solve nonlinear equation

24 views (last 30 days)
Jordan Rosales on 10 Apr 2021 at 1:12
Commented: Walter Roberson on 10 Apr 2021 at 3:05
I have the nonlinear equation I need to use fzero to solve for s at different times, t.
In my script, I defined known parameters, created a function handle for the nonlinear equation set to zero, created a loop to calculate concentration at each time point, and within the loop used the fzero function. For my estimation I used 8, as a graph created earlier in the problem seems to approach 0 around 10. As shown in the command window there seems to be missing inputs within my function. However, I have checked thoroughly for missing inputs and even utilized the function outside of fzero and it worked fine. I assume the issue is then with fzero, but I am not sure what it is. Any advice is welcome! Walter Roberson on 10 Apr 2021 at 1:24
The function handle you pass to fzero() or to fsolve() must accept only one parameter.
Your s0, vmax, and k are all known constants, and do not need to be passed to the function; when you create the anonymous function, MATLAB will pull them out of the current workspace.
Your code suggests that you are trying to find s that satisfies the expression, but your text description indicates you need to find t ?
Walter Roberson on 10 Apr 2021 at 3:05
T is a dummy variable in an anonymous function. arrayfun() in that form will take each value of t and substitute one value at a time for T into fzero(@(s) km*log(s0/s) + (s0-s) - vmax*T, 8) and collect all of the results.
If you prefer to loop then
tvals = 0:0.1:10;
numt = numel(tvals);
s = zeros(size(tvals));
for tidx = 1 : numt
t = tvals(tidx);
s(tidx) = fzero(@(S) km*log(s0/S) + (s0-S) - vmax*t, 8);
end