Tag Info

Hot answers tagged

8

Your corrected code: myVect = {x, y}; offDiag = {{0, 1}, {1, 0}}; Maximize[1/2*myVect.offDiag.myVect, myVect.{2, 2} - 1 == 0, myVect] {1/16, {x -> 1/4, y -> 1/4}}


8

w = RandomReal[{0, 1}, 10]; b = RandomReal[{0.5, 1}, 10]; a = RandomReal[{0, .5}]; rr = Array[r, 10]; Minimize[{w.Log@rr, Thread[a <= rr <= b]}, rr]


8

Brute force approach. Update, now properly using formula for area overlap. (Note this does not properly handle cases of multiple overlap) ( points = RandomReal[{0, 1}, {20, 2}]; R = .15; segarea[R_, L_ ] /; L < 2 R := R^2 ( # - Sin@# ) &@( 2 ArcSin[Sqrt[1 - 1/4 (L/R)^2]] ); segarea[R_, L_] = 0; x = {#, Total[(segarea[R, Norm[Subtract @@ ...


6

Brute force The data points = RandomReal[{0, 1}, {20, 2}]; all the possible 10 Subsets (notice the differences with Tuples) allsubsets = Subsets[points, {10}]; you can measure the overlapping using RegionIntersection and RegionMeasure RegionMeasure[RegionIntersection @@ Thread[Disk[pair, r]]] where r is the radius and pair the a list with the two ...


6

If you want to use Plot to find the extrema of a black-box function, here is an implementation. I decided to look for maxima here, but you can turn this into minima by flipping the sign of the function. It works by first applying MaxDetect to the plot points in a given interval, and then constructing brackets for FindMaximum from the neighboring points. I'll ...


6

As @Guess who it is. states in the comments, an overdetermined linear problem can be solved using Mathematica's LeastSquares[] functionality. To input your above system of equations: a = {{1, -2, 2, 2, 0, -2}, {1, -2, 2, -2, 0, 2}, {1, -2, -2, -2, -4, -2}, {1, 2, 2, -2, -4, -2}, {1, 2, -2, -2, 0, 2}, {1, 2, -2, 2, 0, -2}, {1, -2, -2, 2, 4, ...


5

another test: f = x^3 + x*z^2 - 3*x^2 + y^2 + 2*z^2; cpts = Solve[Grad[f, {x, y, z}] == 0, {x, y, z}, Reals] {{x -> 0, y -> 0, z -> 0}, {x -> 2, y -> 0, z -> 0}} hesse = D[f, {{x, y, z}, 2}] /. cpts {{{-6, 0, 0}, {0, 2, 0}, {0, 0, 4}}, {{6, 0, 0}, {0, 2, 0}, {0, 0, 8}}} {ev1[l1,l2,l3], ev2[l1,l2,l3]} = Eigenvalues /@ hesse {{-6, 4, ...


5

Your problem is partly that NIntegrate calls a function that at the given point in time is not full numerical (the first expression in fn). A way around that is to define the functions in such a way that hey will only evaluate for purely numerical values: Clear[m1, m2, fn, gn]; m1[x_?NumericQ] := (2 (x - 1))/x; m2[x_?NumericQ, y_?NumericQ] := (x (2 - x ...


4

Here's an implementation of the brute-force approach I described above, with the graph theory bit omitted; it doesn't look like Mathematica has a canned algorithm to find the vertex subset I described. To save time, the code pre-computes the overlap areas in adjmat, and then looks for the sub-matrix of adjmat with the lowest sum of its entries. points = ...


4

As mentioned in the comments and as the error message says, FindMaximum only accepts integer domain constraints for linear optimization problems, while of course nx * wx is a non-linear term. For ILP problems, FindMaximum uses a specialized solver from the COIN-OR branch-and-cut (CBC) library. NMaximize is using a different approach.


3

There are three problems with your posted code. Your objective function is a 1x1 matrix rather than a scalar. Your constraint equation is malformed. One side is a 1x1 matrix, the other is a scalar. They should both be scalars. The variables should be in a flat list. @Willinski already gave a more natural way of expressing vectors in Mathematica, so that ...


3

Perhaps this? Clear[f, g, maxg]; f[y_] := 1 - y g[x_, y_] := x - y maxg[y_?NumericQ] := NMaxValue[{g[x, y], 0 <= x <= 1}, x] NMaximize[{f[y], y >= 0 && maxg[y] <= 1/2}, y] (* {0.500001, {y -> 0.499999}} *) See this answer for more information about the use of NumericQ: What are the most common pitfalls awaiting new users? There ...


3

As the Norm is positive, you don't really need the constraint to determine one minimum for p and t: nm = NMinimize[{ (Norm[{xm[t][p], ym[t][p]} - {xsc[t][p], ysc[t][p]}] - 10^7)^2 /. Soln, TOF - 10 (86400) < t < TOF + 10 (86400) && 0.8 < p < 1}, {t, p}, Method -> "NelderMead"] (* {0.000155262, ...


3

A stupid workaround is simply to replace b with -b: q[a_?NumericQ, b_?NumericQ] := NIntegrate[Exp[a*(x^2 - 1) - b*(x^4 - 3.1)], {x, -Infinity, Infinity}]; yy = FindMinimum[{q[a, b], b > 0}, {{a, -2}, {b, .004}}] {4.13273, {a -> -0.5, b -> 8.27074*10^-8}} without any warnings. But it's a pity that FindMinimum behaves so inflexibly. Of ...


3

Integrate[Exp[a*(x^2 - 1) + b*(x^4 - 31/10)], {x, -Infinity, Infinity}] ConditionalExpression[ (Sqrt[-a]*E^(-a - a^2/(8*b) - (31*b)/10)* BesselK[1/4, -(a^2/(8*b))])/(2*Sqrt[-b]), Re[b] < 0 && Re[a] < 0] qExact[a_, b_] = Assuming[{a < 0, b < 0}, Integrate[Exp[a*(x^2 - 1) + b*(x^4 - 31/10)], {x, -Infinity, ...


3

Your data: points = RandomReal[{0, 1}, {20, 2}] Find minimum distance: r = Min@Select[EuclideanDistance @@@ Tuples[points, 2], Positive] Graph Graphics@Table[ Circle[p, r/2] , {p, points} ]


3

Here's an adaptation of the NDSolve method used by Daniel Lichtblau that uses numeric (finite difference) derivatives. Just what to return and in what form is a choice. The following returns a list of FindMinimum style solutions {{m1, {x -> x1}}, {m2, {x -> x2}},...}. The solutions may be polished further with FindMinimum. findAllMinima[f_, {x_, ...


3

Setting WorkingPrecision -> 5 in the gn integral gives you a reasonable convergence time. At the expense of some more computation time you can check that methods DifferentialEvolution and SimulatedAnnealing both return the sme result given here up to four decimal places. m1[x_] := 2 (x - 1)/x; m2[x_, y_] := x (2 - x y)/(2 (x - 1) y); fn[x_?NumericQ, ...


2

Notice the details of the error: NMinimize::nnum: The function value [...] is not a number at {k,x0} = {1.91862,1.66351}. >> You need to be sure that objfn doesn't evaluate until is given a Real value as argument. For that, change objfn[k_, x0_] to objfn[k_Real, x0_Real]. You can also avoid the substitution Rule and evaluate the functions directly. ...


2

How about NMaximize? NMaximize[{ Min[{funA[1.3, 0.7, x, y], funB[1.3, 0.7, x, y]}], 1 <= x <= 2 && 0 <= y <= 1}, {{x, 1, 2}, {y, 0, 1}}] {0.254965, {x -> 1.55733, y -> 0.775248}} To grab the max, x, and y values to later export, you can try: Clear[x, y]; a = Range[1.3, 2, 0.1]; b = Range[0.7, 1.4, 0.1]; With[{m = ...


2

The random functionality within Mathematica is all of a pattern: RandomFunction[range, outputStructure] where range depends on what RandomFunction you are using, e.g. for RandomInteger and RandomReal it is {min, max} and they both default to {0,1} if no min/max are supplied. The outputStructure tells the RandomFunction how many random numbers you want and ...


2

If you want to teach the students to interpret level sets, then it is possible visualize the behavior of a function by manipulating the level. There are various ways to set it up, to cue the students' recognition of the type of extremum, etc. Here's one, whipped up rather quickly. Clear[f, x, y, z]; f = x^3 + x z^2 - 3 x^2 + y^2 + 2 z^2 cpts = {x, y, z} /. ...


2

You can visualise a function of 3 paramters by putting one of them as argument of Manipulate: Manipulate[Plot3D[f /. z -> z1, {x, -3, 3}, {y, -3, 3}], {z1, -3, 3}]


2

As noted by the OP, adding the constraint, Norm[{xm[t][p], ym[t][p]} - {xsc[t][p], ysc[t][p]}] > 10000000 /. Soln triggers error messages, probably because p typically must be given a numerical value before t is given one in expressions such as xm[t][p]. See the documentation for ParametricFunction. This can be accomplished by defining ...


2

The use of NumericQ as mentioned by MarcoB and Guess who it is. in the comments seems to be important. Also, estimating the integral using a Total[Table[]] as in the code I posted in the second revision of the question makes this method computational feasible enough to solve my problem.


2

I can't be sure what FindMinimum does. I can only find the various *Monitor options to see what major steps it takes. Then I might only infer, rightly or wrongly, why it does any of these things. I will say that I think it is behaving "correctly" in the sense of "as intended." When all the conditions are "right," the step taken by FindMinimum under ...


1

You have quite a small data set, so a really inefficient brute force search will still run pretty fast (<1 sec on my computer). I stress that this is STUPID way to do it, and with list manipulation you can surely make it MUCH more efficient. But as I said - it works. First, transform the data so that you could retrieve the data by calling f[x1,x2,x3], ...


1

The calculation, as presented in the question, is ill-posed in that the maximum value is infinity for w1 or w2 equal to zero and the corresponding a not equal to zero. Bounding the ws away from zero and making a few other changes, as shown here, produces an answer. MaxValue[{-a2*Log2[w2] - a1*Log2[w1], 0.01 <= w1 <= q, 0.01 <= w2 <= q, 0 ...


1

Here is my quick answer. Import the data from the files: files = FileNames["/sample_data/1_5ormore_methy_00*"]; data = Import[#, "Table"] & /@ files; Define a function to compute your values: f[s1_, s2_, vals_] := Times @@ (Beta[First@# + s1, Last@# + s2] & /@vals)/Beta[s1, s2]^Length@vals Apply the function to the data for each of the ...


1

Of course this is possible. But I suggest that you break the problem down in smaller steps. Let's start with the problem of reading many files. If you have them in one directory, it can be as simple as files = FileNames["a*"]; data = Import[#,"Table"] &/@ files; This fills the list data with sub-lists, that contain the data n each file. Now you ...



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