Tag Info

New answers tagged

0

If xn has not yet a value, x1=x2=...=xn will set all variables to xn, and then if you do xn=3 all of them will evaluate to 3. If xn already has a value at the point of assignment, you can temporarily unset it using Block. For example c=1 Block[{c}, a=b=c] {a, b, c} (* ==> {1, 1, 1} *) c=2 {a, b, c} (* ==> {2, 2, 2} *) However, in that case ...


6

You can "unify" several variables in the following way x /: HoldPattern[x[k_] = val_] /; 1 <= k <= 10 := (shared = val) x[k_] /; 1 <= k <= 10 && ValueQ[shared] := shared x[2] (* x[2] *) x[3] = 1 (* 1 *) x[9] (* 1 *) x[8] = 0 (* 0 *) x[7] (* 0 *) x[2] = x[3] + 1 (* like shared++ *) (* 1 *) x[11] (* x[11] *) The same for ...


1

You could define a function which creates an array of n constant value var[constant_, n_] := x = ConstantArray[constant, n] For e.g we can make an array of size 5 having 3 as the constant value for all elements as follows var[3, 5] (*{3, 3, 3, 3, 3}*) Since x is equated to this function you can call each array element as follows x[[1]] (*3*) So now ...


7

You can delay-set the variables of interest to a common variable: Table[x[k] := m, {k, 10}]; m = 4; Table[x[k], {k, 10}] m = 3; Table[x[k], {k, 10}] which produces output {4, 4, 4, 4, 4, 4, 4, 4, 4, 4} {3, 3, 3, 3, 3, 3, 3, 3, 3, 3} This way, whenever m is altered, the rest of the x[k] adapt accordingly. I'm not quite sure how to make it so that ...


3

{{x1, y1}, {x2, y2}} = Solve[y^2 == 13 x + 17 && y == 193 x + 29, {x, y}][[All, All, -1]] (* {{(-11181 - Sqrt[2242057])/74498, 1/386 (13 - Sqrt[2242057])}, {(-11181 + Sqrt[2242057])/74498, 1/386 (13 + Sqrt[2242057])}} *) {x1, y2} (* {(-11181- Sqrt[2242057]) / 74498, 1 / 386 (13 + Sqrt[2242057])} *)


1

A certain generalization using an indexed variable: r = FindInstance[Sin[x] == Cos[x] && -10 < x < 10, x, Reals, 15] // Values // Flatten // N {-5.49779, 7.06858, 0.785398, -8.63938, 3.92699, -2.35619} Map[(x[#] = r[[#]]) &, Range @ Length @ r]; {x[1], x[2], x[3], x[4], x[5]} {-5.49779, 7.06858, 0.785398, -8.63938, 3.92699, ...


3

{x1, x2} = Last @@@ Solve[x^2 + 3 x + 2 == 0, x] (* {-2, -1} *) or {x1, x2} = Solve[x^2 + 3 x + 2 == 0, x][[All,1,-1]] (* {-2, -1} *) or sol = Solve[x^2 + 3 x + 2 == 0, x]; sol[[All, 0]] = Last; {x1, x2} = sol (* {-2, -1} *) Note: Since this question has a much simpler structure than the question linked by Artes, these tricks work for the current ...


7

{x1, x2} = x /. Solve[x^2 + 3 x + 2 == 0, x] {-2, -1}


3

I realize from the idiomatic way this program was written that Mathematica is not a language you're very familiar with yet, at least not with the efficient and readable ways Mathematica can address your project. I won't try to suggest every possible improvement but focus on the specific problem of getting the functions you want into the Plot. The following ...



Top 50 recent answers are included