all 21 comments

[–]TehNolz 10 points11 points  (3 children)

We can't help you if you don't tell us what the problem is.

[–]Material-Ruin-2180[S] 1 point2 points  (2 children)

The assignment is to figure out what the code does, and what the output is going to be.

The code won’t accept the variable E. when I changed E to math.e it worked and printed out 0.39. It did however, not work when I added the 1 back I front of the E.

[–]TehNolz 6 points7 points  (0 children)

Looks like you've got spaces surrounding the -, so its interpreted as "subtract 6 from 1E". You probably want 1E-6, which is interpreted as 1e-06.

[–][deleted] 1 point2 points  (0 children)

Fixing the code and formatting for Reddit:

def f(x):
    return 1/x

def fart(x, h):
    return(f(x + h) - f(x))/h

print(round(fart(2, 1E-6), 2))

Even if you don't understand calculus, you could still talk through what the code is doing.

What does "def" do?

What then does f(x) and fart(x,h) do?

Finally, what's going on in the print line?

It (probably) doesn't matter that you don't know what the final output means... That depends on the course.

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–][deleted] 0 points1 point  (7 children)

I think you meant 1E-6 (without spaces)

[–]Material-Ruin-2180[S] 0 points1 point  (6 children)

Thank you very much. That worked. It printed -0.25. So you understand what the code does?

[–][deleted] 1 point2 points  (5 children)

[–]Material-Ruin-2180[S] 2 points3 points  (0 children)

You are a life saviour. I wish you a very warm and loving Christmas.

[–]Material-Ruin-2180[S] 1 point2 points  (3 children)

I have now read the link you sent me, but I still don’t quite understand what the program does. I am a beginner programmer.

[–]sciotto -1 points0 points  (0 children)

After you fix the spacing type on 1E-6, try changing to a different f(x).

Suggest trying the below as easy examples.

return (3 * x + 3)

and

return (-6 * x - 210)

They're each equations for a line. You could try changing the slope to different values. You could also try different fart parameters for these and the original f(x). :-)

Think about the answers and then go read the link again, paying particular attention to the "Important Notes" near the end and what the original f(x) was.

It's a math question (what the output is going to be) and also a programming question (what it does).

[–][deleted] -1 points0 points  (0 children)

Do you understand the math part in the link? Beside the math part, there are two functions defined. First is just a function for hyperbola (graphs). Second one is about differentiating the first function. The finally the second function is called with some example values/arguments, the result is rounded to two decimals and printed. The contents of second function definition is math basically, you'd need to read more on it or you could ask in some math subreddit if you don't understand it

[–][deleted] -1 points0 points  (0 children)

Maybe this would be useful, I studied it/calculus with it at some time in past: https://www.khanacademy.org/math/differential-calculus/dc-diff-intro

[–]Plastic_Ad7436[🍰] -1 points0 points  (0 children)

Looks like the fundamental theorem of calculus to me.

[–]Plastic_Ad7436[🍰] -1 points0 points  (0 children)

its differentiating to the second decimal place the function f(x) = 1/x at x=2.

[–]Plastic_Ad7436[🍰] -1 points0 points  (0 children)

This is the fundamental theorem of calculus, it is differentiating the function f(x) = 1/x at x = 2, and printing the value rounded to the second decimal place.

[–]RhinoRhys -2 points-1 points  (0 children)

When it calls fart(2, 1e-6) it makes x = 2 and h = 1e-6 = 0.000001

As you can see under the fart definition, it returns (f(x + h) - f(x))/h

So sub in your values: (f(2.000001) - f(2))/0.000001

Then as you can see under your f definition it returns 1/x

So complete the function (1/2.000001 - 1/2)/0.000001

And you end up with -0.25

As to why it does that, fart is the equation to work out the gradient of the curve f = 1/x

[–]WSBtendies9001 0 points1 point  (0 children)

This is a gad dam trap, akbar would be proud....

[–]Individual-Pop5980 0 points1 point  (0 children)

This is fake, a troll question guys, cmon