Here is an example. I've set the bounds by using fmincon
.
x=0:10;
y=x*randi(10)-randi(10)+rand(size(x)); % Create data y
f=@(A) sum((y-(A(1)*x+A(2))).^2) % Test function that we wish to minimise
R=fmincon(f,[1 1],[],[],[],[],[0 0],[Inf Inf]) % Run the minimisation R(1)=m, R(2)=b
plot(x,y,x,R(1)*x+R(2)) % Plot the results