Skip to main content

differential equations - VariationalD giving the wrong solution?


EDIT: As pointed out in the comments, VariationalD gives a variational derivative (which I don't want), not a derivative with respect to a function (i.e. df[x]dloge(x) as a simple example - this is what I thought it did the first time I read the description). Is it even possible in Mathematica to take the derivative of a function with respect to another function? I know you can use the chain rule to rewrite, using the example just above, df[x]dloge(x)=xdf[x]dx. In my case the equivalent would be much more cumbersome and I want to be able to change the function w.r.t. which I'm differentiating. Is it possible to do this in Mathematica?
_


I am using the VariationalMethods` package, specifically the VariationalD command. I am trying to take the derivative of one (complicated) function fitted to experimental data with respect to another (complicated) function.


First I wanted to make sure that the command does what I wanted. So I inputted the example given in VariationalD's "Examples" section:


 VariationalD[y[x] Sqrt[y'[x]], y[x], x]


This gives a result of ddy[x](y[x]y[x]1/2)=2y[x]2+y[x]y[x]4y[x]3/2.


Trying to reproduce this by hand I started with the product rule:


ddy[x](y[x]y[x]1/2)=y[x]ddy[x]y[x]1/2+y[x]1/2ddy[x]y[x]

The second term is just


y[x]1/2ddy[x]y[x]=y[x]1/2.


The first term, you use the chain rule on:


y[x]ddy[x]y[x]1/2=y[x]12y[x]1/2ddy[x]y[x]

ddy[x]y[x]=dxdy[x]ddxy[x]=(dy[x]dx)1y[x]=y[x]y[x].


Putting all of that together:


ddy[x](y[x]y[x]1/2)=y[x]1/2+12y[x]y[x]y[x]3/2.


Simplifying:



ddy[x](y[x]y[x]1/2)=2y[x]2+y[x]y[x]2y[x]3/2,


which is identical to the solution given by VariationalD except for a factor of 2. So I assumed it was a mistake I made somewhere (and it might be) and tried comparing every step I made when doing it by hand with the corresponding step in VariationalD. Eventually I discovered one difference in the step


 VariationalD[Sqrt[y'[x]], y[x], x]

which gives an answer of y[x]4y[x]3/2, a factor of 2 different than when I do it. I'm still not sure where the factor of 2 in the y[x]1/2ddy[x]y[x] step comes from.


I kept messing around with it and found that


 VariationalD[y'[x],y[x],x]=0

which can't be right. For example, if y[x]=x2 and y[x]=2x,


dy[x]dy[x]=d(2x)dx2=2(dx2dx)1=2(2x)1=1x.



So why is VariationalD giving a zero answer? It's even weirder because dy[x]dy[x] comes up when you perform the original differentiation by hand as well. It can't be zero there either or the first term would vanish and the answer would just be y[x]1/2.


So am I making a stupid mistake in my differentiation? Even if so, why is VariationalD giving zero for the derivative with respect to a function of its derivative?


I've spent about an hour searching for anything relevant online, and...nothing.


Any help would be appreciated!


EDIT: I've been looking at the description of VariationalD more closely and now I'm not sure it does what I think it does, i.e.


 VariationalD[f[x],g[x],x]

gives df[x]dg[x] as output. Is this correct? Looking at the description it now seems to me that it would give f[x]df[x]dg[x]. But if that's the case then the output for my original expression is still wrong.



Answer



I think VariationalD[y'[x],y[x],x] indeed equals to zero.



You should not mistake functional derivative with ordinary derivative, where in the former case y is usually considered an independent variable to y. so functional dy/dy=0 is just like an ordinary da/db=0.


The same reason you can't write something like this in calculus of variations


J[x,y,y]y=xyJ[x,y,y]x,


just like you can't write this in ordinary calculus:


f(x,y,z)y=xyf(x,y,z)x.


Comments

Popular posts from this blog

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

What is and isn't a valid variable specification for Manipulate?

I have an expression whose terms have arguments (representing subscripts), like this: myExpr = A[0] + V[1,T] I would like to put it inside a Manipulate to see its value as I move around the parameters. (The goal is eventually to plot it wrt one of the variables inside.) However, Mathematica complains when I set V[1,T] as a manipulated variable: Manipulate[Evaluate[myExpr], {A[0], 0, 1}, {V[1, T], 0, 1}] (*Manipulate::vsform: Manipulate argument {V[1,T],0,1} does not have the correct form for a variable specification. >> *) As a workaround, if I get rid of the symbol T inside the argument, it works fine: Manipulate[ Evaluate[myExpr /. T -> 15], {A[0], 0, 1}, {V[1, 15], 0, 1}] Why this behavior? Can anyone point me to the documentation that says what counts as a valid variable? And is there a way to get Manpiulate to accept an expression with a symbolic argument as a variable? Investigations I've done so far: I tried using variableQ from this answer , but it says V[1...