문제

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.

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top