4

I am trying to evaluate a function in Scilab using the following steps:

x=poly(0,'x')
y=(x^18+x^11)^3 // function (the function is variable)
y1=derivat(y) // first derivate
y2=derivat(y) //second derivate
y3=derivat(y) //third derivate

I need evaluate the 3 derivatives in any point.

I know the function: evstr(expression) but it does not work with the return value of the derivative.

I try to use: string(y) but it returns something strange.

How can to do it, to cast the return of derivat to string to evaluate with evstr or how can I evaluate the n-th derivative in any point using Scilab.

Code Geas Coder
  • 1,839
  • 4
  • 23
  • 29

4 Answers4

1

To evaluate numerical derivatives of almost any kind of function (of one or sereval variables) up to machine precision (you won't get better results if you evaluate symbolic expressions obtained by hand), you can use the complex step method (google these terms you will have a bunch of references). For example:

function y = f(x)
  s = poly(0,'s');
  p = (s-s^2)^3;
  y = horner(p,x).*exp(-x.^2);
end

x=linspace(-1,1,100);
d = imag(f(x+complex(0,1e-100)))/1e-100;

true_d = exp(-x.^2).*(-1+x).^2.*x^2.*(3-6*x-2*x.^2+2.*x^3)

disp(max(abs(d-true_d)))

--> disp(max(abs(d-true_d)))

   1.776D-15
Stéphane Mottelet
  • 2,853
  • 1
  • 9
  • 26
  • Is this more accurrate than applying numderivative n times? – EmmanuelMess Oct 12 '20 at 12:13
  • It only works for first derivative, and for first derivative it yields the same result as exact computation using floating point arithmetic. For n-th order derivative see Cleve Moler's historical paper (https://www.math.fsu.edu/~okhanmoh/media/Lyness,%20Moler,%20SJNA,%201967,%20Numerical%20differentiation%20of%20analytic%20functions.pdf)> – Stéphane Mottelet Oct 12 '20 at 15:02
0

To evaluate a symbolic polynomial at a particular point or points, use the horner command. Example:

t = 0:0.1:1
v1 = horner(y1, t)
plot(t, v1)
  • And when is not polynomial, i have the problem when is a trigonometric function or logarithm function – Code Geas Coder Mar 13 '15 at 02:51
  • Scilab does not support symbolic differentiation of arbitrary functions; `derivat` works only for polynomials and rational functions. You can compute the derivative numerically, if you wish (`diff` command, for example). –  Mar 13 '15 at 02:57
0

This is the closest I got to a solution to this problem.

He proposes using:

old = 'f';
for i=1:n
    new = 'd'+string(i)+'f';
    deff('y='+new+'(x)','y=numderivative('+old+',x)');
    old=new;
end

I know, it's horrible, but I think there is no better solution, at least in Scilab.

0

I found a way:

function y = deriva(f, v, n, h)
    deff("y = DF0(x)", "y="+f)
    if n == 0 then
        y = DF0(v);
     else
        for i=1:(n-1)
            deff("y=DF"+string(i)+"(x)", "y=numderivative(DF"+string(i-1)+",x,"+string(h)+",4)");
        end
        deff("y=DFN(x)", "y=numderivative(DF"+string(n-1)+",x,"+string(h)+",4)");
        y = DFN(v);
    end
endfunction

disp(deriva("x.*x", 3, 2, 0.0001));

This correctly calculates numerical derivatives of nth order. But it needs to have the function passed as a string. Errors can get pretty large, and time to compute tends to go up fast as a function of n.

EmmanuelMess
  • 1,025
  • 2
  • 17
  • 31