2015-06-27 32 views
0

我試圖使用fminbnd來查找函數的最大值。我已經使用它與簡單的功能,如f(x) = x^2+2*x,所以找到我做的最小值fminbnd(f,-10,10)。要找到最大值,我只需要做fminbnd(-f,-10,10)如何用2個常量參數找到變量函數的最大值?

現在我有3個變量的另一個功能,但我希望他們的2爲常量:

f(Q,m,Fx) = (Fx^2*(m-1))/sqrt((m*Fx^2-1)^2 + (Fx^2*(Fx^2-1)^2*(m-1)^2*Q^2)) 

我使用ezplot(f(q,m,Fx), [0 8 0 3])qm常數值當前繪製它。如果試圖通過fminbnd(-f(q,m,Fx),0,8)獲得最大值,它會給我一個錯誤。

我怎樣才能找到這個函數的最大值?

回答

1

您可以將函數定義爲函數句柄,然後在調用ezplotfminbnd時使用匿名函數。這將您的問題減少到只有一個變量,因此可以適當處理並且不會產生錯誤。

% define function-handle 
f = @(Q,m,Fx) (Fx^2*(m-1))/sqrt((m*Fx^2-1)^2 + (Fx^2*(Fx^2-1)^2*(m-1)^2*Q^2)); 

% define constants 
q = 1; 
m = 10; 

% plot the curve 
ezplot(@(Fx) f(q,m,Fx), [0 8 0 3]) 

% compute the maximum 
Fx = fminbnd(@(Fx) -f(q,m,Fx),0,8) 
Fy = f(q,m,Fx) 

% plot the maximum as point 
hold on 
plot(Fx,Fy,'*') 
axis([0 8 0 1.2]) 

給出以下結果:

result