0
$\begingroup$

I need to evaluate the derivative of a function. My function looks like

approx[vector_, x_] :=  Sum[vector[[n]]*Basis[n - 1, x], {n, 1, Length[vector]}]

and I'm defining my derivative like:

ddappx[vector_, x_] := D[approx[vector, x], {x, 2}] 

However if I evaluate it at a specific x I get an error:

General::ivar: 1 is not a valid variable. >>

I searched enough to know that a solution when plotting this is to evaluate it first. Something about HoldAll and so on.

What I need to do is use it in a set of simultaneous equations. So I should be able to have:

Solve[{ddappx[{a,b}, 1]==0,ddappx[{a,b}, -1]==0},{a,b}]

To solve for {a,b} by setting the 2nd derivative of approx ==0 at x=1 and x=-1. However I get the

General::ivar: 1 is not a valid variable. >>

Errors again. Evaluating like

 Solve[{Evaluate[ddappx[{a,b}, 1]]==0,Evaluate[ddappx[{a,b}, -1]]==0},{a,b}]

isn't working.

so, any ideas?

cheers

EDIT: Having made the suggested change, that error is solved. However although evaluating approx works perfectly well, evaluating ddappx gives 0, no matter what I do. there is no error, just 0. I've added the whole code which goes in front just in case you think its relevant

diffeq[y_, x_] := y''[x] + y[x]
f[x_] := Cos[2*x]
Basis[n_, x_] := ChebyshevT[n, x]
approx[vector_, x_] := Sum[vector[[n]]*Basis[n - 1, x], {n, 1, Length[vector]}]

ddappx[vector_, x_] := Derivative[0, 2][approx][vector, x]

....

approx[{0, 0, 1, 1}, x]

returns correctly:

-1 - 3 x + 2 x^2 + 4 x^3



 ddappx[{0, 0, 1, 1, 1}, x]

returns incorrectly:

0
$\endgroup$
1

1 Answer 1

1
$\begingroup$

You need to define the function representing the derivative as follows:

ddappx[vector_, x_] := Derivative[2][approx[vector, #] &][x]

Derivative is the form in which your definition would have been stored had you done

Clear[approx1, vector, x]

ddappx[vector_,x_] = D[approx1[vector, x], {x, 2}]

$\text{approx1}^{(0,2)}(\text{vector },x)$

(i.e., defined the function in terms of some generic unassigned symbols first - using Set). But the first definition using SetDelayed works independently of whether you had a previous definition for approx. D gets converted to Derivative when applied to generic functions, and Derivative itself acts like a function to which you can supply variables, in this case vector and x.

In your case, there was another problem because vector as an argument in the derivative is actually interpreted as a list of additional function arguments, so that the variable x doesn't get counted as variable slot number 2 in general. If the vector has four elements, e.g., then the variable x is counted as the fifth slot of the function for the purposes of differentiation. In order to circumvent this slot counting problem, I would supply the function approx to the derivative in the form of a single-argument function, which is done by the using anonymous function approx[vector, #] &.

$\endgroup$
3
  • $\begingroup$ Thankyou! You have done everything I asked because you solved that error...however I had already tried it this way and it gave a different problem. I will have to put it in an edit I think to explain $\endgroup$
    – user5866
    Commented Feb 26, 2013 at 3:18
  • $\begingroup$ OK, thanks for adding the missing definition of Basis - that allowed me to actually try your code... and I think the updated ddappx should work for you. $\endgroup$
    – Jens
    Commented Feb 26, 2013 at 4:12
  • $\begingroup$ Thankyou that works perfectly :) $\endgroup$
    – user5866
    Commented Feb 26, 2013 at 4:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.