How should you calculate partial derivative ∂f/∂x using first the chain rule & directly in MAPLE? - derivative

If f(u,v) = vsinu+v^2, u(x,y)=tan^−1􏰀(y􏰁/x), v=sqrt(􏰂x^2 + y^2)
Calculate using chain rule and directly substituting for u(x,y), v(x,y).

Your syntax is not altogether clear to me, but I am guessing you are trying to achieve something like this:
uveqs := [u(x,y)=arctan(y/x), v(x,y)=sqrt(x^2+y^2)]:
f := v(x,y)*sin(u(x,y))+v(x,y)^2:
Differentiate f with respect to x (noticing the effect of the chain-rule),
K := diff(f,x):
diff(v(x,y),x) * sin(u(x,y))
+ v(x,y) * diff(u(x,y),x) * cos(u(x,y))
+ 2*v(x,y) * diff(v(x,y),x)
Now substitute for u(x,y) and v(x,u), and simplify,
S1 := eval(K,uveqs):
simplify(S1);
2 x
Alternatively, first substitute for u(x,y) and v(x,y) into f itself,
fxy := eval(f,uveqs):
(x^2+y^2)^(1/2)*y/x/(1+y^2/x^2)^(1/2)+x^2+y^2
And then differentiate with respect to x, and simplify,
S2 := diff(fxy,x):
simplify(S2);
2 x
If you have trouble understanding the mechanisms, you might also compare these two pairs of operations,
diff( v(x,y), x );
eval( %, uveqs );
and,
eval( v(x,y), uveqs );
diff( %, x );

Related

Define the function for distance matrix in ampl. Keep getting "i is not defined"

I'm trying to set up a ampl model which clusters given points in a 2-dimensional space according to the model of Saglam et al(2005). For testing purposes I want to generate randomly some datapoints and then calculate the euclidian distance matrix for them (since I need this one). I'm aware that I could only make the distance matrix without the data points but in a later step the data points will be given and then I need to calculate the distances between each the points.
Below you'll find the code I've written so far. While loading the model I keep getting the error message "i is not defined". Since i is a subscript that should run over x1 and x1 is a parameter which is defined over the set D and have one subscript, I cannot figure out why this code should be invalid. As far as I understand, I don't have to define variables if I use them only as subscripts?
reset;
# parameters to define clustered
param m; # numbers of data points
param n; # numbers of clusters
# sets
set D := 1..m; #points to be clustered
set L := 1..n; #clusters
# randomly generate datapoints
param x1 {D} = Uniform(1,m);
param x2 {D} = Uniform(1,m);
param d {D,D} = sqrt((x1[i]-x1[j])^2 + (x2[i]-x2[j])^2);
# variables
var x {D, L} binary;
var D_l {L} >=0;
var D_max >= 0;
#minimization funcion
minimize max_clus_dis: D_max;
# constraints
subject to C1 {i in D, j in D, l in L}: D_l[l] >= d[i,j] * (x[i,l] + x[j,l] - 1);
subject to C2 {i in D}: sum{l in L} x[i,l] = 1;
subject to C3 {l in L}: D_max >= D_l[l];
So far I tried to change the line form param x1 to
param x1 {i in D, j in D} = ...
as well as
param d {x1, x2} = ...
Alas, nothing of this helped. So, any help someone can offer is deeply appreciated. I searched the web but I found nothing useful for my task.
I found eventually what was missing. The line in which I calculated the parameter d should be
param d {i in D, j in D} = sqrt((x1[i]-x1[j])^2 + (x2[i]-x2[j])^2);
Retrospectively it's clear that the subscripts i and j should have been mentioned on the line, I don't know how I could miss that.

Unrestricted Grammar: { sss | s exists in {a,b}* }

