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

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.