Main Content

setCustomCost

Class: fxpOptimizationOptions

Set block-level cost functions to customize the optimization objective

Since R2025a

Syntax

setCustomCost(blockName,costFcn)

Description

setCustomCost(blockName,costFcn) sets the cost function specified by costFcn to calculate the contribution of the block type specified by blockName to the objective function.

Input Arguments

expand all

Block type to assign a custom cost function to, specified as a character vector.

You can specify a default cost function to be used by any blocks that are not assigned cost functions by setting the block type as "Default".

Example: "Gain"

Data Types: char

Cost function to apply to a block type specified as a function handle. The function must have only one input variable. The function input is interpreted as the word length of the data type used by the block.

You can assign only one cost function to each block type.

Example: @(x)3*x

Data Types: function_handle

Examples

expand all

Create an fxpOptimizationOptions object.

opts = fxpOptimizationOptions;

Use setCustomCost function. Set custom cost functions to triple the cost of Gain blocks and double the cost of Sum blocks.

opts.setCustomCost("Gain",@(x)3*x,"Sum",@(x)2*x);

Verify that your fxpOptimizationOptions object has been assigned custom cost functions.

hasCustomCostFunctions(opts)
ans = logical
   1

View the block types that have cost function assignments.

getCustomCostFunctionBlockTypes(opts)
ans = 2×1 string
    "Gain"
    "Sum"

Determine whether a specific block has an associated cost function, and view the cost function for that block.

hasCustomCostFunctionFor(opts,"Gain")
ans = logical
   1

getCustomCostFunctionFor(opts,"Gain")
ans = function_handle with value:
    @(x)3*x

Version History

Introduced in R2025a