Minimising a non lineair function with constraints
2 views (last 30 days)
Show older comments
As a novice to Matlab, I am having great difficulty with the simplest of things. My problem is a bit complicated but bear with me.
My program is supposed to do the following: It has to display a curve of power (P) which corresponds to a hydrogen usage of a fuel cell. What it should do is 1) display the curve of P,Q curve 2) calculate a velocity so that the hydrogen usage is minimal.
In order to do that I came up with this:
P (power) = F (force is a known value) * v (velocity) P / F = v
It should vary the power so it calculates the corresponding hydrogen usage. This value is then used for the velocity. It plays with the power until it finds the minimal hydrogen usage and the minimal velocity to achieve that. My current code is like this:
function hydrogenusage
P = 0:1:4000;
Q= (120*P)-(P.^2);
plot(P,Q)
xlabel('Power')
ylabel('Usage')
title('Hydrogen usage')
axis([0 120 0 4000])
grid on
end
In this basic function with the current formula, there are at max only two solutions for P. That's okay.
But how do I make the program vary the power P so that it looks for the minimal Q? And if this is realised, it should return to me a value of v = 0 so that Q = 0. Therefore I want to add a simpel line with 0 < P > 2000.
How can I make it search for the minimal hydrogen usage Q while having a minimum value constraint on it whilst also giving me a value for v? I know Matlab has a function called 'fmincon' which does just that, but I have no idea how to use it here.
If you want to make suggestions but don't know the right numbers: my numbers are entirely arbitrary: they have no value whatsoever and do not reflect the real thing.
3 Comments
Accepted Answer
Matt J
on 15 Jan 2014
Edited: Matt J
on 15 Jan 2014
Q=@(P) (120*P)-(P.^2);
Pmin = fminbnd(Q,0,2000);
vmin=Pmin/F;
2 Comments
Matt J
on 16 Jan 2014
Wouter Commented:
I tried this:
function superhydrogenusage
F = 100
Q=@(P) (120*P)-(P.^2);
Pmin = fminbnd(Q,0,2000);
vmin=Pmin/F;
plot(P,Q)
xlabel('Power')
ylabel('Usage')
title('Hydrogen usage')
axis([0 120 0 4000])
grid on
end
It gave me the following error:
Undefined function or variable 'P'.
Error in superhydrogenusage (line 6)
plot(P,Q)
What went wrong?
Matt J
on 16 Jan 2014
In the statement,
plot(P,Q)
the input P (and also Q) are not defined. In the first version of your code, you defined P as
P = 0:1:4000;
Perhaps you wish to do that again and then plot as
plot(P,Q(P))
More Answers (0)
See Also
Categories
Find more on Manage Products in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!