Code covered by the BSD License  

Highlights from
Fit Multi Dimensional Polynomial

from Fit Multi Dimensional Polynomial by Yohanan Sivan
The example fits between CMYK to LAB. It assume that LAB is a polynomial function of CMYK.

main
function main
%%
% This is example how to fit multi dimensional polynomial
%
% The example fits between CMYK to LAB
% It assume that LAB is a polynomial function of CMYK
% The polynomial is limited by the deg, which is max(sum(n1,n2,n3,n4))
% The polynomial is in the form:
%
%       Y=...
%       a(1) * (c^0) * (m^0) * (y^0) * (k^0) +...
%       a(2) * (c^1) * (m^0) * (y^0) * (k^0) +...
%       a(3) * (c^0) * (m^1) * (y^0) * (k^0) +...
%       a(4) * (c^0) * (m^0) * (y^1) * (k^0) +...
%       a(5) * (c^0) * (m^0) * (y^0) * (k^1) +...
%       ...
%
% The example is base on the mathematics in:
% http://mathworld.wolfram.com/LeastSquaresFittingPolynomial.html
%


%% starting
fclose all; close all; clear all;
clc; fprintf('starting\n'); pause(0.1);


%% defining
filename='cmykLab.csv';
deg=4;
warning off; %matrixIsCloseToSingular


%% loading
data=csvread(filename);
cmyk=data(:,1:4);
lab=data(:,5:7);


%% fiting
mat=vanMat4dim(cmyk,deg); %creatingTheVanMatrix
coe=((mat'*mat)\mat')*lab; %usingTheVanMatrixToFindCoe
fprintf('===============\n');
fprintf('size(coe)=[%d,%d]\n',size(coe,1),size(coe,2));
labFromFit=vanMat4dim(cmyk,deg)*coe; %usingTheCoeToFindLabFromCmyk


%% checkOneExample
fprintf('===============\n');
fprintf('cmyk=[%d,%d,%d,%d]\n',cmyk(1,1),cmyk(1,2),cmyk(1,3),cmyk(1,4));
fprintf('lab=[%.2f,%.2f,%.2f]\n',lab(1,1),lab(1,2),lab(1,3));
fprintf('labFromFit=[%.2f,%.2f,%.2f]\n',labFromFit(1,1),labFromFit(1,2),labFromFit(1,3));


%% checkMeanDifference
dE=sqrt(sum((lab-labFromFit).^2,2)); %errorInTheMultiDimensional
fprintf('===============\n');
fprintf('mean(dE)=%.3f\n',mean(dE));


%% ending
fclose all; fprintf('ending\n');




Contact us at files@mathworks.com