get x knowing f(x)
2 次查看(过去 30 天)
显示 更早的评论
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
2023-7-17
syms f(x)
f(x) = exp(x);
%input
y0 = 5;
%Obtain corresponding x value
x0 = solve(f(x)==y0,x)
回答(2 个)
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)
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)
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)
Make it slightly harder, like this...
syms a
solve(cos(X) - X == a,X)
And you see that solve just gives up, as it should.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!