exteriorPenalty
Description
Examples
Calculate Exterior Penalty for Point
This example shows how to use the exteriorPenalty
function to calculate the exterior penalty for a given point, with respect to a bounded region.
Calculate the penalty value for the point 0.1
within the interval [-2
,2
], using the step
method.
exteriorPenalty(0.1,-2,2,"step")
ans = 0
Calculate the penalty value for the point 4
outside the interval [-2
,2
], using the step
method.
exteriorPenalty(4,-2,2,"step")
ans = 1
Calculate the penalty value for the point 4
outside the interval [-2
,2
], using the quadratic
method.
exteriorPenalty(4,-2,2,"quadratic")
ans = 4
Calculate the penalty value for the point [-2
,0
,4
] with respect to the box defined by the intervals [0
,1
], [-1
,1
], and [-2
,2
] along the x, y, and z dimensions, respectively, using the quadratic
method.
exteriorPenalty([-2 0 4],[0 -1 -2],[1 1 2],"quadratic")
ans = 3×1
4
0
4
Visualize Penalty Values for an Interval
Create a vector of 1001 equidistant points distributed between -5
and 5
.
x = -5:0.01:5;
Calculate penalties for all the points in the vector, using the quadratic
method.
p = exteriorPenalty(x,-2,2,"quadratic");
Plot the points and add grid, labels, and title.
plot(x,p) grid xlabel("point position"); ylabel("penalty value"); title("Penalty values over an interval");
Input Arguments
x
— Point for which penalty is calculated
scalar | vector | matrix
Point for which the exterior penalty is calculated, specified as a numeric scalar, vector, or matrix.
Example: [-0.1, 1.3]
xmin
— Lower bounds
scalar | vector | matrix
Lower bounds for x
, specified as a numeric scalar, vector, or
matrix. To use the same minimum value for all elements in x
,
specify xmin
as a scalar.
Example: -2
xmax
— Upper bounds
scalar | vector | matrix
Upper bounds for x
, specified as a numeric scalar, vector, or
matrix. To use the same maximum value for all elements in x
,
specify xmax
as a scalar.
Example: [5 10]
method
— Function used to calculate the penalty
'step'
| 'quadratic'
Function used to calculate the penalty, specified either as
'step'
or 'quadratic'
. You can also use strings
instead of character vectors.
Example: "quadratic"
Output Arguments
p
— Penalty value
nonnegative vector
Penalty value, returned as a vector of nonnegative elements. With either of the two
methods, each element pi
is zero if the
corresponding xi
is within the region
specified by xmini
and
xmaxi
, and it is positive otherwise.
Penalty functions are typically used to generate negative rewards when constraints are
violated, such as in generateRewardFunction
.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Version History
Introduced in R2021b
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)