"Optimization of foundation pad dimensions
"Single load case problem
'Loads:
'&emsp;Compression -'N = 1000'kN
'&emsp;Bending along x-axis -'M_x = 200'kNm
'&emsp;Bending along y-axis -'M_y = 400'kNm
'Permissible base stress -'R_0 = 200'kPa
'The mean foundation size is determined, using the fixed point method:
'Number of iterations -'n = 10
'Initial value -'a = N/R_0'm
'Solution -'a = $Repeat{a = cbrt((N*a + abs(M_x) + abs(M_y))/R_0) @ k = 1 : n}
'The target function (to be minimized) is the base area:
'<var>A</var> = <var>a</var>·<var>b</var> = min
'The target parameter is the ratio of the dimensions - <var>k</var> = <var>a</var>/<var>b</var>, within the limits:
k_min = 0.2'and'k_max = 5
'Limits for dimensions:
a_min = k_min*a/4'm
a_max = k_max*a*4'm
#if abs(M_x) < abs(M_y)
	k_min = 1'- for |<var>M</var><sub>x</sub>| &lt; |<var>M</var><sub>y</sub>| 
#else
	k_max = 1'- for |<var>M</var><sub>x</sub>| &gt; |<var>M</var><sub>y</sub>| 
#end if
'Theoretical value of the target parameter:
#if M_x ≡ 0
	k = k_min
#else
	k = max(k_min; min(abs(M_y/M_x); k_max))
#end if
'Load eccentricities:
e_x = M_x/N', 'e_y = M_y/N
'The solution must satisfy the following constraints (resistance criteria):
'1.The maximal stress must not exceed the permissible value:
p_max(a; b) = N/(a*b)*(1 + 6*(abs(e_x)/a + abs(e_y)/b))
'2. The minimal stress must be not less than zero (no uplift):
p_min(a; b) = N/(a*b)*(1 - 6*(abs(e_x)/a + abs(e_y)/b))
'Dimension that satisfies the constraints as a function of the target parameter <var>k</var>:
a_p,max(k) = $Find{p_max(a; a*k) - R_0 @ a = a_min : a_max}
a_p,min(k) = $Find{p_min(a; a*k) @ a = a_min : a_max}
a(k) = max(a_p,min(k); a_p,max(k))
'Foundation base area as a function of the target parameter <var>k</var>:
A(k) = a(k)^2*k
'The minimum value of the area and the respective target parameter are calculated by the embedded $Inf function:
A_inf = $Inf{A(k) @ k = k_min : k_max}
k = k_inf'- equal to the theoretical value
'The optimal dimensions of the foundation pad are:
a = a(k)'m, 'b = a*k'm
'The calculated minimal and maximal base stresses are:
p_min(a; b)'kPa
p_max(a; b)'kPa2
#hide
PlotWidth = 300','PlotHeight = 150','PlotSVG = 1
#show
'Plot for target function and solution:
$Plot{A(k) & k_inf|(k - k_min)*A_inf/(k_max - k_min) & k_inf|A_inf @ k = k_min : k_max}
"Multiple load cases problem
'Loads:
'&emsp;Compression -'N(i) = take(i; 500; 1200; 1500; 3000)'kN
'&emsp;Bending along x-axis -'M_x(i) = take(i; 100; -500; 200; 100)'kNm
'&emsp;Bending along y-axis -'M_y(i) = take(i; 200; -200; 800; 250)'kNm
'Permissible base stress -'R_0 = 200'kPa
#hide
a = 0
i = 0
n = 10
#repeat 4
	i = i + 1
	a_i = N(i)/R_0
	a_i = $Repeat{a_i = cbrt((N(i)*a_i + abs(M_x(i)) + abs(M_y(i)))/R_0) @ k = 1 : n}
	#if a_i > a
		a = a_i
	#end if
#loop
#show
'Mean foundation size -'a'm (calculated as maximum from all load cases)
'Limits for the target parameter:
k_min = 0.2','k_max = 4
'Limits for dimensions:
a_min = k_min*a/4'm
a_max = k_max*a*4'm
'Load eccentricities:
e_x(i) = M_x(i)/N(i)', 'e_y(i) = M_y(i)/N(i)
'The solution must satisfy the following constraints (resistance criteria):
'1.The max stress must not exceed the permissible one for each load case <var>i</var>:
p_max,i(a; b; i) = N(i)/(a*b)*(1 + 6*(abs(e_x(i))/a + abs(e_y(i))/b))
'2.The min stress must be not less than zero (no uplift) for each load case <var>i</var>:
p_min,i(a; b; i) = N(i)/(a*b)*(1 - 6*(abs(e_x(i))/a + abs(e_y(i))/b))
'Dimension that satisfies the constraints as a function of the target parameter <var>k</var> and the load case <var>i</var>:
a_p,max,i(k; i) = $Find{p_max,i(a; a*k; i) - R_0 @ a = a_min : a_max}
a_p,min,i(k; i) = $Find{p_min,i(a; a*k; i) @ a = a_min : a_max}
a_i(k; i) = max(a_p,min,i(k; i); a_p,max,i(k; i))
'Foundation pad area as a function of the target parameter <var>k</var>:
A_i(k; i) = a_i(k; i)^2*k
A_max(k) = max(A_i(k; 1); A_i(k; 2); A_i(k; 3); A_i(k; 4))
'The minimum value of the area and the respective target parameter are calculated by the embedded $Inf function:
A_inf = $Inf{A_max(k) @ k = k_min : k_max}
k = k_inf
'The optimal dimensions of the foundation pad are: 
a = sqrt(A_inf/k)'m, 'b = a*k'm
#hide
i = 0
PlotWidth = 400','PlotHeight = 200
#show
'Calculated base stresses:
#val
'<table class="bordered data">
'<tr><th>i</th><th>p<sub>min</sub></th><th>p<sub>max</sub></th></tr>
#repeat 4
	'<tr><td>&ensp;'i = i + 1'</td><td>'p_min,i(a; b; i):0.00'</td><td>'p_max,i(a; b; i):0.00'</td></tr>
#loop
'</table>
#equ
'Plot for target functions and solution:
$Plot{k|A_i(k; 1) & k|A_i(k; 2) & k|A_i(k; 3) & k|A_i(k; 4) & A_max(k) & k_inf|(k - k_min)*A_inf/(k_max - k_min) & k_inf|A_inf @ k = k_min : k_max}
'<p>Legend: &emsp;
'<b style="color:Tomato">━━</b> 1 &emsp;
'<b style="color:YellowGreen">━━</b> 2 &emsp;
'<b style="color:DodgerBlue">━━</b> 3 &emsp;
'<b style="color:Gold">━━</b> 4 &emsp;
'<b style="color:MediumVioletRed">━━</b> max</p>