Filter löschen
Filter löschen

get x knowing f(x)

1 Ansicht (letzte 30 Tage)
Francesco Pio
Francesco Pio am 17 Jul. 2023
Kommentiert: Francesco Pio am 17 Jul. 2023
Let's suppose we have the following example function
syms f(x)
f(x) = e^x
is there a way to derive the value on the x axis by giving an y ​​value as input?
  2 Kommentare
Dyuman Joshi
Dyuman Joshi am 17 Jul. 2023
I am not sure if this is what you are looking for, but you can use solve -
syms f(x)
f(x) = exp(x);
%input
y0 = 5;
%Obtain corresponding x value
x0 = solve(f(x)==y0,x)
x0 = 
Francesco Pio
Francesco Pio am 17 Jul. 2023
it works, thanks a lot!

Melden Sie sich an, um zu kommentieren.

Antworten (2)

John D'Errico
John D'Errico am 17 Jul. 2023
Is there ALWAYS a simple solution? NO! SOMETIMES, there is. But for almost all problems you might write down, there is no algebraic solution. And for many such problems, we can even prove that nothing can or will ever generate a solution, so no matter how good is your computer, how smart, how fast, how powerful, it will never be able to solve some problems.
For simple problems, like
y = exp(x)
Mathematics can solve that problem, because mathematics has given us the log function. And we know the inverse of exp(x) is given by the log function. So if you have y, then x is given by
x = log(y)
Just basic mathematics there. x has a real solution as long as y is positive.
For slightly more complicated functions, perhaps
y = x + exp(x)
then we can always use fzero, although there may be multiple solutions, and fzero will find only ONE solution. That is a characteristic of all numerical solvers.
f = @(x) x + exp(x);
y = 7;
[xsol,fval,exitflag] = fzero(@(x) f(x) - 7,1)
xsol = 1.6728
fval = -8.8818e-16
exitflag = 1
And that is A solution of the problem. Of course, even that problem has an analytical solution, again, because mathematics has found this problem to be a useful one to solve. So mathematicians have given us the Lambert W function.
syms X
solve(f(X) - y == 0,X)
ans = 
But again, not all problems have an algebraic solution. Sometimes, solve will just resort to vpasolve, when that is possible.
solve(cos(X) - X == 0,X)
Warning: Unable to solve symbolically. Returning a numeric solution using vpasolve.
ans = 
0.73908513321516064165531208767387
Make it slightly harder, like this...
syms a
solve(cos(X) - X == a,X)
Warning: Unable to find explicit solution. For options, see help.
ans = Empty sym: 0-by-1
And you see that solve just gives up, as it should.

Paul
Paul am 17 Jul. 2023
finverse works well in this case.
syms f(x) y
f(x) = exp(x);
g(y) = subs(finverse(f),x,y)
g(y) = 
g(23)
ans = 

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by