What would be a way to construct an unrestricted grammar for sss? I know that in order to construct ss, you need to construct ss^r and then re-reverse the second string, but how would that be done for sss?
There may be a few bugs in this but the idea should get you on the right track.
// section 1
S := LTR
T := ATWX | BTYZ | N
// section 2
WW' := W'W
WY' := Y'W
YW' := W'Y
YY' := Y'Y
// section 3
WX' := W'X'
WZ' := W'Z'
YX' := Y'X'
YZ' := Y'Z'
// section 4
XW' := W'X
XX' := X'X
XY' := Y'X
XZ' := Z'X
ZW' := W'Z
ZX' := X'Z
ZY' := Y'Z
ZZ' := Z'Z
// section 5
XR := X'R
ZR := Z'R
// section 6
NW' := W'N
NX' := X'N
NY' := Y'N
NZ' := Z'N
NR := Q
// section 7
AQ := Qa
W'Q := Qa
X'Q := Qa
BQ := Qb
Y'Q := Qb
Z'Q := Qb
LQ := e
Section 1 sets out the basic scheme. We're going to mark the beginning and end of our working space with L and R to be perfectly explicit. T is our working space and L and R are the left and right markers. Our working space can add either three as (these are what A, W, and X will become later) or add three bs (these are what B, Y and Z will become later) or it can quit by generating the special nonterminal N. N is going to be what turns our string of nonterminals into a string of terminals, as we'll see later.
So we need xxx for x in (a+b)*. A and B get added to the end of the first x because of the way we set up the productions. The job of section 2 is to make sure that the W and Y get added to the end of the second x. Unfortunately, our productions put the W and Y at the beginning of the second x so, without correction, we'd end up with xx^R. Section 2 corrects this problem by "floating" the W and Y to the right as long as they are to the left of a W or Y that has already found its proper place. Ws and Ys can't pass each other and so this guarantees they will end up in the proper sequence once the music stops.
Section 3 shows how the symbols in the second occurrence of x find their correct positions. Basically, they move to the right (based on section 2) until they find symbols of the third x that are in their proper places. Since the symbols of the second x have their proper place before the symbols of the third x, the last possible right before either symbol of the third x (X or Z) is the right position for every symbol of the second x. So we float right all the way to the end of the second x.
We repeat the process for the third x in section 4, although we have to let these symbols (X and Z) float past all the symbols from the second or third x that have already found their proper places. These symbols can't pass themselves and so by the same logic as we saw in section 2 we'll get the symbols in the correct sequence.
Section 5 says when to stop floating symbols that make up the third x to the right. Remember the end of working space marker R? The last symbol of the third x occurs all the way at the right, right before R. So that's when we know we have found the proper place for the symbol.
Section 6 begins the process of finishing off our derivation. We have a lovely string of nonterminals and we want to turn these into terminals. The first thing we do is float N to the right past the well-placed symbols of the second and third xs all the way to the end of string marker R. We don't need R for anything, so we transform N into Q. As we'll see in the next section Q is what makes the magic happen.
Section 7 outlines how Q floats left back through the entire string of nonterminals that have found their proper place all the way to L, changing the nonterminals to the proper terminal along the way. Once it finds L, it obliterates both nonterminals leaving only a string of terminal symbols in its wake. The derivation is complete.
To illustrate the process, let's generate aabaabaab.
S
-> LTR
-> LATWXR
-> LAATWXWXR
-> LAABTYZWXWXR
-> LAABNYZWXWXR
-> LAABNYZWXWX'R
-> LAABNYZWXW'X'R
-> LAABNYZWW'XX'R
-> LAABNYZWW'X'XR
-> LAABNYZWW'X'X'R
-> LAABNYZW'WX'X'R
-> LAABNYZW'W'X'X'R
-> LAABNYW'ZW'X'X'R
-> LAABNYW'W'ZX'X'R
-> LAABNYW'W'X'ZX'R
-> LAABNYW'W'X'X'ZR
-> LAABNYW'W'X'X'Z'R
-> LAABNW'YW'X'X'Z'R
-> LAABNW'W'YX'X'Z'R
-> LAABNW'W'Y'X'X'Z'R
-> LAABW'NW'Y'X'X'Z'R
-> LAABW'W'NY'X'X'Z'R
-> LAABW'W'Y'NX'X'Z'R
-> LAABW'W'Y'X'NX'Z'R
-> LAABW'W'Y'X'X'NZ'R
-> LAABW'W'Y'X'X'Z'NR
-> LAABW'W'Y'X'X'Z'Q
-> LAABW'W'Y'X'X'Qb
-> LAABW'W'Y'X'Qab
-> LAABW'W'Y'Qaab
-> LAABW'W'Qbaab
-> LAABW'Qabaab
-> LAABQaabaab
-> LAAQbaabaab
-> LAQabaabaab
-> LQaabaabaab
-> aabaabaab

