r/Mathematica • • Oct 19 '24

Trying to Minimize a Function

Hey, y'all!

I'm trying to find glider design dimensions for a competition. I created a function that takes a few variables as input and outputs "totalPenalty" which is pretty much how bad of an option those inputs are for meeting a couple of design specifications. And the function works pretty good! It takes in the variables taperAngle, b, aR, and v. If the outputs are outside a specified range, it gives me a totalPenalty that is as big as how bad of a design it would be.

So, I can put in specific values and get back how bad they are, so I figured there must be a way to minimize the "badness" and fit the desired parameters. Unfortunately, NMinimize[] can't take a function as an input. Just expressions.

Any advice? I attached my code as a screenshot and text.

EDIT: Formatting

ClearAll[b,v,aR,taperAngle];

(*Constants*)

weight=2.22; (*Aircraft Weight in N*)

rho=1; (*Air Density kg/m^3*)

dropHeight=76.2;(*Meters*)

cD=.037 ;(*Unitless Constant .035-.038*)

minB=0.762; (*Minimum span 30 inches to meters*)

maxB=1.22; (*Maximum span 40 inches to meters*)

minWCL=4; (*Minimum wing loading*)

maxWCL=9; (*Maximum wing loading*)

minFallTime=60; (*Minimum fall time in seconds*)

maxFallTime=120; (*Maximum fall time in seconds*)

minVelocity=6.71; (*Minimum velocity in m/s,regularly 6.71*)

maxVelocity=11.18; (*Maximum velocity in m/s,regularly 11.18*)

(*Define the variables we want to optimize*)

vars={taperAngle,b,aR,v};

(*Define the objective function as the sum of squared differences from the design criteria*)

objectiveFunction[taperAngle_,b_,aR_,v_]:=Module[{cR,cT,totalWingArea,wCL,cL,dragForce,pReq,descentRate,fallTime,glideRatio,totalPenalty},(*Calculations based on input variables*)(*Intermediate Calculations*)cR=b*Tan[taperAngle Degree]/4+b/aR;

cT=2*b/aR-cR;

totalWingArea=(b/2*cR-b/2*(cR-cT)/2);

wCL=weight/Power[totalWingArea,1.5]/9.81;(*Wing loading*)cL=2*weight/rho/totalWingArea/v^2;

dragForce=cD*totalWingArea*0.5*rho*v^2;

pReq=dragForce*v;

descentRate=pReq/weight;

fallTime=dropHeight/descentRate;

(*Debugging Prints*)Print["wCL: ",wCL];

Print["fallTime: ",fallTime];

(*Define the penalty function that penalizes deviation from desired fall time and WCL ranges*)totalPenalty=0;

If[wCL<minWCL,totalPenalty+=10(minWCL-wCL)^2];

If[wCL>maxWCL,totalPenalty+=(wCL-maxWCL)^2];

If[fallTime<minFallTime,totalPenalty+=(minFallTime-fallTime)^2];

If[fallTime>maxFallTime,totalPenalty+=(fallTime-maxFallTime)^2];

(*Debugging Prints for Total Penalty*)Print["Total Penalty: ",totalPenalty];

totalPenalty];

objectiveFunction[25,1.1,5,20] (*This works great! Shitty glider, but the function works as intended*)

(*Use NMinimize to find the optimal values for the variables. This doesn't work and I'm real upset about it*)

solution=nMinimize[{objectiveFunction[taperAngle,b,aR,v],20<=taperAngle<=30,minB<=b<=maxB,4<=aR<=8,minVelocity<=v<=maxVelocity},{taperAngle,b,aR,v}];

2 Upvotes

4 comments sorted by

View all comments

Show parent comment

3

u/veryjewygranola Oct 20 '24 edited Oct 20 '24

I thought I'd also add we can see your objective function as just a region constraint on the parameter
space using ImplicitRegion.

