0

I'm a beginner to matlab coding so any help would be appreciated.

I'm trying to minimize difference of summation squared problem SUM((a-b)^2) for 2 variables. I've already coded it up in Excel's Solver like this:

Goal= Sum[{i, 9}, ( Y[i]- (X[i]*m+b) )^2 ] using nonlinear methods.

where Y and X and arrays, and m and b are the variables we are trying to find by minimizing the sum. How would go do this same thing in Matlab?

thanks.

Jon
  • 55
  • 6

1 Answers1

1

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
David
  • 8,449
  • 1
  • 22
  • 32
  • Thanks David, this is looking great. I do need constraints that m and b are nonnegative. Since you're passing in [1 1] as the starting values for m and b, how do we put the constraints in? – Jon Apr 08 '14 at 14:04