Reputation: 6200
I'm trying to find the maximum value of a function using fminbnd
. I have used it with simple functions like f(x) = x^2+2*x
so to find the minimum value I do fminbnd(f,-10,10)
. To find the maximum value I just have to do fminbnd(-f,-10,10)
.
Now I have another function with 3 variables but I want 2 of them to be constant:
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) )
I'm currently plotting it using ezplot(f(q,m,Fx), [0 8 0 3])
being q
and m
constant values. If a try to get the maximum value with fminbnd(-f(q,m,Fx),0,8)
it gives me an error.
How can I find the maximum value of this function?
Upvotes: 0
Views: 1563
Reputation: 13923
You can define the function as function-handle and then use an anonymous function in the call for ezplot
and fminbnd
. This reduces your problem to only one variable, so it can be handled appropriately and generates no errors.
% 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])
Gives the following result:
Upvotes: 1