Function value and YDATA sizes are not equal BUT THEY ARE

1 view (last 30 days)
Hi everyone,
I have a problem when I try to fit a non-linear function. The message I received says: FUNCTION VALUE AND Y DATA SIZES ARE NOT EQUAL"
BUT I already checked those sizes and seems to be equal. I don't know what is the problem. Hope somebody could help me.
This is the code:
%REFERENCE:
% "The physical chemistry of high-sensitivity differential scanning
% calorimetry of biopolymers" by Stephen Leharne in ChemTexts (2017)
% 3:1 [DOI 10.1007/s40828-016-0038-0]
clc, clear;
%--------------------------------- INPUT ---------------------------------
Data_Fig_7 = [303.58, 0.09
304.55, 0.56
305.56, 1.05
306.52, 1.48
307.52, 1.96
308.51, 2.56
309.43, 3.06
310.41, 3.55
311.43, 4.17
312.37, 4.76
313.36, 5.31
314.36, 5.95
315.39, 6.58
316.38, 7.14
317.36, 7.96
318.23, 8.46
318.94, 9.24
319.89, 10.01
320.80, 10.74
321.37, 11.47
322.08, 12.47
322.91, 13.69
323.48, 14.77
323.91, 15.72
324.31, 16.88
324.85, 18.19
325.36, 19.83
325.88, 21.70
326.39, 23.75
326.91, 26.10
327.43, 28.53
327.98, 31.62
328.50, 34.80
329.02, 38.64
329.53, 42.51
330.05, 46.64
330.47, 50.58
330.98, 54.15
331.50, 57.44
332.11, 59.66
332.82, 60.80
333.58, 60.23
333.55, 59.76
334.12, 57.88
334.64, 54.67
335.15, 50.73
335.71, 46.63
336.21, 42.70
336.63, 39.12
337.17, 35.88
337.68, 33.13
338.20, 30.93
338.71, 28.90
339.23, 27.30
339.74, 26.15
340.30, 25.23
341.00, 23.92
341.79, 23.11
342.69, 22.58
343.68, 22.23
344.67, 21.96
345.67, 21.77
346.66, 21.70
347.66, 21.63
348.64, 21.56
349.62, 21.48
350.61, 21.49
351.59, 21.51
352.52, 21.48
353.50, 21.47
354.50, 21.41
355.49, 21.32
356.46, 21.34
357.47, 21.32
358.46, 21.32
359.46, 21.11
360.47, 21.06
361.45, 20.93
362.26, 20.98];
Hvh = 400; %kJ mol-1
Hcal = 300; %kJ mol-1
R = 0.00831; %kJ mol-1 K-1
Tr = 332.4; %K Obtained form the Table 2
cp = 20; %kJ mol-1 Obtained from the Fig 7
%------------------------------- Calculations -----------------------------
%Figure 7
T7 = Data_Fig_7(:,1);
Cp7 = Data_Fig_7(:,2);
x(1) = Hvh;
x(2) = Hcal;
%K0 = 1*exp(-((x(1)/R) * (1./T7 + 1/Tr)) + ((cp/R)*((log(T7./Tr))-1+(Tr./T7))))
%fD0 = K0./(1+K0)
%Cp0 = ((x(1)*x(2))./(R.*(T7).^2)) .* (fD0.*(1-fD0)) + (fD0.*(x(2)/x(1))*cp)
K = @(T7,x) 1*exp(-((x(1)/R) * (1./T7 + 1/Tr)) + ((cp/R)*((log(T7./Tr))-1+(Tr./T7))))
Ka = K(T7,x)
fD = @(T7,x) K(T7,x)./(1+K(T7,x))
fDa = fD(T7,x)
Cp = @(T7,x) ((x(1)*x(2))./(R.*(T7).^2)) .* (fD(T7,x).*(1-fD(T7,x))) + (fD(T7,x).*(x(2)/x(1))*cp)
Cpa = Cp(T7,x)
[x,resnorm,~,exitflag,output] = lsqcurvefit(Cp,x,T7,Cp7)

Accepted Answer

John D'Errico
John D'Errico on 19 Nov 2021
Edited: John D'Errico on 19 Nov 2021
The expectation is that in the function (here, Cp) the data vector should be the SECOND argument.
It needs to be
Cp = @(x,T7) ((x(1)*x(2))./(R.*(T7).^2)) .* (fD(T7,x).*(1-fD(T7,x))) + (fD(T7,x).*(x(2)/x(1))*cp);
If you are not sure, read the help.
min sum {(FUN(X,XDATA)-YDATA).^2} where X, XDATA, YDATA and the
X values returned by FUN can be
vectors or matrices.
So the first argument to fun should have been x, and the second argument T7. MATLAB cares about the sequence you provide the arguments. :)
  1 Comment
Leonardo Gómez
Leonardo Gómez on 19 Nov 2021
Thank you for answering ! It is working now! I completely missed that order.

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by