10

I've implemented this recently, basically it counts how many times each specific colour borders another colour to make up a frequency table. To generate an image, a random colour and position are selected and the rest of the image is built up from there. Results aren't very coherent, but they match the colour palette of the original. In order to make sure ...


6

In general, one cannot expect rk4 to maintain quadratic invariants of the system, it simply doesn't do that. Methods that do maintain specific invariants have to be specially devised — this usually goes by the name of geometric integration. Symplectic integrators are the most common kind of these methods, although not for your problem. See, for example, the ...


4

Let's say you have numbered your vertices along the line $y=-\pi/2$ as $u_1,u_2,\ldots,u_n$ (so that you'll have a total of $N=nm$ vertices, with $m$ rows of vertices covering your domain). Then your constraint can be written as $$ u_2=u_1, \\ u_3=u_1, \\ \vdots \\ u_n=u_1. $$ (And the same, obviously, for the vertices at the upper boundary.) This can be ...


4

You want to convert a constrained problem into an unconstrained problem. There are two general techniques for this: If you have a constraint of the form $g(x_1,...,x_N)=0$, then you may be able to write it as $x_N=f(x_1,...,x_{N-1})$ and thereby simply eliminate one of the variables from your problem. You then don't need the constraint any more. (Of course, ...


4

As a matter of fact, if you project each Runge-Kutta stage you will (in general) reduce the method to first order accuracy; this effect is similar to the order reduction that can occur due to boundary condition implementations. To explain it briefly, notice that the first stage of the method is just a forward Euler step, which gives a first-order accurate ...


4

The way to deal with this kind of constraint is to add "slack variables" to your system. In your case, let us say that you want to solve the problem $$ \min_a c^T a \\ \text{subject to}\; \sum_{i=1}^{N-1} |a_i - a_{i+1}| \le 2 $$ then you could introduce slack variables $y_1,\ldots y_{N-1}$ so that $$ y_i \ge |a_i-a_{i+1}| $$ and then solve this ...


4

No, this is not possible. There is a standard way of showing this: The feasible region of your constraints is not convex. For example, $x_{1,1}=1$, $x_{1,2}=0$ is feasible, $x_{1,1}=0$, $x_{1,2}=1$ is feasible, but the midpoint $x_{1,1}=1/2$, $x_{1,2}=1/2$ is not feasible. The feasible set of a system of linear equality and inequality constraints is ...


3

Simulated annealing comes from computations in statistical mechanics. When I think of simulated annealing, I very much think in terms of physics: I want to minimize some potential energy function that depends on the configuration, and whose global minimum corresponds to an optimal solution of your problem. At high temperature, there is enough kinetic energy ...


3

