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!