Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.4K Views
|
12 Replies
|
1 Total Like
View groups...
Share
Share this post:

Custom function that takes a variable as an argument to derivative wrt. it?

Posted 1 year ago

This doesn't work but gives the general idea. I want to specify which argument I take one derivative with respect to, for a function with an arbitrary number of variables. ff is one such function (happens to have 3 variables) and in the example I want to take the partial derivative with respect to the second variable. This will be used in coding numerical dif eqs with lots of variables.

POSTED BY: Iuval Clejan
12 Replies
Posted 1 year ago

Thanks Eric and Gianluca. I added Eric's suggestion, but I am still having trouble. I guess I need the function to have a list of arguments, so I define like that, though I could use Sequence instead. Here is a file that tries to solve a set of ordinary difeqs. I used a simple example for ff, just for illustration.

POSTED BY: Iuval Clejan
Posted 1 year ago

I used a simple ff (not the one I need for the real thing) for illustration, with 12 variables, and the difeqs have 6 variables.

POSTED BY: Iuval Clejan

There are three subtle problems with how you define your function ff. The first is that you collect the arguments in a list {c1...}. Consider the two following functions:

f[x_, y_, z_] := x^2 + y z;
g[{x_, y_, z_}] := x^2 + y z;

The two functions achieve the same goal, but the input is packaged in two different ways. The function f is a function of THREE variables, one in each slot. The function g is a function of ONE vector variable. The mechanism of Derivative relies on the counting of slots, so that it only applies to f, not to g. Try to calculate the derivative with respect to y:

Derivative[0, 1, 0][f][x, y, z]
Derivative[0, 1, 0][g][{x, y, z}]

The second problem is that you define ff only for NUMERIC values of the variables ff[{c1_Real, c2_Real... This makes it impossible to calculate the derivatives, because calculating a derivative involves the function calculated on a SYMBOLIC input.

The third problem is that your ff depends on 13 variables, while your derivative operator nder[n, 12, ff] assumes 12 variables.

The following code runs without errors:

sig = 1/10; teq = 1;
cout0 = Table[If[i <= 4, 1, 1/16], {i, 1, 6}];
nder[n_, m_, f_] := (Derivative @@ UnitVector[m, n])[f];
ccvar = Table[cc[i][t], {i, 6}];
ff[c1_, c2_, c3_, c4_, alpha11_, alpha21_, alpha31_, alpha41_,
   alpha12_, alpha22_, alpha32_, alpha42_, alpha13_] = 1;
eqnlist = Flatten[Table[{Derivative[1][cc[n]][t] == sig*
       nder[n, 13, ff][Sequence @@ Flatten[{ccvar, ccvar}]],
     cc[n][0] == cout0[[n]]},
    {n, 6}]];
solDEsuper = NDSolve[eqnlist, ccvar,
  {t, 0, teq}]
POSTED BY: Gianluca Gorni
Posted 1 year ago

Gianluca, I can deal with the first problem with using Sequence. The third is just that when I simplified my example, I didn't count right--my original has 20 variables in the eqn and 40 arguments to ff (which is much more complicated than a constant), and I wanted something simpler for the example (sorry for that). As far as the numerical derivative, I have needed this in the past, it wouldn't work without it. I will see now if I fix the first problem if it will work.

POSTED BY: Iuval Clejan
Posted 1 year ago

It seems that the error I'm getting now has to do with too many variables (40). Here is an example where everything is fine with evaluating ff, which has 20 variables, but with 40 variables it refuses to evaluate ffsuper.

POSTED BY: Iuval Clejan
Posted 1 year ago

I wonder if this is just a problem with my old version of Mathematica.

POSTED BY: Iuval Clejan

It is just a typo: Alpha23_RealA.

POSTED BY: Gianluca Gorni
Posted 1 year ago

Wow. Thanks so much. Your eyes are better than mine. I was thinking I have to buy a new laptop to get the new version, because my laptop has an older MacOS, and last time I tried installing new OS it said it didn't have enough memory (and I don't think I have more RAM slots).

Incidentally, the old version, which was initially installed on a linux machine, gives me annoying font errors (I have to close the notebook and restart Mathematica for it to go away). The tech support guy assured me I won't have this problem if I get the new version, but as I said above that would probably mean a new laptop.

POSTED BY: Iuval Clejan

The code we discussed here does not require recent versions of Mathematica. However, more memory helps with larger problems. A new laptop may be a good idea.

POSTED BY: Gianluca Gorni
Posted 1 year ago

A new laptop is a last resort due to expense. It's crazy that I should spend so much money just because of a font problem: INTERNAL SELF-TEST ERROR: MacFont|c|451 Click here to find out if this problem is known, and to help improve Mathematica by reporting it to Wolfram Research.

POSTED BY: Iuval Clejan

A little shorter still:

nder[n_, m_, f_] := (Derivative @@ UnitVector[m, n])[f]
nder[2, 3, ff]
POSTED BY: Gianluca Gorni
Posted 1 year ago

If you read the documentation for Derivative, you'll see that the expected form is

Derivative[n1, n2, n3, ...]

The form you're generating is

Derivative[{n1, n2, n3, ...}]

So, you could try this:

nder[n_, m_, f_] := Derivative[Sequence @@ nvec[n, m]][f]

Also, there is a built-in function for your nvec: UnitVector. So you could actually do this:

nder[n_, m_, f_] := Derivative[Sequence @@ UnitVector[m, n]][f]
POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard