Using custom function with matlab Optimization Toolbox

31 views (last 30 days)
I'm trying to make a model that optimizes certain perameters of a system based on empirical results. There's really 5 optimization parameters that I can tweak to try and get the model to most closely match my empirical results. Those 5 parameters are Yield, NormalPlumeCoeff, NonNormalPlumeCoeff, AngleFromTargetNormal, and Pct.
I'm trying to use matlab's Optimization Toolbox and Global Optimization toolbox to do this, but I keep getting errors that I think are related to syntax but I'm not sure. Here's the code so far:
ModelOptimization = optimproblem("Description", "Optimize the parameters.");
%Define variables
Yield = optimvar("Yield", "LowerBound", .1, "UpperBound", 1.2);
NormalPlumeCoeff = optimvar("NormalPlumeCoeff", "LowerBound", 1, "UpperBound", 5);
NonNormalPlumeCoeff = optimvar("NonNormalPlumeCoeff", "LowerBound", 1, "UpperBound", 5);
AngleFromTargetNormal = optimvar("AngleFromTargetNormal", "LowerBound", 0, "UpperBound", 1.5);
Pct = optimvar("Pct", "LowerBound", 0, "UpperBound", 1);
%Definte Objective Function
ExpPlanetaryThk1 = [655.40 683.400 688.833 655.020];
CalculatedPlanetaryThk1 = Planetary_Rotation5(50, AngleFromTargetNormal, Pct, 1.2502165, 1200.055, 39.948, "Ta2O5", Yield, NormalPlumeCoeff, NonNormalPlumeCoeff, 4.6, 59.45, false);
PlanetaryThk1Polynomial = polyfit([0 11.8 18.3 24.2], CalculatedPlanetaryThk1, 3);
PlanetaryThk1Approximation = [polyval(PlanetaryThk1Polynomial, 0), polyval(PlanetaryThk1Polynomial, 11.8), polyval(PlanetaryThk1Polynomial, 18.3), polyval(PlanetaryThk1Polynomial, 24.2)];
Plan1Difference = abs(ExpPlanetaryThk1-PlanetaryThk1Approximation);
ModelOptimization.Objective = sum(Plan1Difference)*2;
%Solve Optimization Problem
sol = solve(ModelOptimization);
I have the empirical values stored in the variable "ExpPlanetaryThk1". The function "Planetary_Rotation5" is a function that calculates a similiar vector of 1x4 values.- I don't believe the contents of this function are necessary to help answer this question, but if I'm wrong please let me know.
Currently this code give me the following error:
Error using cell2mat
CELL2MAT does not support cell arrays containing cell arrays or objects.
Error in Planetary_Rotation5 (line 111)
There is some use of cell2mat in Planetary_Rotation5, but it seems like the above code is trying to call the function with the optimvar's rather than with the values they could take on. When I debug into Planetary_Rotation5, it's evident that the variables I'm passing in as optimvar's are structs and not doubles (which they should be). and cell2mat is getting upset with the fact that it's trying to use a sturct instead of a double value.
I feel like I must be doing something syntactically wrong in the setup and calling of the optimization, but I can't figure out what?

Answers (1)

Walter Roberson
Walter Roberson on 21 Aug 2023
To have any hope of getting further you will need to use https://www.mathworks.com/help/optim/ug/fcn2optimexpr.html
However your reference to cell2mat() suggests to me that your code might contain functions that cannot be converted.

Community Treasure Hunt

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

Start Hunting!