Reputation: 3876
When I want to solve a set of linear equations for two functions, e.g.
solutions := solve({f(x)=x,g(x)=x},{f(x),g(x)});
what exactly can I do to work with the solutions as functions themselves in maple?
The only thing which I was able to do was
f_solution := x2 -> subs(x=x2, rhs(solutions[1]))
But that is ugly in many aspects. First, this trivial substitution x->x2
seems necessary, without it will not work. Second, the construct rhs(solutions[1])
is very bad, as it is not possible to control the order of the solutions. Consequently everytime I modify my equations, I would have to check manually, if the index [1]
is still correct.
Is there a clean and standard way to extract the functions from the set?
Upvotes: 0
Views: 2093
Reputation: 7246
solutions := solve({2*f(x)=sin(x),g(x)/3=cos(x)},{f(x),g(x)});
/ 1 \
{ f(x) = - sin(x), g(x) = 3 cos(x) }
\ 2 /
and now, with f_solution
as an expression,
f_solution := eval(f(x), solutions);
1
- sin(x)
2
or with f_solution
as a procedure,
f_solution := unapply( eval(f(x), solutions), x);
1
x -> - sin(x)
2
Upvotes: 0
Reputation: 2017
Have a look at assign
. It can fix the solutions you obtain in your calculation
> restart:
> solutions := solve({f(x)=x,g(x)=x},{f(x),g(x)});
solutions := {f(x) = x, g(x) = x}
> assign(%);
> f(x);
x
You could also just try subs
like this
> restart:
> solutions := solve({f(x)=x,g(x)=x},{f(x),g(x)});
solutions := {f(x) = x , g(x) = x}
> subs(solutions,f(x));
x
Upvotes: 0