fitting multiple data simultaneosly

I have a set of data that relates to three different temperatures. I need to use non linear regression to find the optimal parameters that can describe all the three data sets using a model. I can do this in Excel solver and also do for a single data in Matlab but cannot do simultaneously for multiple data IN Matlab. example (not actual data but describes my problem)
@ T=20 i have data P1= rand(6,1) and V1= rand(6,1) @ T=40 i have data P2= rand(4,1) V2=rand(4,1) @ T=60 i have data P3=rand (7,1),V3=rand(7,1).
the model equation that needs to fit all three data is given as
V= (B.exp(C/T))/(A+P) i need to find parameters B,A and C that will fit the above 3 data sets appropriately. please can someone show me how this will be done in Matlab ? as this will save me lots of time doing it in excel (i have lots of data and different equations ). your help will be very much appreciated.

 Accepted Answer

Try this:
P1= rand(6,1);
V1= rand(6,1);
P2= rand(4,1);
V2= rand(4,1);
P3= rand(7,1);
V3= rand(7,1);
x = [repmat(20, size(P1)) P1; repmat(40, size(P2)) P2; repmat(60, size(P3)) P3]; % Independent Variable Matrix
y = [V1; V2; V3]; % Dependent Variable Vector
Vfun = @(b,x) b(1).*exp(b(2)./x(:,1))./(b(3) + x(:,2)); % b(1) = B, b(2) = C, b(3) = A
NRCF = @(b) norm(y - Vfun(b,x));
B0 = rand(3,1);
[Bv, resnorm] = fminsearch(NRCF, B0);
It creates a (17x2) matrix for ‘x’ with ‘T’ in the first column and ‘P’ in the second, and refers to them appropriately in the objective function ‘Vfun’.
The objective function will work with nlinfit and lsqcurvefit. (If you use them, you do not need the ‘NRCF’ (Norm Residual Cost Function) function.)

4 Comments

Thank you very much StarStrider. I've been through the code and applied the changes to my own problem. However the issue am having is that when i use the lsqcurvefit , i get an error. here is the modification of your code to fit my problem:
P1= [0.94 2.19 3.81 5.41 6.93 8.57 9.97 11.46 12.66 13.43 13.92]';
V1= [0.034 0.062 0.089 0.109 0.123 0.135 0.144 0.15 0.154 0.156 0.157]';
P2=[0.74 1.75 3.01 4.52 6.22 8.09 9.86 11.51 12.84 13.73 14.32]';
V2= [0.016 0.034 0.053 0.072 0.092 0.107 0.12 0.129 0.135 0.139 0.142]';
P3=[0.86 2.01 3.35 4.98 6.59 8.17 9.93 11.53 12.67 13.42 13.91]';
V3=[0.015 0.035 0.053 0.072 0.088 0.101 0.114 0.123 0.129 0.132 0.135]';
x = [repmat(35.4, size(P1)) P1; repmat(50.4, size(P2)) P2; repmat(65.4, size(P3)) P3]; % Independent Variable Matrix
y = [V1; V2; V3]; % Dependent Variable Vector
Vfun =@(b,x) (b(1)*exp(-b(2).*x(:,1)).*x(:,2))./(x(:,2)+sqrt(x(:,1))./b(3).*exp(b(4)/x(:,1))); % b(1) = VS, b(2) = Dt, b(3) = A , b(4)= B
x0=[0.1 3 5 1]'; % initialization
[k,resnorm]= lsqcurvefit(Vfun,x0,x,y);
unforutnately this is the error i keep getting : Error using lsqcurvefit (line 251) Function value and YDATA sizes are not equal.
Error in Untitled (line 15) [k,resnorm]= lsqcurvefit(Vfun,x0,x,y);
I have same problem when i use the NRCF you proposed, the values keep the same as the initial values. is there a way around this ? cheers
My pleasure.
You need to vecortise ‘Vfun’ to do element-wise operations. That is probably the reason you are getting the error.
This works, and the plot of the fit seems quite good:
P1= [0.94 2.19 3.81 5.41 6.93 8.57 9.97 11.46 12.66 13.43 13.92]';
V1= [0.034 0.062 0.089 0.109 0.123 0.135 0.144 0.15 0.154 0.156 0.157]';
P2=[0.74 1.75 3.01 4.52 6.22 8.09 9.86 11.51 12.84 13.73 14.32]';
V2= [0.016 0.034 0.053 0.072 0.092 0.107 0.12 0.129 0.135 0.139 0.142]';
P3=[0.86 2.01 3.35 4.98 6.59 8.17 9.93 11.53 12.67 13.42 13.91]';
V3=[0.015 0.035 0.053 0.072 0.088 0.101 0.114 0.123 0.129 0.132 0.135]';
x = [repmat(35.4, size(P1)) P1; repmat(50.4, size(P2)) P2; repmat(65.4, size(P3)) P3]; % Independent Variable Matrix
y = [V1; V2; V3]; % Dependent Variable Vector
Vfun =@(b,x) (b(1).*exp(-b(2).*x(:,1)).*x(:,2))./(x(:,2)+sqrt(x(:,1))./b(3).*exp(b(4)./x(:,1))); % b(1) = VS, b(2) = Dt, b(3) = A , b(4)= B
NRCF = @(b) norm(y - Vfun(b,x));
B0 = rand(4,1);
[Bv, resnorm] = fminsearch(NRCF, B0);
Pmat = [P1 P2 P3];
Tmat = [repmat(35.4, size(P1)) repmat(50.4, size(P2)) repmat(65.4, size(P3))];
Vmat = [V1 V2 V3];
Rmat = reshape(Vfun(Bv,x), size(Vmat));
figure(1)
stem3(Tmat, Pmat, Vmat, 'r', 'Filled')
hold on
surf(Tmat, Pmat, Rmat, 'FaceAlpha',0.5)
hold off
xlabel('T')
ylabel('P')
zlabel('V')
grid on
This returns:
Bv =
0.269119788513771
0.00268652682087009
0.762854833594065
-2.06939914069629
resnorm =
0.0223667604176212
Note that you can probably use lsqcurvefit to get the parameter confidence intervals with the Statistics and Machine Learning Toolbox nlparci function. You might be able to get prediction confidence surfaces with nlpredci, and plot them using the same rehape call used to calculate ‘Rmat’.
Thank you very much , works really well.
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!