Question

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.

Était-ce utile?

La solution

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top