I have a matrix x1 and I am trying to use nlinfit to eventually obtain values of b, b(1) and b(2).
I believe that my function handle is incorrectly doing matrix multiplication, however I cannot figure out what I must do to fix it.
x1 = 0:0.05:1;
x2 = 1-x1;
T = 313.15;
R_const = 8.314;
mdlfunc = @(b,x1) -R_const*T(x1.* log(x1 + b(1).* x2) + x2.* log(x2 + b(2).* x1));
A = 1286.46130;
for i = 1:numel(x1)
gE_exp(i) = A*x1(i)*x2(i);
end
b0 = rand(1,2);
[b,R,J,CovB,MSE] = nlinfit(x1,gE_exp,mdlfunc,b0);
my error message:
Error using nlinfit (line 213)
Error evaluating model function '@(b,x1)-R_const*T(x1.*log(x1+b(1).*x2)+x2.*log(x2+b(2).*x1))'.
Error in Proj1B (line 28)
[b,R,J,CovB,MSE] = nlinfit(x1,gE_exp,mdlfunc,b0)
Caused by:
Array indices must be positive integers or logical values.

 採用された回答

Steven Lord
Steven Lord 2020 年 11 月 18 日

1 投票

mdlfunc = @(b,x1) -R_const*T(x1.* log(x1 + b(1).* x2) + x2.* log(x2 + b(2).* x1));
You meant to multiply T by the quantity (x1.* log(x1 + b(1).* x2) + x2.* log(x2 + b(2).* x1)) but the way you wrote it you indexed into the (scalar) T with that quantity, which is unlikely to always be equal to 1.

1 件のコメント

Conor Luppnow
Conor Luppnow 2020 年 11 月 18 日
wow, I am kicking my self right now. Thank you very much.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by