Trouble creating function handle with matrices in matlab

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.

 Accepted Answer

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.

More Answers (0)

Categories

Find more on Mathematics and Optimization in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!