views:

142

answers:

4

Please help! cannot figure this out for the life of me

Given the function f(x,n)= n**x(n-1)

5c. Using this function, calculate the rate of change of (((2^3 + 3^2)^4 -2^4)^2 + (3^4 – (6^2 + 3)^4)^3)^3

This is what I came up with in IDLE:

def function(x, n):
    return (n*(x**(n-1)))

assertEqual (
  function ((((
    function (2.0, 3.0))+(
      function (3.0, 2.0)), 4.0)-(
        function (2.0, 4.0)), 2.0)+((
          function (3.0, 4.0))-((
            function (6.0, 2.0))+(
              function (3.0, 1.0)), 4.0), 3.0), 3.0), 35994405888.0)

and after I save and run it, I get this message:

Traceback (most recent call last):
  File "C:\Users\Jonathan Cohen\Desktop\School\CISC 106\lab2.py", line 83, in <module>
    assertEqual (function ((((function (2.0, 3.0))+(function (3.0, 2.0)), 4.0)-(function (2.0, 4.0)), 2.0)+((function (3.0, 4.0))-((function (6.0, 2.0))+(function (3.0, 1.0)), 4.0), 3.0), 3.0), 35994405888.0)
TypeError: unsupported operand type(s) for -: 'tuple' and 'float'
+3  A: 

(function(a, b), c) is a 2-tuple consisting of the result of function(a,b) and c.

If you want to represent (a^b)^c, you'd need something like function(function(a,b), c) (assuming function() computes its first parameter raised to the second.)

Wooble
+2  A: 

There are a few occurances of this, but here is the first one.

function ((((function (2.0, 3.0))+(function (3.0, 2.0)), 4.0)-(function (2.0, 4.0)), 2.0)+((function (3.0, 4.0))-((function (6.0, 2.0))+(function (3.0, 1.0)), 4.0), 3.0), 3.0)
           ((function (2.0, 3.0))+(function (3.0, 2.0)), 4.0)

The segment that I've blocked out, produces a tuple of (18.0, 4.0). You then try to do normal math on this, which fails.


In response to the comment, this particular segment would work better if you did the following:

function((function (2.0, 3.0))+(function (3.0, 2.0)), 4.0)
sharth
how can I correct this segment then?
Jonathan
+1  A: 

This starts off looking like calculus to me, but quickly goes off the rails.

Elementary differential calculus says that if you have a function:

alt text

then the first derivative with respect to the independent variable x is:

alt text

It looks to me like you're trying to evaluate something like this with Python, but the tuple expression you give makes no sense to me. Was there supposed to be an independent variable hidden in there somewhere?

duffymo
agreed. I stared at this question several times today, saw the derivative connection, but still could make no sense of the question.
ma3
A: 

So after two weeks of pulling my hair out trying to figure this out I think I finally got it. Thanks so much everybody, all your input really helped! I broke down each part and rewrote everything according to what you all suggested. This is what I came up with:

def function (x,n):
    return (n*(x**(n-1)))

assertEqual (function (function (function((function (2, 3))+(function (3, 2)), 4)-function(function (2, 4), 1), 2)+function (function ((function (3,4)),1)-function ((function (6,2))+(function (3,1)),4),3),3), 161027925052617363)

after saving it and running it, the Python shell tells me it's successful, what do you all say?

Once again thanks for all your help!

Jonathan
Where did you get 161027925052617363 from? or 35994405888.0 (from the original question)? How do you know which, if either one, is the correct answer? There's still a lot of info missing here.
ma3
I still say it's absolute crap, regardless of whether or not the Python shell accepted it and gave you an answer. The value for x ought to be the same for each function call if you're evaluating at a particular point, but it's not. It's either a bad assignment, a poor implementation, or both.
duffymo