Re: Using results of "Solve"
- To: mathgroup at smc.vnet.net
- Subject: [mg25764] Re: [mg25693] Using results of "Solve"
- From: BobHanlon at aol.com
- Date: Sat, 21 Oct 2000 18:33:00 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
In a message dated 10/19/2000 5:35:06 AM, helgek at studserv.stud.uni-hannover.de writes: >I have a series of matrices which depend on several parameters. I can >create those matrices fine and display them. They are called > >S[k_,m_,b_,n_] > >Now there is an unkown called "q" in the matrix which is evaluated by >setting the determinant of the matrix zero and solving the resulting >equation for q: > >Solve[Det[S[2, m, b, 2]] == 0, q] > >Now I want to use this function. The result is given in a form like: >{{ q -> rhs1 },{ q -> rhs2}} and so on. I would like now to plot q. > >A colleque gave me the following "hack": >h =.; >h[m_, b_] := Solve[Det[S[2, m, b, 2]] == 0, q]; >Plot3D[q /. h[m, b] [[2]], {m, 0, 3}, {b, 0, 3}] > >Is there a more straightforward way ? Especially I would like to create >several functions this way and their sum is the resulting function I >am interested in (actually it's a series). > S[k_, m_, b_, n_] := {{k*q + m + b + n , k + m + b - n}, {k*q + m - b + n, k*q - m + b + n}}; Your approach is fine; however, to the extent possible and practicable, you should define functions so that they are evaluated only once. For example, Clear[detS]; detS[k_, m_, b_, n_] := Det[S[k, m, b, n]]; ?detS Global`detS detS[k_, m_, b_, n_] := Det[S[k, m, b, n]] Note that the deterimant of S would be evaluated for each call. Compare this with Clear[detS]; detS[k_, m_, b_, n_] := Evaluate[Det[S[k, m, b, n]]]; ?detS Global`detS detS[k_, m_, b_, n_] := 2*b^2 + b*k - k*m - 2*m^2 - k*n + 2*n^2 + b*k*q - k^2*q - k*m*q + 3*k*n*q + k^2*q^2 For functions which are not too complex, it may be worthwhile to Simplify the result as part of the definition. Clear[detS]; detS[k_, m_, b_, n_] := Evaluate[FullSimplify[Evaluate[Det[S[k, m, b, n]]]]]; ?detS Global`detS detS[k_, m_, b_, n_] := 2*b^2 + b*k*(1 + q) + (-2*m + 2*n + k*(-1 + q))*(m + n + k*q) Similarly, Clear[h]; h[m_, b_] := Solve[detS[2, m, b, 2] == 0, q]; ?h Global`h h[m_, b_] := Solve[detS[2, m, b, 2] == 0, q]; Clear[h]; h[m_, b_] := Evaluate[Solve[detS[2, m, b, 2] == 0, q]]; ?h Global`h h[m_, b_] := {{q -> (-4 - b + m - Sqrt[-7*b^2 - 2*b*m + 9*m^2])/4}, {q -> (-4 - b + m + Sqrt[-7*b^2 - 2*b*m + 9*m^2])/4}} Off[Plot3D::gval]; (p2 = Plot3D[q /. h[m, b] [[2]], {m, 0, 3}, {b, 0, 3}]) // Timing (p1 = Plot3D[q /. h[m, b] [[1]], {m, 0, 3}, {b, 0, 3}, PlotPoints -> 25]) // Timing Off[Graphics3D::nlist3]; Show[{p1, p2}]; Bob Hanlon