Minimising a non lineair function with constraints

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

Matt J
Matt J on 15 Jan 2014
Edited: Matt J on 15 Jan 2014
And if this is realised, it should return to me a value of v = 0 so that Q = 0.
That's not what it looks like in your example. In your formula for Q, it looks like Q goes to -infinity when P, and hence v, is made arbitrarily large.
I'm sorry if I wasn't clear. What I want is that it searches for the minimal amount of hydrogen spent within a set boundaries of power. Then, after this minimal amount has been found, it should put that corresponding power into the formula so it can find the velocity that a car that is powered by the fuel cell should drive so it spends the minimal amount of hydrogen possible.
What I said about the numbers was unclear and I apologise for it. What I tried to say was that the numbers themselves don't make much sense. The fuel cell doesn't have a maximum of 120 W nor does it behave exactly like the formula given, but that is just to sketch the behavior of the fuel cell.
See my Answer, then. I think it's what you've asked for.

Sign in to comment.

 Accepted Answer

Matt J
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

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?
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))

Sign in to comment.

More Answers (0)

Asked:

N/A
on 15 Jan 2014

Commented:

on 16 Jan 2014

Community Treasure Hunt

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

Start Hunting!