Computing the inverse of function in MATLAB

后端 未结 3 936
无人共我
无人共我 2021-02-10 17:16

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?

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-10 17:40

    If the analytical approach fails (which is preferred whenever possible) use numerical approach:

    Given y and guess x0 for the inverse

    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

    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.

提交回复
热议问题