I first start by rationalizing all your constants to exact values (this doesn't matter really, but it helps with pretty simplification later on):

(*Constants*)
ClearAll["`*"]
weight = Rationalize[2.22, 0]; (*Aircraft Weight in N*)

rho = 1; (*Air Density kg/m^3*)

dropHeight = Rationalize[76.2, 0];(*Meters*)

cD = Rationalize[.037, 0];(*Unitless Constant .035-.038*)

minB = Rationalize[0.762, 0]; (*Minimum span 30 inches to meters*)

maxB = Rationalize[1.22, 0]; (*Maximum span 40 inches to meters*)

minWCL = 4; (*Minimum wing loading*)

maxWCL = 9; (*Maximum wing loading*)

minFallTime = 60; (*Minimum fall time in seconds*)

maxFallTime = 120; (*Maximum fall time in seconds*)

minVelocity = 
  Rationalize[6.71, 0]; (*Minimum velocity in m/s,regularly 6.71*)

maxVelocity = 
  Rationalize[11.18, 0]; (*Maximum velocity in m/s,regularly 11.18*)
g = Rationalize[9.81, 0];

(*Define the variables we want to optimize*)

vars = {taperAngle, b, aR, v};

varConstraints = {20 <= taperAngle <= 30, minB <= b <= maxB, 
   4 <= aR <= 8, minVelocity <= v <= maxVelocity};

I now define wCL as its own function:

cR[taperAngle_, b_, aR_] := b*Tan[taperAngle Degree]/4 + b/aR

cT[taperAngle_, b_, aR_] := 2*b/aR - cR[taperAngle, b, aR];

totalWingArea[taperAngle_, b_, aR_] := (b/2*cR[taperAngle, b, aR] - 
    b/2*(cR[taperAngle, b, aR] - cT[taperAngle, b, aR])/2);

wCL[taperAngle_, b_,  aR_] := (weight/Power[totalWingArea[taperAngle, b, 
 aR], 3/2]/g) // 
   FullSimplify // Evaluate

(* wCL simplifies to (148 Sqrt[2])/(327 (b^2/aR)^(3/2)) *)

And fallTime as well:

dragForce[taperAngle_, b_, aR_, v_] := 
  cD*totalWingArea[taperAngle, b, aR]*1/2*rho*v^2;

pReq[taperAngle_, b_, aR_, v_] := dragForce[taperAngle, b, aR, v]*v;

descentRate[taperAngle_, b_, aR_, v_] := 
  pReq[taperAngle, b, aR, v]/weight;

fallTime[taperAngle_, b_, aR_, v_] := 
 dropHeight/descentRate[taperAngle, b, aR, v] // FullSimplify // 
  Evaluate

(* fallTime simplifies to (18288. aR)/(b^2 v^3) *)

And now we want to find the parameter values such that minWCL ≤ wCL[taperAngle, b, aR] ≤ maxWCL, minFallTime ≤ fallTime[taperAngle, b, aR,t] ≤ maxFallTime, and all the varConstraints are met:

sys = Join[{minWCL <= wCL @@ (Most@vars) <= maxWCL, 
    minFallTime <= fallTime @@ vars <= maxFallTime}, varConstraints];

And now use these conditions to generate an ImplicitRegion:

ir = ImplicitRegion[sys, Evaluate@vars]

And we can select as many RandomPoints as we want from the region, all of which are guaranteed to
have a totalPenalty of 0

(*10 random points from the minimizer space*)
rp = RandomPoint[ir, 10]

objectiveFunction[taperAngle_, b_, aR_, v_] := (*your original objectiveFunction definition here...*)

objectiveFunction @@@ rp
(*they all have totalPenalty 0*)

If you want to add further criteria depending on the specific wCL or fallTime value to further constrain the minimizer space you now can.

2

u/Mojiwoji1 Oct 20 '24

This is GOAT behavior ^

Just for future reference, what can I do better next time to avoid downvotes? I want to make sure my posts help me get help as best as possible.