Error in optimisation code (division by an OptimizationVariable not supported.in prob.objective)

1 view (last 30 days)
I am currently building a basic optimisation code to maximise d and t within specific constraints. Once the code works I will develop ity further but for now I am stuck with the error Division by an OptimizationVariable not supported. After reviewing this on the forums I am still no closer to working out what I am doing wrong. Any advice would be appreciated. Thank you.
%% Clear workspace
clear;
clc;
%% Input Variables
l=14830; % Member length (mm)
k=1; % Effective length factor
%% Equations
prob = optimproblem('ObjectiveSense','max');
d = optimvar('d', 1,'LowerBound',0);
t = optimvar('t',1,'LowerBound',0);
prob.Objective= (d/t) + ((k*l)/r);
%% Variables
din=d-(2*t); % Member inner diameter
a=3.1415/4 * ((d^2)-(din^2)); % Member cross sectional area
i=3.1415/64 * ((d^4)-(din^4)); % Member second moment of inertia
r=(a/i)^0.5; % Member radius of gyration
%% Constraints
cons1 = d>=0;
cons2 = d<=3000;
cons3 = t>=0;
cons4 = t<=100;
cons5 = d/t<=30;
cons6 = k*l/r<=40;
prob.Constraints.cons1 = cons1;
prob.Constraints.cons2 = cons2;
prob.Constraints.cons3 = cons3;
prob.Constraints.cons4 = cons4;
prob.Constraints.cons5 = cons5;
prob.Constraints.cons6 = cons6;
show(prob)
sol = solve(prob);
sol.d
sol.t
The error I am getting is:
Error using optim.internal.problemdef.Rdivide.getRdivideOperator
Division by an OptimizationVariable not supported.
Error in /
Error in optimizejacketframe (line 15)
prob.Objective= (d/t) + ((k*l)/r);
  3 Comments
Walter Roberson
Walter Roberson on 6 Oct 2022
r is not defined.
In current MATLAB versions, if you define r with a definite numeric value, the code is accepted.
I notice the poster is using R2018b; it might be necessary to upgrade in order to use division by an optimization variable (even one constrained to be non-zero)
Maximilian
Maximilian on 6 Oct 2022
Amazing thank you for both of your help. Now that I have updated my matlab version the code runs up to the sol = solve(prob) section. Now it is saying Error using optim.problemdef.OptimizationProblem/solve SOLVE requires a non-empty initial point structure to solve a nonlinear problem. So I now assume I need to set an initial guess for the code to run so I will get on with that.

Sign in to comment.

Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!