AMPL: Subscript out of bound

Hello fellow optimizers!
I'm having some issues with the following constraint:
#The supply at node i equals what was present at the last time period + any new supply and subtracted by what has been extracted from the node.
subject to Constraint1 {i in I, t in T, c in C}:
l[i,t-1,c] + splus[i,t] - sum{j in J, v in V, a in A} x[i,j,v,t,c,a]= l[i,t,c];
which naturally causes this constraint to provide errors the first time it loops, as the t-1 is not defined (for me, l[i,0,c] is not defined. Where
var l{I,T,C} >= 0; # Supply at supply node I in time period T for company C.
param splus{I,T}; # Additional supply at i.
var x{N,N,V,T,C,A} integer >= 0; #Flow from some origin within N to a destination within N using vehicle V, in time T, for company C and product A.
and set T; (in the .mod) is a set defined as:
set T := 1 2 3 4 5 6 7; in the .dat file
I've tried to do:
subject to Constraint1 {i in I, t in T: t >= 2, c in C}:
all else same
which got me a syntax error. I've also tried to include "let l[1,0,1] := 0" for all possible combinations, which got me the error
error processing var l[...]:
no data for set I
I've also tried
subject to Constraint1 {i in I, t in T, p in TT: p>t, c in C}:
l[i,t,c] + splus[i,p] - sum{j in J, v in V, a in A} x[i,j,v,p,c,a]= l[i,p,c];
where
set TT := 2 3 4 5 6;
in the .dat file (and merely set TT; in the .mod) which also gave errors. Does someone have any idea of how to do this?
One way to fix this is to specify the condition t >= 2 at the end of the indexing expression:
subject to Constraint1 {i in I, t in T, c in C: t >= 2}:
...
See also Section A.3 Indexing expressions and subscripts for more details on syntax of indexing expressions.

Displaying decimals as fractions using SQL

I need to write a SELECT query for some users that will return a ratio. The math involved would just be the simple division of two numbers. However, they would like to have the ratio presented as a fraction rather than as a decimal. What is the best way to do that in SQL using Oracle 10g?
You can define a function in Oracle to calculate greatest common divisor (taken from here):
CREATE FUNCTION gcd (x INTEGER, y INTEGER) RETURN INTEGER AS
ans INTEGER;
BEGIN
IF (y <= x) AND (x MOD y = 0) THEN
ans := y;
ELSIF x < y THEN
ans := gcd(y, x); -- Recursive call
ELSE
ans := gcd(y, x MOD y); -- Recursive call
END IF;
RETURN ans;
END;
And then use that function in your SQL to concatenate a fraction string:
SELECT CAST(TRUNC(A / GCD(A, B)) AS VARCHAR2(10))
|| ' / ' ||
CAST(TRUNC(B / GCD(A, B)) AS VARCHAR2(10)) AS FRACTION FROM TABLE

Sequence points in a language with left to right evaluation order?

When evaluation order is specified as "left to right" and language is a (pseudo) C-like, which are the sequence points in the following examples?
int x = 1;
int z = x-- + x; // z = 1 + 0 or z = 1 + 1?
my_func(x++); // x incremented before or after my_func execution?
my_func(x++ + --x); // combining those above
A sequence point is what the language standard defines to be a sequence point. The answers I'm about to give apply to C, but another "C-like" language might very well define different sequence points and thus have different answers to those questions.
int z = x-- + x; // z = 1 + 0 or z = 1 + 1?
Since + is not a sequence point in C, the result of the above statement is undefined.
my_func(x++); // x incremented before or after my_func execution?
x is incremented before my_func runs, but my_func is called with the old value of x as an argument.
my_func(x++ + --x); // combining those above
Undefined for the same reason as the first one.