MATLAB: How to color the are in between two fitted curves

curve fitting

Hi,
I have used the fit function on two arrays to emphasize the trend of the values. Since they stem from the same formula with slight variations to the parameters i would like to color the area in between for better visibility. However, when using the functions area, fill or patch on the fitted curves they do not work, since apparently the concatenation of cfit objects not permitted. Is there a way to work around this issue or is there a different possibility to color the area between the curves? I have included a sample of my code below.
x = transpose(2006:1:2019);
vTval1 =[0.4000 0.4202 0.4430 0.4068 0.3827 0.3971 0.4028 0.4097 0.4063 0.4012 0.3869 0.3672 0.3624 0.3589];
vTval2 =[0.4000 0.5966 0.6170 0.5840 0.5614 0.5785 0.5867 0.5961 0.5951 0.5923 0.5801 0.5618 0.5589 0.5575];
hold all
plot(datenum(x,1,1),vTval1,'xblack')
plot(datenum(x,1,1),vTval2,'xblack')
dateFormat = 10;
datetick('x',dateFormat)
vTf1 = fit(datenum(x,1,1),transpose(vTval1),'smoothingspline')
vTf2 = fit(datenum(x,1,1),transpose(vTval2),'smoothingspline')
plot(vTf1,'black')
plot(vTf2,'black')
patch([datenum(x,1,1), fliplr(datenum(x,1,1))], [vTf1, fliplr(vTf2)]) %Here the error occurs
Thank you!

Best Answer

Try this
x = (2006:1:2019).';
vTval1 =[0.4000 0.4202 0.4430 0.4068 0.3827 0.3971 0.4028 0.4097 0.4063 0.4012 0.3869 0.3672 0.3624 0.3589];
vTval2 =[0.4000 0.5966 0.6170 0.5840 0.5614 0.5785 0.5867 0.5961 0.5951 0.5923 0.5801 0.5618 0.5589 0.5575];
X = datenum(x,1,1);
hold all
plot(X,vTval1,'xblack')
plot(X,vTval2,'xblack')
dateFormat = 10;
datetick('x',dateFormat)
vTf1 = fit(X,transpose(vTval1),'smoothingspline');
vTf2 = fit(X,transpose(vTval2),'smoothingspline');
plot(vTf1,'black')
plot(vTf2,'black')
X_new = datenum((2006:0.1:2019).', 1, 1); % better resolution for smooth edges
patch([X_new; flipud(X_new)], [vTf1(X_new); flipud(vTf2(X_new))], 'r') %Here the error occurs
Related Question