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 Exchange에서 Optimization에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2021년 11월 29일

댓글:

2021년 11월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by