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

 採用された回答

Star Strider
Star Strider 2021 年 11 月 29 日

0 投票

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 件のコメント

Danny Darby
Danny Darby 2021 年 11 月 29 日
You got it. Thanks!
Star Strider
Star Strider 2021 年 11 月 30 日
As always, my pleasure!
.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeOptimization についてさらに検索

製品

リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by