How to maximise the objective function with constraints and plot the solution

13 views (last 30 days)
I want to maximise the objective function with respect to x
function=a*ln(1+x)+b(c-x)
subjected to constraints : x,min<=x<=x,max
given parameters are a,b,c.(Randamly I choose some values but it shows error as 'Index exceeds the number of array elements. Index must not exceed 1",how to correct this?)
Also plot the x verses function graph.

Accepted Answer

William Rose
William Rose on 27 Feb 2022
@Ancy S G, Since this is a simple function of one adjustable variable, use calculus: differentiate and solve for where the derivative is zero.
therefore
which equals zero when
.
Test if . If x_opt is NOT in that range, then the value that maximizes the function inside the range will be at one end or the other of the range, so simply evaluate both endpoints and pick the higher one.
To demonstrate that this is true, let's pick random values for a,b,c. Then we will compute x_opt and y(x_opt). Then we will plot y(x) over a range to see if we got the maximum right.
Try it.
  4 Comments
Ancy S G
Ancy S G on 1 Mar 2022
Sir,I have a doubt.Can we find the optimal point of this function on the graph without providing the theoretical maximum value?
William Rose
William Rose on 1 Mar 2022
@Ancy S G, Yes. Use fminbnd() to minimize a function of one variable. See attached code.
In the script, at the bottom, I define function ancy1(x,a,b,c). In the main body of the code, I use fminbnd() to find the minimum of -ancy1() with respect to x. Then I take the negative of that, to get the maximum of +ancy1(). Results are displayed graphically and on the console. The maximum found by calculus matches the minimum found numerically.

Sign in to comment.

More Answers (1)

William Rose
William Rose on 27 Feb 2022
The routine fmincon() is good for this. The name is short for function minimization with constraints. Minimize the negative of your function in order to maximize.

Community Treasure Hunt

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

Start Hunting!