In [1]:
using PyPlot

n = 60;

function f(x::Number)
    y = 20*x^2*10^(-1.5*x);
end

srand(84);
u = sort(rand(n)*2);
w = 1+randn(n)*0.1;
v = f.(u).*w;

figure();
plot(u,v,"o");
grid("on");
axis("square");
xlim(0, 1);
ylim(0, 1);
xlabel("u");
ylabel("v");
title("Raw data");
/usr/local/lib/python2.7/dist-packages/matplotlib/cbook/deprecation.py:107: MatplotlibDeprecationWarning: Passing one of 'on', 'true', 'off', 'false' as a boolean is deprecated; use an actual boolean (True/False) instead.
  warnings.warn(message, mplDeprecation, stacklevel=1)
In [3]:
X = ones(n,3);
X[:,2] = u;
X[:,3] = log(u);
y = log(v);

theta_opt = X\y;

vp = linspace(0.001,2,100);

X_vp = ones(length(vp),3);
X_vp[:,2] = vp;
X_vp[:,3] = log(vp);

figure();
plot(u,v,"o");
plot(vp,exp(X_vp*theta_opt));
grid("on");
axis("square");
xlim(0, 1);
ylim(0, 1);
xlabel("u");
ylabel("v");
title("Monomial-exponential fit");