Lets assume we have a two dimensional function
def f(x,y): #some calculations here
return value
we know from observations the following
f(x,0.9y) = 10
f(x,0.8y)=15
f(x,0.8y) = 23
...
how can I find the right values for x,y to get the best fit to the observations? which procedure is recommended in case of such an optimization problem?
It seems you have a typo in your question. Anyhow, given that we only have two data points, optimization is not exactly doable, as a set of simultaneous equations gives you an exact result, for many different types of functions.
If you meant to give three data points, there is different approach:
Note that the x is constant. This means that whatever the function is, we cannot say anything about the x portion of the 2D function. So it's really a one dimensional problem: How does y behave?
Given that we know nothing about the data in question, and given the values you gave, I would by gut instinct probably go with an exponential/logarithmic function (assuming the final value is meant to read 0.7). It might also be a linear function, though once you fit a function that you produce, you would have to calculate the error (here is how you would do this). Beyond this, in my experience, there is not much that you can do.
Related
This sounds to be something well explored, so I just need a reference.
I need to take a look (i.e. sketch the plot) to the solution of certain complex equations with several complex parameters and I would like to do that using Python/NumPy.
I have a certain number of complex functions of one complex variable depending on two parameters, and I need to add and compose them and finally to study the set of complex numbers such that the modulus of the above sum and composition is equal to a fixed positive real number.
For short: I have to plot the graph of the solutions of a certain complex equation, and see how does it move when parameters are touched.
Can somebody give a reference, please?
Thanks
I'm working on a panel method code at the moment. To keep us from being bogged down in the minutia, I won't show the code - this is a question about overall program structure.
Currently, I solve my system by:
Generating the corresponding rows of the A matrix and b vector in an explicit component for each boundary condition
Assembling the partial outputs into the full A, b.
Solving the linear system, Ax=b, using a LinearSystemComp.
Here's a (crude) diagram:
I would prefer to be able to do this by just writing one implicit component to represent each boundary condition, vectorising the inputs/outputs to represent multiple rows/cols in the matrix, then allowing openMDAO to solve for the x while driving the residual for each boundary condition to 0.
I've run into trouble trying to make this work, as each implicit component is underdetermined (more rows in the output vector x than the component output residuals; that is, A1.x - b1= R1, length(R1) < length(x). Essentially, I would like openMDAO to take each of these underdetermined implicit systems, and find the value of x that solves the determined full system - without needing to do all of the assembling stuff myself.
Something like this:
To try and make my goal clearer, I'll explain what I actually want from the perspective of my panel method. I'd like a component, let's say Influence, that computes the potential induced by a given panel at a given point in the panel's reference frame. I'd like to vectorise the input panels and points such that it can compute the influence coefficent of many panels on one point, of many points on one panel, or of many points on many panels.
I'd then like a system of implicit boundary conditions to find the correct value of mu to solve the system. These boundary conditions, again, should be able to be vectorised to compute the violation of the boundary condition at many points under the influence of many panels.
I get confused again at this part. Not every boundary condition will use the influence coefficient values - some, like the Kutta condition, are just enforced on the mu vector, e.g .
How would I implement this as an implicit component? It has no inputs, and doesn't output the full mu vector.
I appreciate that the question is rather long and rambling, but I'm pretty confused. To summarise:
How can I use openMDAO to solve multiple individually underdetermined (but combined, fully determined) implicit systems?
How can I use openMDAO to write an implicit component that takes no inputs and only uses a portion of the overall solution vector?
In the OpenMDAO docs there is a close analog to what you are trying to accomplish, with the node-voltage analysis tutorial. In that code, the balance comp is used to create an implicit relationship that is similar to what you're describing. Its singular on its own, but part of a larger group is a well defined system.
You'll need to find a way to build similar components for your model. Each "row" in your equation will be associated with one state variables (one entry in your x vector).
In the simplest case, each row (or set of rows) would have one input which is the associated row of the A matrix, and a second input which is ALL of the other values for x, and a final input which is the entry of the b vector (right hand side vector). Then you could evaluate the residual for that specific row, which would be the following
R['x_i'] = np.sum(A*x_full) - b
where x_full is the assembly of the full x-vector from the x_other input and the x_i state variable.
#########
Having proposed the above solution, I have to say that I don't think this is a particularly efficient way to build or solve this linear system. It is modular, and might give you some flexibility, but you're jumping through a lot of hoops to avoid doing some index-math, and shoving everything into a matrix.
Granted, the derivatives might be a bit easier in your design, because the matrix assembly is going to get handled "magically" by the connections you have to create between the various row-components. So maybe its worth the trade... but i would say you might be better of trying a more traditional coding approach and using JAX or some other AD code to make the derivatives easier.
I have two variables, y being the number of targets achieved and x the number of individuals involved in the operation. The relationship is positive and non-linear (there are only so many individuals you can add before the returns of adding individuals are diminishing), however I am not sure what is the function of x that best predict y.
Using Python, I would like to find the best fitting line/function and identify the maximum of the function to advise how many individuals include in each team to achieve the maximum number of targets [controlling for different variables]
How to do it?
You can use ridge-regression with kernels. kernels helps in transforming linear function to non-linear function without changing its actual form. This is what Graipher was pointing.
Here is a script, which shows an example to use it.
Also, these selection comes with experience and understanding. You can also guess closely(eg. which polynomial can fit in), by visualizing the function form.
I am modeling electrical current through various structures with the help of FiPy. To do so, I solve Laplace's equation for the electrical potential. Then, I use Ohm's law to derive the field and with the help of the conductivity, I obtain the current density.
FiPy stores the potential as a cell-centered variable and its gradient as a face-centered variable which makes sense to me. I have two questions concerning face-centered variables:
If I have a two- or three-dimensional problem, FiPy computes the gradient in all directions (ddx, ddy, ddz). The gradient is a FaceVariable which is always defined on the face between two cell centers. For a structured (quadrilateral) grid, only one of the derivates should be greater than zero since for any face, the position of the two cell-centers involved should only differ in one coordinate. In my simulations however, it occurs frequently that more than one of the derivates (ddx, ddy, ddz) is greater than zero, even for a structured grid.
The manual gives the following explanation for the FaceGrad-Method:
Return gradient(phi) as a rank-1 FaceVariable using differencing for the normal direction(second-order gradient).
I do not see, how this differs from my understanding pointed out above.
What makes it even more problematic: Whenever "too many" derivates are included, current does not seem to be conserved, even in the simplest structures I model...
Is there a clever way to access the data stored in the face-centered variable? Let's assume I would want to compute the electrical current going through my modeled structure.
As of right now, I save the data stored in the FaceVariable as a tsv-file. This yields a table with (x,y,z)-positions and (ddx, ddy, ddz)-values. I read the file and save the data into arrays to use it in Python. This seems counter-intuitive and really inconvenient. It would be a lot better to be able to access the FaceVariable along certain planes or at certain points.
The documentation does not make it clear, but .faceGrad includes tangential components which account for more than just the neighboring cell center values.
Please see this Jupyter notebook for explicit expressions for the different types of gradients that FiPy can calculate (yes, this stuff should go into the documentation: #560).
The value is accessible with myFaceVar.value and the coordinates with myFaceVar.mesh.faceCenters. FiPy is designed around unstructured meshes and so taking arbitrary slices is not trivial. CellVariable objects support interpolation by calling myCellVar((xs, ys, zs)), but FaceVariable objects do not. See this discussion.
I am trying to optimize a nonlinear function, a sum of terms, that looks something like:
1/(x1+x1)+2/(x1+x2)+1/(x2+x2)+1/(x1+w1)+1/(x2+w1)+1/(x1+w2)+1/(x2+w2)+...
where the variables are x1 and x2 and I have a list of w's.
When testing out various sets of w's for which I know the answer, I find the optimizer I'm using (scipy.optimize.fmin_tnc) fails to give to correct answer for x1 and x2 values that are several orders of magnitudes apart.
It gives the correct answer if the values are more similar in size.
For example, a set of 2 unique and several degenerate values (given small amounts of noise):
w=[5e-13,5e-13,5e-13,5e-13,5e-13,5e-7,5e-7,5e-7]
returns values of ~5e-13 and ~2.4e-7.
I tried adjusting the tolerances, but this doesn't seem to improve the results for this test case.
Setting the 'rescale' flag higher than the default value(1.3) eliminates the problem. I believe this flag makes the minimizer more stable by scaling the variables so they are more similar in size.