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

 Réponse acceptée

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.
.

2 commentaires

Danny Darby
Danny Darby le 29 Nov 2021
You got it. Thanks!
Star Strider
Star Strider le 30 Nov 2021
As always, my pleasure!
.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Optimization dans Centre d'aide et File Exchange

Produits

Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by