Search code examples
scilab

Is it possible to use the input command to enter functions in order to use numderivative?


I've been working on numerical methods to solve polynomial and non-polynomial equations. I wanted to use numderivative to calculate the defined derivative of a function entered by the user with the following simple code:

clc
clear 
x0 =input('Enter the x value: ') // x0 = 4
function y = f(x)
    y = input('Enter your function: ') // y = sqrt(x)
endfunction
dd = numderivative(f,x0)
printf('The definite derivative value of f(x) in x = %d is %.6f',x0,dd)
  • The output is the following:

    Enter the x value: 4

    Enter your function: sqrt(x)

    Enter your function: sqrt(x)

    The definite derivative value of f(x) in x = 4 is 0.250000

  • This code asks for the function twice. I would like to know how to solve that problem. Thank in advance.


Solution

  • No it is not possible to enter a function, but you can enter the instructions of the function:

    x0 =input('Enter the x value: ') // x0 = 4 
    instr = input('Enter the expression to derivate as a function of x: ',"s")//sqrt(x)
    deff("y=f(x)","y="+instr)
    
    dd = numderivative(f,x0)
    printf('The definite derivative value of f(x) in x = %d is %.6f',x0,dd)