Info
此问题已关闭。 请重新打开它进行编辑或回答。
Finding the maximum value of a function
2 次查看(过去 30 天)
显示 更早的评论
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.
1 个评论
回答(2 个)
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 个评论
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
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
- It's often good in small problems to check first whether the unconstrained minimizer satisifes the constraints.
- 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.
0 个评论
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!