Info

此问题已关闭。 请重新打开它进行编辑或回答。

Finding the maximum value of a function

1 次查看(过去 30 天)
som
som 2013-2-17
Hi all,
I have a non-linear function like y=3*x^2+5*x+9 wich comes with some constraint as below:
3*x-5<= 16
x^2-2<=8
I want to find the maximum value of y subjected to these constraint. How can I write this program?
Thanks in advance.

回答(2 个)

Image Analyst
Image Analyst 2013-2-17
Is this your homework? If so, please make at least some attempt at some code first. A hint though: first I'd solve those equations analytically to find out what your valid range for x is, like x can go between a and b. Then I'd use linspace(a, b, numberOfElements) to get a list of x values in an array (row vector). You then put that in to your equation for y, but use ".^" instead of "^" because x is an array of values, not a single value and you want to square all the values element by element. This way you can avoid the loop, which wouldn't take much time at all but avoiding the loop and writing vectorized code is the more MATLAB-ish way of doing things.
  2 个评论
som
som 2013-2-17
thanks.
can you please guid me with its code. it's urgent for me.
Image Analyst
Image Analyst 2013-2-17
x = linspace(a, b, numberOfElements); % Like I already said
y=3*x.^2+5*x+9;
Now call max(y) and look at both arguments that max() returns. You'll find your answer there. I can't give you a, b, or numberOfElements, or the call to max() because it's your homework and you can' just turn in some solution done 100% by someone else. You've got to do something, other than asking someone else to do it.

Matt J
Matt J 2013-2-17
编辑:Matt J 2013-2-17
If this is a simplified example of a bigger problem, you probably want to use FMINCON. If this is your actual problem, here are a few hints
  1. It's often good in small problems to check first whether the unconstrained minimizer satisifes the constraints.
  2. It's easy to see here that the constraints can't be simultaneously active. For example, 3*x-5<= 16 is active only at x=7, but x=7 will not satisfy the second constraint. Therefore, the Lagrange multiplier equations will at most involve the second constraint, not the first one.

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by