# Can I generate a variable function for varying amounts of variables?

1 view (last 30 days)
Louis Alen on 27 Aug 2020
Hello,
As a sense of context, I am trying to create an optimisation algorithm with the design variable being the force on a beam itself (xF) and the objective function being the absolute difference between a measured displacement (d) and the displacement caused by the force applied to the beam. In the simplest form (with only one beam element), the optimiser will optimise the tip displacement to meet the measured tip displacement by adjusting the tip load. With more beam elements, the objective function becomes difficult to write. I have it here for two beam elements:
fobj_val = @(xF) abs((xF(1)+xF(2))*edge^3/(3*E*I)-d(1))...
+abs(xF(2)*edge^3/(3*E*I)-(d(2)-d(1)));
With 'xF' being a 2D array, 'edge' being the beam element length, 'E' and 'I' structural properties and 'd' the 2D array of the measured displacements at the locations where the forces in 'xF' are applied. The question is basically: Is it possible to write a code to automatically write the objective function for varying number of elements ('N')? In the provided objective function, 'N' would be equal to two. Due to the nature of the system, the equation is relatively structured and can be written out in a compiled equation (with summation of, etc.). Feel free to ask what more information you would require or if something is unclear.
The equation can be generally written as: Where the second summation does not sum over all the forces, but for the first node it sums the forces at the first node (F_1), the second node all the second forces (F_1+F_2), etc. As shown in the matlab code above.
Kind regards,
Louis
Louis Alen on 27 Aug 2020
No I haven't, I am unfamiliar with the variety of tools. I will take a look!

Abdolkarim Mohammadi on 27 Aug 2020
In order to achieve scalability in optimization problems or elsewhere, you should use write a general equation for your equations. For example, instead of xF(1)+xF(2) you should write sum(xF,2). The former works only for the special case of N=2, but the latter works for any value of N. Similarly, for your last sentence, I think it is a cumulative sum, which is implemented via cumsum() in MATLAB. In addition, I think using m-file functions are easier to write than inline anonymous function. You can break your equation into multiple parts, which can help write complex formulae accurately.