Maximization of a Function

12 views (last 30 days)
Nazam Ali
Nazam Ali on 16 Sep 2020
Edited: Stephen23 on 16 Sep 2020
Hi All,
I am trying to find the maximization of the following function with bus frequency as a variable.
operator_profit(bus_frequency) =((flow_bus * distance * bus_fare - bus_operation_cost * bus_frequency) + subsidy);
max_profit(bus_frequency) = -@(operator_profit)
max_profit = @(bus_frequency) -((bus_flow*distance*bus_fare - bus_operation_cost * bus_frequency)) + subsidy;
x0 = [0 0];
[xmin, gmin] = fminsearch(max_profit(bus_frequency),x0)
When I run it, it shows following error;
Error: File: Bus_model.m Line: 40 Column: 48
Invalid expression. Check for missing or extra characters.
I don't know how to fix it. Kind guidance and help will be appreciated.

Answers (2)

Stephen23
Stephen23 on 16 Sep 2020
Edited: Stephen23 on 16 Sep 2020
The syntax
.. = -@(..)
is not valid for multiple reasons:
  1. the output of @ is a function handle. Arithmetic operations are not defined for function handles (only for numeric/char/logical arrays).
  2. that syntax specifies one input variable operator_profit, but then inside the function nothing is defined. This is definitely not a valid syntax: a function has to do something. Read more here:
It seems like your goal is to simply take the negative of a function, in order to maximise the function. The correct way to negate the output of a function handle is to call it, which you can also do inside an anonymous function:
baz = @(x) -foo(x);
However I doubt that much of the rest of your code does anything useful. In particular:
  1. you appear to have defined operator_profit as an array, whose existence is of little relevance to the anonymous function because you redefined it as an input argument.
  2. ditto for bus_frequency.
  3. max_profit is (implicitly?) defined and then ignored and redefined.
  4. fminsearch requires a function handle as its first input, but instead of providing it with a function handle you call the function max_profit(bus_frequency) and provide fminsearch with whatever output that has (most likely an array of some kind).
  5. x0 is defined with two elements, whereas most likely you should be providing a scalar for that (intended) function to minimize.
  6. ... probably more bugs, I gave up at that point.
As far as I can tell, your confusion arises from your approach of thinking that you need to include bus_frequency everywhere. You don't. Most likely you should be doing something like this:
max_profit = @(bus_freq) bus_flow*distance*bus_fare - bus_operation_cost*bus_freq + subsidy;
[xmax, gmax] = fminsearch(@(bf)-max_profit(bf),0)

Bjorn Gustavsson
Bjorn Gustavsson on 16 Sep 2020
When I create a sequence of functions like that I always have to have the input-variables explicitly visible on the right-hand side. So this line looks suspicious:
max_profit (bus_frequency) =-@ (operator_profit)
I would have thought that you at least have to make the input-variable to operator_profit visible:
max_profit (bus_frequency) =-@ operator_profit(bus_frequency);
Also the error-message mentions Bus_model but that is something not anywhere else in your message...
HTH
  2 Comments
Nazam Ali
Nazam Ali on 16 Sep 2020
Hi @Bjorn
Thank you for the answer. I have clearly defined input variable "bus_frequency" earlier. Don't know why it's not working. I understand your comment. But error is still persistant.
Bjorn Gustavsson
Bjorn Gustavsson on 16 Sep 2020
Actually even the assignment of max_profit I suggested is wrong. The version I suggested assigns the function-handle (RHS) to some elements determined by the current value of bus_frequency in the array max_profit. What you most likely want is closer to this:
max_profit = @(bus_frequency) -operator_profit(bus_frequency);
There we at least create a function-handle variable max_profit that has an input variable bus_frequency and is the negative of the function operator_profit. That function you could now minimize with fminsearch:
[xmin, gmin] = fminsearch (max_profit, x0)
Or you could do this without the intermediate steps:
operator_profit = @(bus_frequency)((flow_bus * distance * bus_fare --bus_operation_cost * bus_frequency) + subsidy);
[xmin, gmin] = fminsearch (@(b_f) -operator_profit(b_f), x0);
First we have to create a function-handle that depends on the variable that we want to optimize the function over, then, since we want to maximize operator_profit we need to minimize the negative of that function, therefore we create an anonymous function that is just that in the call to fminsearch. I have no idea what the values are for your other parameters in the operator_profit expression, flow_bus, distance, bus_fare, bus_operation_cost, and subsidy have to exist when you define operator_profit - either as variables or functions without inputs.
HTH

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!