% Hints: use ; at end of line to prevent debug'ish output from being printed % Initial plot of just the data points t = [0 .3 .8 1.1 1.6 2.3]'; y = [0.5 0.82 1.14 1.25 1.35 1.40]'; plot(t,y,'o'), grid on X = [ones(size(t)) t t.^2] a = X\y % Fit curve of the data points T = (0:0.1:2.5)'; Y = [ones(size(T)) T T.^2]*a plot(T,Y,'-',t,y,'o'), grid on %% Increase degree of polynomial to increase accuracy of fit OR use linear in parameter regression (below) X = [ones(size(t)) exp(-t) t.*exp(-t)] a = X\y T = (0:0.1:2.5)'; Y = [ones(size(T)) exp(-T) T.*exp(-T)]*a plot(T,Y,'-',t,y,'o'), grid on
In the polynomian regression code snippet, we've tried to fit an equation of the form:
In the second example shown, instead of a polynomial function, I've used a function that is linear-in-the-parameters - an exponential function of the form:
This is the output you can expect to see when you run the above shown .m file.
X = 1.0000 0 0 1.0000 0.3000 0.0900 1.0000 0.8000 0.6400 1.0000 1.1000 1.2100 1.0000 1.6000 2.5600 1.0000 2.3000 5.2900 a = 0.5318 0.9191 -0.2387 Y = 0.5318 0.6213 0.7060 0.7860 0.8612 0.9316 0.9973 1.0582 1.1143 1.1656 1.2121 1.2539 1.2909 1.3231 1.3506 1.3733 1.3912 1.4043 1.4127 1.4163 1.4151 1.4091 1.3984 1.3829 1.3626 1.3375 X = 1.0000 1.0000 0 1.0000 0.7408 0.2222 1.0000 0.4493 0.3595 1.0000 0.3329 0.3662 1.0000 0.2019 0.3230 1.0000 0.1003 0.2306 a = 1.3974 -0.8988 0.4097 Y = 0.4986 0.6212 0.7286 0.8226 0.9047 0.9764 1.0390 1.0934 1.1408 1.1818 1.2174 1.2482 1.2747 1.2976 1.3172 1.3339 1.3482 1.3604 1.3707 1.3793 1.3866 1.3926 1.3976 1.4017 1.4050 1.4077