필터 지우기
필터 지우기

get x knowing f(x)

조회 수: 1 (최근 30일)
Francesco Pio
Francesco Pio 2023년 7월 17일
댓글: Francesco Pio 2023년 7월 17일
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
Dyuman Joshi
Dyuman Joshi 2023년 7월 17일
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 2023년 7월 17일
it works, thanks a lot!

댓글을 달려면 로그인하십시오.

답변 (2개)

John D'Errico
John D'Errico 2023년 7월 17일
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 2023년 7월 17일
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 = 

카테고리

Help CenterFile Exchange에서 Conversion Between Symbolic and Numeric에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by