How do I get the inverse of a function? - wolfram-mathematica

If I have some function y[x_]:=ax+b (just an example), how do I obtain x[y_]:=(y-b)/a in Mathematica? I've tried InverseFunction,Collect and they don't work.

Treat it as an equation and use Solve.
In:=Solve[y-ax-b==0,x]
Out={{x -> (-b + y)/a}}

If you want to define a function, you could do:
x[y_] := x /. Solve[y == a x + b, x][[1]]
x[1]
-> (1 - b)/a

http://mathworld.wolfram.com/InverseFunction.html
Specifically the line:
In Mathematica, inverse functions are
represented using InverseFunction[f].

One way is with Solve:
In[29]:= Solve[y == a x + b, x]
Out[29]= {{x -> (-b + y)/a}}

Related

How to fix 'Equations may not give solutions for all "solve" variables' error

I am attempting a problem that can be solved with MUC (method of undetermined coefficients).
However, when I use the Solve function, it gives an error.
y[x_] := a x^3 + b x^2 + c x + d
Solve[{y''[x] + 2 y'[x] + y[x] == x^3}, {a, b, c, d}]
[ERROR]:
Solve::svars: Equations may not give solutions for all "solve" variables.
Shouldn't this solve for all variables in the set?
Thank You for your help :)
Looks like some extra methodology is needed for this.
As you stated, a function with the finite family of derivatives for x^3 is
y[x_] := a x^3 + b x^2 + c x + d
Equating coefficients
sol = Solve[Thread[CoefficientList[
y''[x] + 2 y'[x] + y[x], x] == CoefficientList[x^3, x]]]
{{a -> 1, b -> -6, c -> 18, d -> -24}}
Checking the results
FullSimplify[y''[x] + 2 y'[x] + y[x] == x^3 /. sol]
{True}

Using Dsolve with Assumptions/Conditions

I would like to solve the following equation:
DSolve[u''[x]+k^2 u[x], u[x],x]
if k^2<0 the solution is
u[x]-> C[1] e^(kx) + C[2] e^(-kx)
if k^2>0 the solution is
u[x] -> C[1] Sin [kx] + C[2] Cos[kx]
in my equation
k^2=(a-b)/(c-d)
when b >a and c >d, meaning k^2<0
when I plug the equation into Mathematica, it reverses the sign and given me the exponents solution and not the cosine one.
does anyone have an idea how to plug the Assumptions or Conditions into the equation? Or patch between the two so I'll get the true solution?
Cheers
introduce a constant k2n which is the negative of your assumed negative k^2:
First#DSolve[{u''[x] - k2n u[x] == 0 }, u[x], x]
E^(Sqrt[k2n] x) C[1] + E^(-Sqrt[k2n] x) C[2]
now we know k2n>0 so back substitute
% /. Sqrt[k2n] -> k
E^(k x) C[1] + E^(-k x) C[2]
As a general answer I don't think there is a way to tell DSolve to make assumptions about parameters.

How do I force Mathematica to include user defined functions in Simplify and FullSimplify?

Let's say I have a relation r^2 = x^2 + y^2. Now suppose after a calculation i get a complicated output of x and y, but which could in theory be simplified a lot by using the above relation. How do I tell Mathematica to do that?
I'm referring to situations where replacement rules x^2+y^2 -> r^2 and using Simplify/FullSimplify with Assumptions won't work, e.g. if the output is x/y + y/x = (x^2+y^2)/(xy) = r^2/(xy).
Simplification works really well with built in functions but not with user defined functions! So essentially I would like my functions to be treated like the built in functions!
I believe you are looking for TransformationFunctions.
f = # /. x^2 + y^2 -> r^2 &;
Simplify[x/y + y/x, TransformationFunctions -> {Automatic, f}]
(* Out= r^2/(x y) *)
In the example you give
(x/y + y/x // Together) /. {x^2 + y^2 -> r^2}
==> r^2/(x y)
works. But I've learned that in many occasions replacements like this don't work. A tip I once got was to replace this replacement with one which has a more simpler LHS like: x^2 -> r^2-y^2 (or even x->Sqrt[r^2-y^2] if you know that the values of x and y allow this).

mathematica, Simplify trig function

if say i have a function given :
singlepattern = Cosh[theta] + Cosh[3theta]
How do i get a rational expression in terms of x of the function if i want to substitute Cosh[theta] by
"Cosh[theta] = ( x )/ 2 "
expression?
I retagged the question as a homework. You should look into ChebyshevT polynomials. It has the property that ChebyshevT[3, Cos[th] ]==Cos[3*th]. So for your problem the answer is
In[236]:= x/2 + ChebyshevT[3, x/2]
Out[236]= -x + x^3/2
Alternatively, you could use TrigExpand:
In[237]:= Cos[th] + Cos[3*th] // TrigExpand
Out[237]= Cos[th] + Cos[th]^3 - 3 Cos[th] Sin[th]^2
In[238]:= % /. Sin[th]^2 -> 1 - Cos[th]^2 // Expand
Out[238]= -2 Cos[th] + 4 Cos[th]^3
In[239]:= % /. Cos[th] -> x/2
Out[239]= -x + x^3/2
EDIT The reason the above has to do with the explicit question, is that Cosh[theta] == Cos[I*u] for some u. And since u or theta are formal, results will hold true.
Use Solve to solve for theta, then substitute, Expand, and Simplify:
In[16]:= TrigExpand[Cosh[3 theta] + Cosh[theta]] /.
Solve[Cosh[theta] == (x)/2, theta] // FullSimplify
During evaluation of In[16]:= Solve::ifun: Inverse functions are being used by Solve,
so some solutions may not be found; use Reduce for complete solution information. >>
Out[16]= {1/2 x (-2 + x^2), 1/2 x (-2 + x^2)}
This might interest you:
http://www.wolframalpha.com/input/?i=cosh%28x%29+%2B+cosh%283*x%29

How to substitute an aggregate expression

For example, I have symbollically
1/n*Sum[ee[k] + 1, {k, j, n}]^2
And I want to substitute Sum[ee[k], {k, j+1, n}] to be x. How can I do this? May thanks for your help!
You may use the recurrence relation for the sum. For example:
f[j] := f[j + 1] + (ee[j] + 1);
1/N f[j]^2 /. f[j + 1] -> x
Out
(1 + x + ee[j])^2/N
Edit
Based on several questions you posted, I think you are somehow misinterpreting what the Replace[] command does. It is not "algebraic" based, but "pattern" based. It doesn't understand nor use more algebraic transformations than those already defined (by you or by Mma itself).
For example:
x/. (x-1)->y
will not match anything. But
(x-1) /. x->y-1
Will give you (y-2) because the pattern x is matched.
Moreover:
x = 3;
(x - 1) /. x -> y - 1
will give you 2 because x is evaluated before the possible match, and the x in the pattern is also evaluated (just paste, execute and look at the symbol color).
1/N*Sum[ee[k] + 1, {k, j, N}]^2 /. Sum[ee[k] + 1, {k, j, N}] -> x
Doesn't that work, or do I misunderstand? By the way, you shouldn't use N as a variable. It's a Mathematica function.

Resources