is not valid for multiple reasons:
- the output of @ is a function handle. Arithmetic operations are not defined for function handles (only for numeric/char/logical arrays).
- 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:
However I doubt that much of the rest of your code does anything useful. In particular:
- 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.
- ditto for bus_frequency.
- max_profit is (implicitly?) defined and then ignored and redefined.
- 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).
- x0 is defined with two elements, whereas most likely you should be providing a scalar for that (intended) function to minimize.
- ... 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)