Contenido principal

addCustomTolerance

R2026b

Add custom tolerance

Since R2026b

    Description

    Add-On Required: This feature requires the Optical Design and Simulation Library for Image Processing Toolbox add-on.

    tolSet = addCustomTolerance(tolSet,fcnHandle,value) adds a custom tolerance defined by fcnHandle to the tolerance set tolSet, with an absolute tolerance range defined by value. The tolerance set, tolSet, is an opticalToleranceSet object. This function adds a CustomTolerance object to the Tolerances property of tolSet and appends the details of the tolerance to the ToleranceTable property of tolSet.

    tolSet = addCustomTolerance(tolSet,fcnHandle,value,arg1,...,argN) passes additional input arguments arg1,...,argN to the custom tolerance function.

    example

    tolSet = addCustomTolerance(___,Name=name) specifies a name for the custom tolerance.

    Examples

    collapse all

    Import an optical system into the workspace.

    opsys = zmximport("PhotographicLens.zmx");

    Create an empty optical tolerance set.

    tolSet = opticalToleranceSet;

    Add a custom tolerance function to the tolerance set. The custom function modifyGap is defined at the end of this example.

    tolSet = addCustomTolerance(tolSet,@modifyGap,[-0.1 0.1])
    tolSet = 
      opticalToleranceSet with properties:
    
            Tolerances: [1×1 optics.tolerance.CustomTolerance]
        ToleranceTable: [1×4 table]
    
    

    Create a merit function object for evaluating system performance.

    meritFcn = opticalMeritFunction;
    meritFcn = addSpot(meritFcn);

    Run a sensitivity analysis on the optical system using the merit function and custom tolerance.

    resultSensitivity = opticalSensitivity(opsys,meritFcn,tolSet);

    Run a Monte Carlo tolerance analysis with 100 trials on the optical system using the same merit function and tolerance.

    resultTolerance = opticalTolerance(opsys,meritFcn,tolSet,NumTrials=100);

    Custom Function

    The modifyGap function computes how much to adjust the gap after a specific component and updates the component position. The function gets the current gap after a given component, increases or decreases it according to the perturbation value, and then updates the position of the component to reflect the new gap value.

    function [opsys,requiredGap] = modifyGap(opsys,gapValue)
        compIndex = 2;
        if length(opsys.Components) <= compIndex
            error("Invalid component index")
        end
        currentGap = distanceAfter(opsys,compIndex);
        requiredGap = currentGap-gapValue;
        opsys.Components(compIndex).Position(3) = opsys.Components(compIndex).Position(3)+requiredGap;
    end

    Input Arguments

    collapse all

    Tolerance set to which to add the custom tolerance, specified as an opticalToleranceSet object.

    Custom tolerance function, specified as a function handle. The function must accept at least two input arguments: an opticalSystem object and a scalar perturbation value. It must return exactly two output arguments: the modified opticalSystem object and the nominal scalar value.

    You can write the custom function in this format.

    function [newopsys,nominalValue] = myCustomFunction(opsys,perturbationValue,arg1,..., argN)
        % add any supporting code here
    
        % define nominal value
        nominalValue = ...
    
        % return modified optical system
        newopsys = ...
    end

    Data Types: function_handle

    Tolerance range, specified as a 1-by-2 numeric vector of the form [min max] representing the absolute perturbation range. Unlike built-in tolerances, custom tolerances do not support percentage values.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    Additional input arguments for the custom tolerance function, specified as a comma-separated list. The types of the inputs depend on the function fcnHandle. These arguments are inputs to fcnHandle after the optical system and perturbation value arguments.

    Tolerance name, specified as a string scalar or character vector. Use this argument to identify the custom tolerance in the tolerance set table.

    Data Types: char | string

    Output Arguments

    collapse all

    Updated tolerance set, returned as an opticalToleranceSet object with the custom tolerance appended. This function adds a CustomTolerance object to the Tolerances property of tolSet and appends the details of the tolerance to the ToleranceTable property of tolSet.

    Version History

    Introduced in R2026b