# How could I define a function as the solution of equations in Mathematica?

I have posed a specific question yesterday but it may be too trivial to answer.

I think the question could be asked in a more general way so it is easier to answer. Then I could solve the original problem by myself.

Usually, we define a function as

F[x_] := x + 10


where the right-hand side is an expression.

Now, I would like define a function where the right-hand side is the solution of a set of equations. Like

optV[V_, r_, λ_, μ_, η_, σ_] :=
V /.
FindRoot[{
A V^θ[r, λ, μ, η, σ] h[V, c, r, λ, μ, η, σ] == V - c,
D[A*V^θ*h[V, c, r, λ, μ, η, σ], V] == 1
},
{A, 0}, {V, 1}];

a[V_, r_, λ_, μ_, η_, σ_] :=
A /.
FindRoot[{
A V^θ[r, λ, μ, η, σ] h[c, r, λ, μ, η, σ] == V - c,
D[A V^θ[r, λ, μ, η, σ]  h[V, c, r, λ, μ, η, σ], V] == 1
},
{A, 0}, {V,1}]


This may not be correct code, but I think you can understand my question from it. optV and a are both parameters for another final function which would be manipulated. So you could regard optV and a as intermediate parameters. So the solution of the set of equations, correspondingly optV and a, varies along with c, r, λ, μ, η, σ, which are parameters input for manipulating plot.

-
Since NSolve[] will return a list of lists, you'll likely want to put Part[] in there somewhere... –  Guess who it is. Jun 17 '13 at 17:45
–  belisarius Jun 17 '13 at 17:49
@bel, FWIW: it's one of the songs I listen to after a hard day... precisely that medley. –  Guess who it is. Jun 17 '13 at 17:51
Just yesterday I posted an answer where this is illustrated. There are countless other answers on this site that do similar things. Maybe just searching for NDSolve will turn up other good examples. –  Jens Jun 17 '13 at 17:56
you can easily do what you asked, eg.f[a_] := x /. NSolve[{x + y == a, x^2 + y^2 == 1}, {x, y}]. I cant understand what your intent is with those examples though. The arg x on input does what? –  george2079 Jun 17 '13 at 18:37

If I understand you right, here is the answer, but it is in a form of a more transparent example.

Let this

Clear[a,b];
eq=x^2 - b*x - 1 == 0


be an equation depending upon a parameter b with b>0. Let us define a=a[b] which is its smaller solution of the equation eq:

a[b_] := Solve[eq, x][[1, 1, 2]]


It seems that this (or something alike) is what you need. You can check that a is indeed a function of b by, say, plotting a[b]. Evaluate this:

Plot[a[b], {b, 0, 3}]


With some care one can do the same with the FindRoot statement:

Clear[a,b];
a[b_] := FindRoot[eq, {x, 0}][[1, 2]]
Plot[a[b], {b, 0, 3}]

-
While this works, I would be careful about explicitely making eq a function of b: 'eq[b_] = x^2 - b*x - 1 == 0' and I would force the definition of a[b] to take only numeric values: a[b_?NumberQ]. In this example it is fine, but you have to be careful of the order of evaluation when you are mixing both symbolic and numerical values. –  Jonathan Shock Jun 18 '13 at 8:41
Isn't it inefficient to call Solve every time the function a is evaluated? If you used Set instead of SetDelayed, the equation would only be solved once. –  nikie Jun 18 '13 at 9:07
@nikie yes in this simple example. There are however situations where you need to Solve (or especially NSolve ) for the specific value of the parameters. –  george2079 Jun 18 '13 at 19:15