using fzero to solve nonlinear equation

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!

回答(1 个)

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 ?

4 个评论

When I set (t) as the only function handle input, it returns the error:
Operands to the || and && operators must be convertible to logical scalar values.
Error in fzero (line 307)
elseif ~isfinite(fx) || ~isreal(fx)
Error in rosalesLab101 (line 31)
s(i)=fzero(conc,8);
I am not quite sure what this means
No loop needed:
s = arrayfun(@(T) fzero(@(s) km*log(s0/s) + (s0-s) - vmax*T, 8), t)
what is 'T' in this solution?
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

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Surrogate Optimization 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by