1
$\begingroup$

I have a question concerning the FindInstance function. If I write

FindInstance[c == 3, {c, l}]

I obtain

{{c -> 3, l -> 33/10 + (8 I)/5}}

Is there any way to obtain l=0? In general, when one wants to solve a system of equations and one of the variables is free (the system is undeterminate compatible), is it possible to assign the value 0 to it?

EDIT: I put a clearer example, since this one is causing confusion. Imagine I ask this problem: find a polynomial P of degree 2 such that P(x+1)-P(x)=1.

P = a0 + a1*x + a2*x^2;

Q = (P /. x -> x + 1) - P - 1; (* deg Q = 1 *)

FindInstance[CoefficientList[Q, x] == {0, 0}, {a0, a1, a2}]

I obtain

{{a0 -> 33/10 + (8 I)/5, a1 -> 1, a2 -> 0}}

but clearly x also satifies (x+1)-x=1. I want to obtain the most simple solution (that is, the one with a0=0, but notice that I don't know previously that a0 will disappear from the system, so I cannot assign to a0 the value 0 a priori).

$\endgroup$
  • $\begingroup$ Maybe my idea is wrong, but the way you write the code, the variables are effectively uninitialized, why should it give a zero, what would the idea behind that argument be --- assign zero to l? $\endgroup$ – leosenko Apr 25 '16 at 14:10
  • $\begingroup$ I wrote here an example, but my original problem is slightly different. I have a code that returns a vector (u_0,...u_n). The vector is computed by means of a linear system A(u_0,...u_n)=0, which may not have a unique solution. There are cases that, when I apply A, a u_i disappears from all the equations (in my example, it's as if u_i were l). I would like then to put the value 0 to u_i in such a case, and not a strange complex value 33/10 + (8 I)/5. $\endgroup$ – user39756 Apr 25 '16 at 14:23
  • $\begingroup$ The example I posted is just that, an example. I am asking what one can do in general when there are free variables in the system but you don't know which are. $\endgroup$ – user39756 Apr 25 '16 at 14:28
  • $\begingroup$ If you know that you want to to be zero, just equate it to zero. FindInstance[{c == 3, a == 0}, {c, a}] evaluates to {{c -> 3, a -> 0}} $\endgroup$ – Bob Hanlon Apr 25 '16 at 14:43
  • $\begingroup$ @BobHanlon Thank you for answering. But my doubt is that, in general, when I have an undeterminate compatible system, I don't know which will be the free variables, so I cannot assign a priori the value 0 to them. $\endgroup$ – user39756 Apr 25 '16 at 14:50
3
$\begingroup$

One possibility would be to restate the problem as a minimization problem, for instance:

Minimize[(c - 3)^2 + l^2, {c, l}]

returns {0, {c -> 3, l -> 0}}.

Another possibility is to use Solve directly:

Solve[CoefficientList[Q, x] == 0, {a0, a1, a2}]
{{a1 -> 1, a2 -> 0}}

This also returns a warning, that Equations may not give solutions for all "solve" variables but this is, of course, what you are looking for.

$\endgroup$
2
$\begingroup$
p[x_] = a0 + a1*x + a2*x^2;

eqn = p[x + 1] - p[x] == 1;

If you want only real values, restrict FindInstance to the real domain.

FindInstance[ForAll[x, eqn], {a0, a1, a2}, Reals, 10]

(*  {{a0 -> -(433/10), a1 -> 1, a2 -> 0}, {a0 -> -(207/5), a1 -> 1, 
  a2 -> 0}, {a0 -> -37, a1 -> 1, a2 -> 0}, {a0 -> -26, a1 -> 1, 
  a2 -> 0}, {a0 -> 109/10, a1 -> 1, a2 -> 0}, {a0 -> 84/5, a1 -> 1, 
  a2 -> 0}, {a0 -> 92/5, a1 -> 1, a2 -> 0}, {a0 -> 139/5, a1 -> 1, 
  a2 -> 0}, {a0 -> 43, a1 -> 1, a2 -> 0}, {a0 -> 97/2, a1 -> 1, 
  a2 -> 0}}  *)

Or, more generally,

soln = Reduce[ForAll[x, eqn], {a0, a1, a2}] // ToRules

(*  {a1 -> 1, a2 -> 0}  *)

eqn /. soln

(*  True  *)

To set any arbitrary variables (a0 in this case) to zero

soln2 = {soln,
    Thread[
     Complement[{a0, a1, a2}, soln[[All, 1]]] -> 0]} // 
  Flatten // Sort

(*  {a0 -> 0, a1 -> 1, a2 -> 0}  *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.