Problem with minimizing optimization function with multiple variables

5 ビュー (過去 30 日間)
Meaghan
Meaghan 2017 年 9 月 10 日
編集済み: John D'Errico 2017 年 9 月 10 日
I'd like to minimize the function "x^2 +5y^2 +4xy-30x-60y+219". When I did it by hand, I got my minimum to be at 0,15, but I'd like to double check.
My total program is:
function b = f(v)
x = v(1);
y = v(2);
b = (x^2 + 5.*y^2 + 4.*x.*y -30.*x - 60.*y + 219);
v = [0,15]; %guess
a = fminsearch(@f,v)
When I ran the program, the program gave me an error that I didn't have enough input arguments. I think I had enough, since similar programs had similar code.

採用された回答

Walter Roberson
Walter Roberson 2017 年 9 月 10 日
function a = f_driver
v = [0,15]; %guess
a = fminsearch(@f,v);
function b = f(v)
x = v(1);
y = v(2);
b = (x^2 + 5.*y^2 + 4.*x.*y -30.*x - 60.*y + 219);

その他の回答 (1 件)

John D'Errico
John D'Errico 2017 年 9 月 10 日
編集済み: John D'Errico 2017 年 9 月 10 日
Defining this m-file...
function b = myfun(v)
x = v(1); y = v(2);
b = (x^2 + 5.*y^2 + 4.*x.*y -30.*x - 60.*y + 219);
Now, test it out.
v0 = [0,15];
vfinal = fminsearch(@myfun,v0)
vfinal =
15 -2.9839e-05
Is the solution correct? Of course. We can verify that.
syms x y
fsym = (x^2 + 5.*y^2 + 4.*x.*y -30.*x - 60.*y + 219);
sol = solve(gradient(fsym) == 0,x,y)
sol =
struct with fields:
x: [1×1 sym]
y: [1×1 sym]
sol.x
ans =
15
sol.y
ans =
0
Since you don't show how you tried it out, I cannot know why it gave you that error. My guess is you are still at the novice stage where you think you can use the "run" button on everything. I wish they would just get rid of the run button in the editor.

カテゴリ

Help Center および File ExchangeSystems of Nonlinear Equations についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by