在 MATLAB 中计算函数的逆
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19083078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Computing the inverse of function in MATLAB
提问by hherklj kljkljklj
How do you compute the inverse of a function in MATLAB? Say you want to compute the inverse of f(x)=e^x, what would be the code?
在 MATLAB 中如何计算函数的倒数?假设你想计算 f(x)=e^x 的倒数,代码是什么?
回答by Andreas H.
If the analytical approach fails (which is preferred whenever possible) use numerical approach:
如果分析方法失败(尽可能首选),请使用数值方法:
Given y and guess x0 for the inverse
给定 y 并猜测 x0 的逆
x = fzero( @(x)(f(x)-y), x0 );
or a low accuracy but faster method when the range of x known to be bounded in xmin...xmax
或者当 x 的范围已知受限于 xmin...xmax 时,一种低精度但速度更快的方法
xx = linspace( xmin, xmax, N );
yy = f(xx);
x = interp1(yy, xx, y);
Of course, N has to be chosen according to the desired accuracy.
当然,必须根据所需的精度选择 N。
回答by Cici
you can use finverse from the symbolic math toolbox http://www.mathworks.com/help/symbolic/finverse.htmlbut for your example you can just do ln()?
您可以使用符号数学工具箱中的 finverse http://www.mathworks.com/help/symbolic/finverse.html但对于您的示例,您可以只执行 ln()?
回答by Marco Haschka
Numerical inverse of a monotone Function: let v be a monotone ascending array of numbers (that means v=sort(v)). Then you can derive the inverse (vinv) very simple:
单调函数的数值倒数:让 v 是一个单调递增的数字数组(这意味着 v=sort(v))。然后你可以非常简单地推导出逆(vinv):
vinv=cumsum(hist(v,length(v)));
vinv=cumsum(hist(v,length(v)));
After this you can beautify the result with a bit of scaling, but basically the cumsum of hist-thingi does the trick.
在此之后,您可以通过一些缩放来美化结果,但基本上hist-thingi 的 cumsum 就可以解决问题。
You can test the following:
您可以测试以下内容:
x=randn(1,1000);
v=sort(x);
plot(v);
vinv=cumsum(hist(v,1000));
figure(2);
plot(vinv);