I had a task - to write a program for tabulating a function. The task is very simple and I kind of solved it. But I have doubts about the correctness of my decision. Could you tell me if I wrote the code correctly?
import math
#Задание индивидуальное
print('Individual task')
a=float(input('Enter a: '))
b=float(input('Enter b: '))
x0=float(input('Enter x0: '))
xn=float(input('Enter xn: '))
h=float(input('Enter h: '))
x=x0
while x<xn:
if x<0:
y=math.log(x-b)**2
print('x = '+str(x)+','+' y = '+str(y))
x+=h
else:
y=a*math.sqrt(x)
print('x = '+str(x)+','+' y = '+str(y))
x+=h
1 Answer 1
I'm not sure if your logic makes sense. Your function is undefined (complex) when x<0, but you instead calculate the numerator when it is negative, and the denominator when it is positive? In general that will not be what you want.
You are likely to have floating point errors and be off-by-one in your loops, so look out for that, perhaps try to correct it by using integers where possible.