So i am working on an assembly language program to determine the optimum dimensions of a closed cylindrical can, such as those used for canning food products. There are three input variables, which i have already created the calculus portion in assembly language code:
The cost of the end material in dollars/cm2.
The cost of the side material in dollars/cm2.
The volume of the can in milliliters.
Given these three input variables, i have determined the dimensions (height and diameter) of the can such that the cost of the can is minimized. Again i have came up with the calculus portion to solving this program but curious as to what a brute force would look like using a do
or while
loop. How would one go about doing to create a brute force that generates pretty much the same output as the calculus answer, for example:
Enter the cost of end material per square cm:
.001
Enter the cost of the side material per square cm:
.003
Enter the desired volume in milliliters:
100
Calculus Answer:
Can cost: 0.24
Diameter: 7.25
Height: 2.41
Brute Force Answer:
Can cost: 0.24
Diameter: 7.25
Height: 2.41
The calculus portion that i have come up with resulting in a calculus answer output is:
********** CONSTANTS **********
TWO: EQU $40000000
PI: EQU $40490FDA
ONE_THIRD: EQU $3EAAAAAb
START_R: EQU $3C23D70A
*******************************
start: initIO * Initialize (required for I/O)
initF
setEVT
lineout p1
floatin buffer
cvtaf buffer,D5 * END cost
lineout p2
floatin buffer
cvtaf buffer,D6 * SIDE cost
lineout p3
floatin buffer
cvtaf buffer,D7 * VOLUME
**********************************************************************
** Calculus Answer
** Formula for the radius of the optimum can:
** radius = (((volume*side_cost)/(2*PI*end_cost))^(1/3)
** numerator, volume*side_cost:
move.l D7,D1 * VOLUME
fmul D6,D1 * VOLUME*SIDE_COST
** denominator, 2*PI*end_cost
move.l D5,D2 * END_COST
fmul #TWO,D2 * END_COST * 2.0
fmul #PI,D2 * END_COST * 2.0 * PI
** now take result to 1/3 power
fdiv D2,D1 * numerator/denominator
move.l #ONE_THIRD,D0
fpow D1,D0 *(numerator/denominator) ^ (1/3)
** Calulus answer done, now calculate diameter, height, cost
move.l D0,D1 * D1 has radius
fmul #TWO,D0 * D0 has diameter
cvtfa diameter,#2
** calculate height = (volume / PI*r^2)
move.l D1,D2 * radius
fmul D2,D2 * radius^2
fmul #PI,D2 * radius^2*PI
move.l D7,D3 * copy of volume
fdiv D2,D3 * vol / PI*radius^2 HEIGHT --> D3
move.l D3,D0
cvtfa height,#2
** calculate cost = SIDE_COST*SIDE_SURFACE + 2*END_COST*END_SURFACE
*** side cost:
move.l #PI,D2
fmul #TWO,D2 * 2*PI
fmul D1,D2 * 2*PI*radius
fmul D3,D2 * 2*PI*radius*height = side surface area
fmul D6,D2 * side surface area * SIDE_COST
*** end cost:
move.l #PI,D0
fmul #TWO,D0 * 2*PI
fmul D1,D0 * 2*PI*radius
fmul D1,D0 * 2*PI*radius*radius
fmul D5,D0 * 2*PI*radius*radius*END_COST
fadd D2,D0
cvtfa cost,#2
** DONE, print the calculus answer
lineout ans1
lineout ans2
lineout ans3
How might it be if one wanted to create a brute force for this program using a 'do' or 'while' loop like below. Can someone help me.
radius = 0.01
lastCost = Calculate
do:
radius = radius+0.01
newCost = Calculate
if(newCost lastCost)
goto print
lastCost = newCost
goto loop
print lastcost
just curious as to what a brute force method might look like for this, im pretty sure it is basically the same code but just adding a couple of lines of code. I just want to know where might i add those lines of code.
Well if I understand you correctly you only have to calculate ALL combinations of height and width and take the lowest cost.the problem here is to find an interval which will include the best answer, but also a stepsize which is feasible for you current problem (otherwise you will have too many possibilities) You also need to set the side condition with height*width²*pi = fixed volumina because if the volumina is smaller (which could happen from some combinations) then the cost would also be smaller