A good first step with any parameter estimation problem is to solve it in simulation to verify that you can get a good solution and that the parameters have an effect on the objective. You can first simulate with m.options.IMODE=7. Once you have an initial solution, you can set your objective function with: for i in range(n): m.Minimize((phi[i]-phi_hat[...


3

As said in the comments, it is not mathematically appropriate to constrain a single point value in the given BVP. The simplest way to achieve something similar to this is to create small circular punctures into your domain and set the prescribed value at the boundary of those. With small enough circles the solution should be very close to the solution ...


3

If you have access to the MATLAB optimization toolbox then this can easily be done using the quadprog() function. You'd start by writing the objective in quadratic form as $ \| Ax - b \|_{2}^{2} = x^{T}(A^{T}A)x-2(A^{T}b)^{T}x+b^{T}b$ then multiply to get $P=A^{T}A$ and $q=-2A^{T}b$. Then your objective is $f(x)=x^{T}Px+q^{T}x+b^{T}b$ and ready to ...


3

An algorithm for this computation is described in Computational Geometry in C, Chapter 7, Section 6. Code is available at that link. Much of the tricky code is concerned with "degenerate" cases. Here is a nice Univ Montreal web page (Eric Plante) that describes a different algorithm: link.          


3

In the general case, this cannot be done in $O(n+k)$ time, as there can be as many as $4\lfloor\frac{n}{2}\rfloor\lfloor\frac{k}{2}\rfloor$, i.e., Θ(nk) intersection points. If the two polygons are convex, however, you can find the intersections in $O(n+k)$ time. See Computational Geometry in C Second Edition, page 253, or Shamos (1978), page 116.


2

$$ \left.\frac{\partial u}{\partial x}\right|_{y=\pi/2} = 0 $$ would be one strategy, though it's a little unusual to impose a tangential derivative boundary condition, and your system will likely be under-constrained if you don't fix the value of $u$ at at least one point. If your $A$ is a decent operator, then your combination of periodic and fixed ...


2

Here is the Lagrange multiplier approach alluded to by Christian Clason. Structurally, I hope you agree that your problem can be put into the form, \begin{align} \text{argmin}_{u}&\quad \frac{1}{2}||Au - f||^2 \\ \text{such that}&\quad Bu=g, \end{align} where $A$ is the PDE operator, $f$ is the PDE right hand side, $B$ is the "observation ...


2

I cannot comment, so as an answer may I recommend looking at something like: http://lorenabarba.com/blog/cfd-python-12-steps-to-navier-stokes/ form start to finish? This takes the reader from basic discretisation of convective and diffusive terms in 1d and 2d, over a way of dealing with incompressibility via an equation for pressure derived from the ...


2

You should have a look at Satisfiability Modulo Theories or SMT for short. A huge number of problems can be thought of as instances of SMT for a particular theory. For example, correctly designing certain types of integrated circuits can be phrased as an SMT problem. The problem you're describing fits under the theory of quantifier-free linear integer ...


2

Like what @Kirill says, write a script that defines $f(u)$ to be a function that approximates the integral. Thus all that you need to is solve $f(u)=0$ which can use standard root finding tools like Newton's method. You're best choice for this is probably Julia. Using the built in quadgk function for Gauss-Kronrad quadrature, you can define the f function ...


2

You will have to transcribe the control problem in some manner so that you can feed it to a non-linear programming solver. You may want to look at Matthew Kelly's paper for a good introduction to transcription methods for trajectory optimization problems. It basically summarizes transcription methods as follows: There are various classifications of ...


2

Let $z_i = \log p_i$ (i.e. $p_i = e^{z_i}$) and everything changes to linear constraints except the constraint $\sum e^{z_i} = 1$ which you can relax to $\sum e^{z_i} \leq 1$ since it will be tight at optimality (assuming $m_i$ positive).


2

The constraints are not convex. Consider the example below in which x1 and x2 are each vectors of 3 elements which satisfy the inequality in question, as shown. 0.5(x1 + x2) does not satisfy the inequality, thereby proving it is not convex. >> x1 = [0.868417827606570 0.121582145814843 0.010000025679806]; >> x2 = [0.017508300926335 0....


2

One approach to this problem is to reparameterize your problem in terms of $x_{1}$ and $z_{i}=x_{i}-x_{i-1}$, $i=2, \ldots, n-1$. You can then rewrite your objective function in terms of the the $z_{i}$ variables by substituting $x_{i}=x_{1}+z_{1}+\ldots + z_{i}$, $i=2, 3, \ldots, n$. Your separation constraints become $k \leq z_{i}$


2

If the exact solution indeed stays within [0,1], the solver may still resolve too coarsely the dynamics and "jump" over the physical bounds. One way to solve this is to use lower absolute and relative tolerances in your solve_ivp call: solve_ivp(..., atol=1e-9, rtol=1e-9, ... for example. Finally, you can hack your way around by simply projecting ...


2

With your constraints of $a = 1$ and $c = -\frac{b}{d} - 1$, your $f$ looks like this: $$\hat{\mathbf{y}} = f(\mathbf{x},b,d) = \frac{bd}{d\exp{(\mathbf{x})}-b - d} + d$$ You're trying to solve a nonlinear least square problem basically: $$b,d = \arg \min \sum_{i=1}^{N} (f(x_{i})-y_{i})^{2}$$ It's a job for scipy.optimize.curve_fit: import numpy as np from ...


1

EDIT: Intermediate solution from cvxpy import * import numpy as np np.random.seed(1) n = 10 Sigma = np.random.randn(n, n) Sigma = Sigma.T.dot(Sigma) w = Variable(n) mu = np.abs(np.random.randn(n, 1)) ret = mu.T*w risk = quad_form(w, Sigma) orig_weight = [0.15,0.2,0.2,0.2,0.2,0.05,0.0,0.0,0.0,0.0] min_weight = [0.35,0.0,0.0,0.0,0.0,0,0.0,0,0.0,0.0] ...


1

You should put "mirrors" to bracket your domain. For example, in a one-dimensional case: If I know that my unknown $x$ is bounded between $0$ and $a$. At each iteration of my method, i will check if $0 < x < a$. If not, while $x < 0$ or $x > a$, if $x < 0$ I will change $x$ to $-x$, else if $x > a$ I will change $x$ to $a-(x-a)$. Another ...


1

I found the error, I just need to constrain the implied identity matrix on the right-hand-side too: r = rand(4, 4); r = r' + r; p=[1 0 0 -1]'; M = eye(4); w = 1000; [V,D ] = eig(r + w*p*p', M + w*p*p'); V


1

Ironically, there is an iterative method called "SIMPLE" (semi-implicit method for pressure-linked equations) designed to resolve the steady state navier-stokes equations based on a predictor-corrector scheme. It works by solving a linearized form of the momentum equation (predictor step) which produces a velocity field which generally does not satisfy ...


1

With the continuity eqn only, you are missing all the mechanical balance: viscous and/or inertial effects will decide of the streamlines of such a flow. If your major aim is to keep it as simple as possible, I would go for a lubrication approximation. If you consider that your fluid is seeping between two flat plates with a very narrow gap, in the end the ...


1

Blog post on an experimentation using Markov to recreate art: https://magenta.as/using-machine-learning-to-make-art-84df7d3bb911 Code is on github made by @william-index: https://github.com/william-index/markov-fun


Only top voted, non community-wiki answers of a minimum length are eligible