function [x_fine, y_fine] = interpCubicSpline2D(x_orig, y_orig, x_fine)
% interpCubicSpline2D  Interpolate y vs x using a cubic spline for finer x spacing.
%
%   [x_fine, y_fine] = interpCubicSpline2D(x_orig, y_orig, x_fine)
%
%   Inputs:
%     x_orig : vector of original x values (1D, strictly increasing)  
%     y_orig : vector or same-size array of original y values (1D)  
%              Must be same length as x_orig.  
%     x_fine : vector of new x values (1D) at which to compute interpolated y.
%
%   Outputs:
%     x_fine : the same vector of new x values (for convenience)  
%     y_fine : vector of interpolated y values (1D) corresponding to x_fine.
%
%   Notes:
%     - The original x_orig must be sorted (increasing).  
%     - The function performs cubic spline interpolation.  
%       You may also use `interp1(x_orig,y_orig,x_fine,'spline')` or `spline(x_orig,y_orig,x_fine)`.  
%     - If x_fine includes values outside the range of x_orig, the interpolation will extrapolate  
%       (depending on MATLAB version you may get NaNs or warnings).
%
%   Example:
%     x_orig = [0, 1, 2, 3];  
%     y_orig = [0, 1, 4, 9];  
%     x_fine = 0:0.1:3;  
%     [xf, yf] = interpCubicSpline2D(x_orig, y_orig, x_fine);
%     plot(x_orig, y_orig,'o', xf, yf,'-');
%
%   See also spline, interp1.

    % --- Input validation ---
    if nargin < 3
        error('Three input arguments are required: x_orig, y_orig, x_fine');
    end
    x_orig = x_orig(:);   % ensure column
    y_orig = y_orig(:);
    x_fine = x_fine(:);
    
    if length(x_orig) ~= length(y_orig)
        error('x_orig and y_orig must have the same length');
    end
    if any(diff(x_orig) <= 0)
        error('x_orig must be strictly increasing');
    end
    
    % --- Perform cubic spline interpolation ---
    % We can use interp1 with 'spline' for simplicity:
    y_fine = interp1(x_orig, y_orig, x_fine, 'spline');
    
    % Optionally: you could use MATLAB’s spline function directly:
    % y_fine = spline(x_orig, y_orig, x_fine);
    
    % --- Output ---
    x_fine = x_fine;   % just returning as output for clarity

end
