0

I'm trying to build a code that asks the user to enter the equation then the code will get the derivate of it, so I can use the derivative in numerical method.

This is my current code that calculates the true/approximate value/error (this is my first python program so pardon the mess)

def ggxp(x,h):
    z1= int(((ggx(x)*ggx(h)) - ggx(x))/h)
    return z1

def ggxd(x):
    z2= 4*x+1
    return z2

def ggx(x):
    z3= int(2*x^2+x-5)
    return z3


x = int(input("Enter x \n"));
c =2;
while c > 0:
    h= int(input("Enter h \n"))
    y0 = ggxd(x)
    y1=  ggxp(x,h)
    TrueError=y0-y1;
    print("The approximate value at ", x ," is ", y1 ," \n")

    if c == 2:
        prevA = y1;
        
    c= c-1;
    

print ("The true value at ",x," is ",y0,"\n")
RelativeTE= int((TrueError/y0)*100);
print ("the relative true error is ", RelativeTE ,"\n")

if prevA != y1:
    Ea = int(prevA - y1);
    REa = int((Ea / y1) * 100)
    print("The approximate error is " , Ea , "\n")
    print("The relative approximate error is ", REa, "\n")

I want a program that runs like this but the user is the one who enters the function then the program will get the derivative by itself and then continue. (I know it's easier to do in matlab but I don't have access to matlab)

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • What is your question ? And does your code works ? – rafathasan Oct 20 '22 at 19:52
  • 1
    @Ahmad Your function `ggx` probably doesn't behave in the way that you're expecting. Note that the syntax for squaring x in python is `x**2`, not `x^2`. – Ben Grossmann Oct 20 '22 at 19:57
  • @Ahmad Perhaps the [sympy diff method](https://docs.sympy.org/latest/tutorials/intro-tutorial/calculus.html) is what you're looking for – Ben Grossmann Oct 20 '22 at 19:59
  • 1
    @RandomDavis For what it's worth, googling `how to input custom equation python` doesn't yield any obviously applicable results, and I don't know why you'd expect that somebody new to Python would know off-hand that a method to generically find the derivative of a function exists. Finding answers is a skill that is cultivated with experience; I don't see any problem with asking for help in that on this site. – Ben Grossmann Oct 20 '22 at 20:04
  • @rafathasan it does work, but the thing is that i want the functions (in the beginning of the code) to be entered by the user and the program will get the derivative of it and start calculating the same way – Ahmad yahya Oct 20 '22 at 20:19
  • Why to you truncate the function value? Read up on difference quotients, there are many posts on this topic here and on math.SE. For symbolic calculations you need a package for symbolic expressions, which for python is sympy. There is also a function there that parses a string into an expression. – Lutz Lehmann Oct 20 '22 at 20:34
  • Thanks sympy did work with me – Ahmad yahya Oct 20 '22 at 20:50
  • sympy is also compatible with [latex](https://stackoverflow.com/questions/74145291/how-to-count-how-many-times-i-got-the-certain-result) syntax – cards Oct 20 '22 at 22:00
  • It is quite unclear if 1) you want to enter a function symbolically at run-time or just in code, 2) if you want a numerical or symbolic derivative. –  Oct 21 '22 at 10:23

0 Answers0