Group Abstract Group Abstract

Message Boards Message Boards

0
|
4.1K 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 2 years ago
POSTED BY: Iuval Clejan
12 Replies

It is just a typo: Alpha23_RealA.

POSTED BY: Gianluca Gorni
Posted 2 years ago
POSTED BY: Iuval Clejan
POSTED BY: Gianluca Gorni
Posted 2 years 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
Posted 2 years ago

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

POSTED BY: Iuval Clejan
Posted 2 years 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 2 years 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

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 2 years 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
Posted 2 years 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

A little shorter still:

nder[n_, m_, f_] := (Derivative @@ UnitVector[m, n])[f]
nder[2, 3, ff]
POSTED BY: Gianluca Gorni
Posted 2 years ago
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