Maple: Accessing the solutions of solve when solving for multiple functions - function

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?

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

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

Related

Solving a system of equations in Maple

I have a system of n equations and n unknown variables under symbol sum. I want to create a loop to solve this system of equations when inputting n.
y := s -> 1/6cos(3s);
A := (k, s) -> piecewise(k <> 0, 1/2exp(ksI)/abs(k), k = 0, ln(2)exp(s0I) - sin(s));
s := (j, n) -> 2jPi/(2*n + 1);
n := 1;
for j from -n to n do
eqn[j] := sum((A(k, s(j, n))) . (a[k]), k = -n .. n) = y(s(j, n));
end do;
eqs := seq(eqn[i], i = -n .. n);
solve({eqs}, {a[i]});
enter image description here
Please help me out!
I added some missing multiplication symbols to your plaintext code, to reproduce it.
restart;
y:=s->1/6*cos(3*s):
A:=(k,s)->piecewise(k<>0,1/2*exp(k*s*I)/abs(k),
k=0,ln(2)*exp(s*I*0)-sin(s)):
s:=(j,n)->2*j*Pi/(2*n+1):
n:=1:
for j from -n to n do
eqn[j]:=add((A(k,s(j,n)))*a[k],k=-n..n)=y(s(j,n));
end do:
eqs:=seq(eqn[i],i=-n..n);
(-1/4+1/4*I*3^(1/2))*a[-1]+(ln(2)+1/2*3^(1/2))*a[0]+(-1/4-1/4*I*3^(1/2))*a[1] = 1/6,
1/2*a[-1]+ln(2)*a[0]+1/2*a[1] = 1/6,
(-1/4-1/4*I*3^(1/2))*a[-1]+(ln(2)-1/2*3^(1/2))*a[0]+(-1/4+1/4*I*3^(1/2))*a[1] = 1/6
You can pass the set of names (for which to solve) as an optional argument. But that has to contain the actual names, and not just the abstract placeholder a[i] as you tried it.
solve({eqs},{seq(a[i],i=-n..n)});
{a[-1] = 1/6*I/ln(2),
a[0] = 1/6/ln(2),
a[1] = -1/6*I/ln(2)}
You could also omit the indeterminate names here, as optional argument to solve (since you wish to solve for all of them, and no other names are present).
solve({eqs});
{a[-1] = 1/6*I/ln(2),
a[0] = 1/6/ln(2),
a[1] = -1/6*I/ln(2)}
For n:=3 and n:=4 it helps solve to get a result quicker here if exp calls are turned into trig calls. Ie,
solve(evalc({eqs}),{seq(a[i],i=-n..n)});
If n is higher than 4 you might have to wait long for an exact (symbolic) result. But even at n:=10 a floating-point result was fast for me. That is, calling fsolve instead of solve.
fsolve({eqs},{seq(a[i],i=-n..n)});
But even that might be unnecessary, as it seems that the following is a solution for n>=3. Here all the variables are set to zero, except a[-3] and a[3] which are both set to 1/2.
cand:={seq(a[i]=0,i=-n..-4),seq(a[i]=0,i=-2..2),
seq(a[i]=0,i=4..n),seq(a[i]=1/2,i=[-3,3])}:
simplify(eval((rhs-lhs)~({eqs}),cand));
{0}

MapleSoft: solutions to the inverse of a function puzzle

I have to find the inverse of a function which looks like:
T := ->x (x)^0.5/(x^0.5+(1-x)^0.5)^2.
As we can see from the polynomial, we have 4 solutions when solving y= f(x). In maple,I soled for the inverse of T(x)
V := x-> solve(t=T(x),x,useassumptions=true) assuming 0<=t<=1.
and I can evaluate V, i.e maple can do V(0)=0 V(1)=1 etc.
However, as discussed, there are four solutions to the inverse function, the output of V is an expressions sequence, which looks like (solution1, solution2, solution3, solution4).
In later part of the task, I have to find the derivative of V(x)and integrate it. When I apply diff(V(x),x), maple gives me an error, saying V(x) is not valid.As V(x) is an expression sequence. I tried to use the function D(V), but still no luck.
My questions is how would I be able to handle this V(x) as an expression sequence to finish the rest of the task. Is V(x) a piecewise function? If that's the case, how would I be able to convert this expression sequence to a piecewise function.
Regards,
restart:
T := proc (x) options operator, arrow; sqrt(x)/(sqrt(x)+sqrt(1-x))^2 end proc:
V := proc (x) options operator, arrow; solve(x = T(y), y) end proc:
sol := [allvalues(V(x))]:# Extract 4 solution, with command op(1, sol)->Only first solution is correct.
plot([x, T(x), op(1, sol)], x = 0 .. 2, legend = [typeset("Curve: ", "x"),
typeset("Curve: ", "T(x)"), typeset("Curve: ", "V(x)")]);
VV := proc (x) options operator, arrow; evalf(op(1, sol)) end proc;
eval(VV(x), x = 1/2); #Inverse function at point x=1/2
eval(diff(VV(x), x), x = 1/2);# Derivative of inverse function at point x=1/2
int(VV(x), x = 1/10 .. 1/2, numeric);# Integral of inverse function at range (1/10..1/2)
Mathematica 11.3 solution:

GNUPLOT : Plotting a user defined equations which operates indifferent intervals

Hi there I need some help!
I am new to Gnuplot and have difficulties with the scripts.
Actually my equation is a lot more complicated. It is a parametric equation consisting of 7 parts each defined into a specific interval, with a bunch of parameters.
I just need a lead. So let me simplify the problem.
Suppose I have a function defined as follows f(x)= a*x+cos(x) : for 0 <= x <= 3;
and f(x)= b*1/cos(x) : for 3 < x <=10
my question is how do I instruct GNUPLOT:
1-) to consider "a" and "b" as parameters
2-) to plot "my user-defined" equation into the intervals of definition of f(x)
So far I have used the "set parametric" command but the problem is always at the end at the "PLOT f(x)" command for which I really don't know how to deal with the intervals.
I am using Windows 7 with the latest gnuplot.
Please help
You can define your f(x) as a (conditional) piece-wise function:
f(x) = 0 <= x && x <= 3 ? a*x+cos(x) : 3 < x && x <= 10 ? b/cos(x) : 1/0
The 1/0 above makes sure the function is not defined outside of the given intervals. The parameters a and b are already implicitly treated as parameters by gnuplot. When you change their values, f(x) is updated automatically. Example:
set xrange [-2:12]
a = 1.; b = 1.
plot f(x)
If you want more flexibility, you can take a and b as variables and do the following:
f(x,a,b) = 0 <= x && x <= 3 ? a*x+cos(x) : 3 < x && x <= 10 ? b/cos(x) : 1/0
set xrange [-2:12]
plot f(x,1,1), f(x,2,3)

Defining Your Own Functions with a few procedures in Wolfram Language

I have created a function with one procedure....
Func1[n_] := Table[a[i], {i, n}]
which returns
Func1[5]
{a[1], a[2], a[3], a[4], a[5]}
I also have created a function with a few parameters or with a few arguments, few variables!
Func1[x_, y_, z_] := (x + y)*z - 1
which returns
Func1[5, 2, 3]
20
But what about if I want to create a function with a several procedure which returns whatever I want?
I already know that when one procedure is done I have to type " ; " at the end of this procedure!
Like in for loops we do....
For[k = 2, k < 3, k++,
S := Table[a[i], {i, n}];
B := Dimensions[S][[1]]];
]
So I need to create a function with a several procedure!
How to do it?
Please help me!
A couple of examples here. Remember to use lower-case initial letters to avoid conflicting with built-in functions which all start with a capital letter.
s[n_] := Table[a[i], {i, n}]
b[s_] := Dimensions[s][[1]]
For[k = 2, k < 3, k++,
x = s[k];
Print[b[x]]]
2
For[k = 2, k < 3, k++,
Print[b[s[k]]]]
2
Use parentheses for grouping.
For example
set$s$b[n_Integer] := ($s = Table[a[i], {i, n}];
$b = Dimensions[$s][[1]];)
Now, after executing, e.g.,
set$s$b[5]
one will get
$s
{a[1], a[2], a[3], a[4], a[5]}
$b
5
However, making use of modularity might be a better design choice in situations where the execution of several procedures is needed.

Any way to "visualize" a thunk/function? Or how to view a function for a general argument

I'm not totally sure how to ask this, but is there a way to show the structure of a thunk?
For example
f x = x + 2
g x = 3 x
compo x = f (g x)
ans = compo 5
-- result: (3 * 5) + 2 = 17
Is there any way I could "see" the thunk for ans? As in, I could see the process of the beta reduction for compo or like the "general" form.
I would like to see, for example:
compo n
--> (3 * n) + 2
As in, if I had a function compo x, I would like to view that it is decomposed to (3*n)+2.
For example, in Mathematica:
f[x_] := x+2;
g[x_] := 3*x;
compo[x_] := f[g[x]];
compo[n]
(%
--> (3 * n) + 2
%)
There is the ghc-vis package on hackage which show a visualization of your heap and of unevaluated thunks.
See the package on hackage or the Homepage (which contains rather impressive examples).
If you just want to see the sequence of reductions, you could try using the GHCi interactive debugger. (It's in the GHC manual somewhere.) It's not nearly as easy as your typical IDE debugger, but it more or less works...
In general (we are talking about Haskell code) I think it has no sense, the final thunk stream will be different for different input data and, on the other hand, functions are expanded partially (functions are not only simple expressions).
Anyway, you can simulate it (but ugly)
Prelude> :set -XQuasiQuotes
Prelude> :set -XTemplateHaskell
Prelude> import Language.Haskell.TH
Prelude> import Language.Haskell.TH.Quote
Prelude> runQ [| $([|\x -> 3 * x|]) . $([|\y -> y + 2|]) |]
InfixE (Just (LamE [VarP x_0] (InfixE (Just (LitE (IntegerL 3))) (VarE GHC.Num.*) (Just (VarE x_0))))) (VarE GHC.Base..) (Just (LamE [VarP y_1] (InfixE (Just (VarE y_1)) (VarE GHC.Num.+) (Just (LitE (IntegerL 2))))))