0

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?

The essence of the task

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
asked Nov 25, 2019 at 5:37

1 Answer 1

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.

answered Nov 25, 2019 at 5:45
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.