fzero returns "Too many input arguments"

Function code:
function retf = f(pH, L, bl1, bl2, bl3, bh2, bh1, Ltot, MLratio)
Mtot = MLratio * Ltot;
H = 10^(-pH);
M = Mtot / (1 + bl1*L + bl2*bl1*L^2 + bl3*bl2*bl1*L^3);
Ltot_calculated = L + bl1*M*L + 2*bl2*bl1*M*L^2 + 3*bl3*bl2*bl1*M*L^3 + bh2*H^2*L + bh1*H*L;
retf = Ltot - Ltot_calculated;
end
Want to optimize retf to 0 at specified pH values by varying L
clear all vars
clc
bl1 = 10^(6.78);
bl2 = 10^(11.78);
bl3 = 10^(14.9);
bh2 = 10^(15.9);
bh1 = 10^(9.83);
Ltot = 0.04;
MLratio = 1/3;
'speciation_ni_his_varied_pH_function.m';
fun = @(L)f(7, L, bl1, bl2, bl3, bh2, bh1, Ltot, MLratio);
[L, fval, info, output] = fzero(fun, [0, Ltot]);
L / Ltot
L is an element of 0:Ltot

 采纳的回答

Perhaps the order in the code is wrong. The functions must all be at the end of the script for included functions to work.
This runs without error when I run i t here —
bl1 = 10^(6.78);
bl2 = 10^(11.78);
bl3 = 10^(14.9);
bh2 = 10^(15.9);
bh1 = 10^(9.83);
Ltot = 0.04;
MLratio = 1/3;
'speciation_ni_his_varied_pH_function.m';
fun = @(L)f(7, L, bl1, bl2, bl3, bh2, bh1, Ltot, MLratio);
[L, fval, info, output] = fzero(fun, [0, Ltot]);
L / Ltot
ans = 9.5868e-09
function retf = f(pH, L, bl1, bl2, bl3, bh2, bh1, Ltot, MLratio)
Mtot = MLratio * Ltot;
H = 10^(-pH);
M = Mtot / (1 + bl1*L + bl2*bl1*L^2 + bl3*bl2*bl1*L^3);
Ltot_calculated = L + bl1*M*L + 2*bl2*bl1*M*L^2 + 3*bl3*bl2*bl1*M*L^3 + bh2*H^2*L + bh1*H*L;
retf = Ltot - Ltot_calculated;
end
Experiment to get different results.
.

更多回答(0 个)

类别

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

产品

版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by