Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Partial differentiation

Subject: Partial differentiation

From: Dean

Date: 16 Dec, 2010 01:52:07

Message: 1 of 5

Hi,

I am struggling a bit with what I guess should be a simple problem. I have a function F(x), R^3-->R, where x is a 3-element symbolic vector [x1 x2 x3]. I need to obtain the gradient vector of F with respect to x, which involves calculating the partial derivative of F with respect to each of x1, x2, and x3.

Now x1,x2 and x3 represent real positive variables, and they are subject to the constraint x1+x2+x3=1. So I believe that the partial derivative of xi wrt xj, where j is not equal to i, should be -1. Unfortunately I don't know how to specify this in MATLAB. I find I must write the constraint as x1=1-x2-x3. I can then obtain diff(x1,x2)=diff(x1,x3)=-1, as I hoped. However, MATLAB does not seem to evaluate any of the other partial derivatives, e.g. diff(x2,x1) = diff(x2,1 - x3 - x2), NOT -1. I don't know how to specify in MATLAB that all such partial derivates equal -1. The following is a few lines of what I've tried in MATLAB and the errors encountered. Any assistance would be greatly appreciated.

>> syms x1 x2 x3 real
>> x1 + x2 + x3 =1
??? x1 + x2 + x3 =1
                 |
Error: The expression to the left of the equals sign is not a valid target for an
assignment.

>> x1 = 1 - (x2 + x3)
 
x1 =
 
1 - x3 - x2
 
>> diff(x2,x1)
 
ans =
 
diff(x2, 1 - x3 - x2)
 
>> diff(x1,x2)
 
ans =
 
-1
 
>> diff(x1,x3)
 
ans =
 
-1
 
>> x2=1-(x1+x3)
 
x2 =
 
x2
 
>> diff(x2,x1)
 
ans =
 
diff(x2, 1 - x3 - x2)


Thanks for your time.

Dean

Subject: Partial differentiation

From: Roger Stafford

Date: 16 Dec, 2010 05:22:04

Message: 2 of 5

"Dean " <dchesterfield@hotmail.com> wrote in message <iebrc7$17f$1@fred.mathworks.com>...
> Hi,
>
> I am struggling a bit with what I guess should be a simple problem. I have a function F(x), R^3-->R, where x is a 3-element symbolic vector [x1 x2 x3]. I need to obtain the gradient vector of F with respect to x, which involves calculating the partial derivative of F with respect to each of x1, x2, and x3.
>
> Now x1,x2 and x3 represent real positive variables, and they are subject to the constraint x1+x2+x3=1. So I believe that the partial derivative of xi wrt xj, where j is not equal to i, should be -1. Unfortunately I don't know how to specify this in MATLAB. I find I must write the constraint as x1=1-x2-x3. I can then obtain diff(x1,x2)=diff(x1,x3)=-1, as I hoped. However, MATLAB does not seem to evaluate any of the other partial derivatives, e.g. diff(x2,x1) = diff(x2,1 - x3 - x2), NOT -1. I don't know how to specify in MATLAB that all such partial derivates equal -1. The following is a few lines of what I've tried in MATLAB and the errors encountered. Any assistance would be greatly appreciated.
> ......
- - - - - - - - - - - -
  You have written "F(x), R^3-->R" which I was taught to interpret as "F maps (all of) R^3 space into R". Yet you have also said that the coordinates are restricted by x1+x2+x3=1 and x1>0, x2>0, x3>0. As it stands those two statements do not make good mathematical sense together. If you truly have a mapping from all of R^3 into R, then the three partial derivatives have a meaning without reference to the above constraints. If F only maps a subset plane of R^3 into R satisfying the constraints, then the three independent partial derivatives with respect to x1, x2, and x3 have no joint meaning.

  The plane is a two-dimensional space and can have two independent variables defining position in that plane. It is valid to take partial derivatives with respect to such a pair of variables. For example you could let x1 and x2 be the two independent variables with the understanding that x3 is always equal to 1-x1-x2. However the two partial derivatives are by no means the same as those of the corresponding partials for the whole R^3 space. For example the gradient of F using two such variables would always point parallel to the plane, whereas the gradient of F in the full R^3 space in general has no such restriction.

  I think you need to straighten out these concepts in your own mind before any useful help can be forthcoming from this group. Do you actually have a mapping of the entire R^3 into R or not? If you do, what two variables would you choose to represent position in the R^2 plane? Do they give distances between pairs of points equal to the corresponding distances in the R^3 x1-x2-x3 space? (The above example of using the x1 and x2 pair would not satisfy this last.)

Roger Stafford

Subject: Partial differentiation

From: Roger Stafford

Date: 16 Dec, 2010 20:21:05

Message: 3 of 5

"Dean " <dchesterfield@hotmail.com> wrote in message <iebrc7$17f$1@fred.mathworks.com>...
> I am struggling a bit with what I guess should be a simple problem. I have a function F(x), R^3-->R, where x is a 3-element symbolic vector [x1 x2 x3]. I need to obtain the gradient vector of F with respect to x, which involves calculating the partial derivative of F with respect to each of x1, x2, and x3.
>
> Now x1,x2 and x3 represent real positive variables, and they are subject to the constraint x1+x2+x3=1. ........
- - - - - - - - - - - -
  To expand upon what I wrote yesterday, suppose that your F is defined over three-dimensional space but that you are interested in some kind of gradient defined on the plane x+y+z=1. (I switched to x, y, and z for brevity instead of your x1, x2, x3.)

  It is advantageous to define coordinates u and v in this plane that preserve the Euclidean metric. That is, if the points (u1,v1) and (u2,v2) in plane coordinates correspond respectively to (x1,y1,z1) and (x2,y2,z2) in the x,y,z coordinate system, then

 ((u2-u1)^2+(v2-v1)^2)^(1/2) = ((x2-x1)^2+(y2-y1)^2+(z2-z1)^2)^(1/2)

always holds.

  One definition of u and v coordinates that satisfies this equality is

 x = 1/3+1/sqrt(6)*2*u
 y = 1/3-1/sqrt(6)*(u-sqrt(3)*v)
 z = 1/3-1/sqrt(6)*(u+sqrt(3)*v)

It is easy to check that x+y+z=1 is identically true for all u and v, so this is a valid parametric representation of points on your plane. A little more work can show that the Euclidean metric is preserved.

  Taking advantage of the identities

 dF/du = dF/dx*dx/du + dF/dy*dy/du + dF/dz*dz/du
 dF/dv = dF/dx*dx/dv + dF/dy*dy/dv + dF/dz*dz/dv

you can show that the 2D gradient [dF/du,dF/dv] simply amounts to the orthogonal projection of the 3D gradient [dF/dx,dF/dy,dF/dz] onto the plane.

  In the above, the origin of the u, v coordinates was located at x,y,z coordinates (1/3,1/3,1/3). This origin could actually have been placed anywhere on the plane. The u and v axes could also have been rotated around the plane normal by any angle provided they remain mutually orthogonal and you would still have a coordinate system that would relate the two different gradients in the above relation: the u,v 2D gradient being the projection of the x,y,z 3D gradient onto the plane.

  With plane coordinates that do not satisfy this metric-preserving condition, the relationship between the 2D (plane) and the 3D gradients would be more complicated.

  I hope this serves to clarify the situation of the gradient concept when defined in terms of plane surface coordinates.

Roger Stafford

Subject: Partial differentiation

From: Dean

Date: 17 Dec, 2010 02:19:07

Message: 4 of 5

Thanks for your feedback Roger.

You are correct, I did make an error in stating "F(x), R^3-->R", as F does not map all of R^3 to R, just the subset of (x,y,z) that satisfies the constraints x+y+z=1 and x>=0, y>=0, z>=0. The x, y and z in my case represent mole fractions in a liquid, hence the constraints.

I understand that because of the constraints, the domain of valid coordinates is a two-dimensional plane, and therefore only two independent variables are required to define position in the plane. In my case, I just use x and y to define position, and when in future I work out Grad(F), I will just use the first two elements, ie. 2D gradient given by (dF/dx,dF/dy).

My main problem lay in the actual evaluation of dF/dx and dF/dy. F is a function in x, y and z. So in the process of evaluating the partial derivative of F with respect to x, say, I come across a term in F which contains y or z, and am faced with the question of "What does dy/dx and dz/dx equal?". Do they equal 0, or because of the constraint x+y+z=1, do they equal -1?

I appreciate the time you have taken to reply to my post.

Dean

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <iedsbg$j7r$1@fred.mathworks.com>...
> "Dean " <dchesterfield@hotmail.com> wrote in message <iebrc7$17f$1@fred.mathworks.com>...
> > I am struggling a bit with what I guess should be a simple problem. I have a function F(x), R^3-->R, where x is a 3-element symbolic vector [x1 x2 x3]. I need to obtain the gradient vector of F with respect to x, which involves calculating the partial derivative of F with respect to each of x1, x2, and x3.
> >
> > Now x1,x2 and x3 represent real positive variables, and they are subject to the constraint x1+x2+x3=1. ........
> - - - - - - - - - - - -
> To expand upon what I wrote yesterday, suppose that your F is defined over three-dimensional space but that you are interested in some kind of gradient defined on the plane x+y+z=1. (I switched to x, y, and z for brevity instead of your x1, x2, x3.)
>
> It is advantageous to define coordinates u and v in this plane that preserve the Euclidean metric. That is, if the points (u1,v1) and (u2,v2) in plane coordinates correspond respectively to (x1,y1,z1) and (x2,y2,z2) in the x,y,z coordinate system, then
>
> ((u2-u1)^2+(v2-v1)^2)^(1/2) = ((x2-x1)^2+(y2-y1)^2+(z2-z1)^2)^(1/2)
>
> always holds.
>
> One definition of u and v coordinates that satisfies this equality is
>
> x = 1/3+1/sqrt(6)*2*u
> y = 1/3-1/sqrt(6)*(u-sqrt(3)*v)
> z = 1/3-1/sqrt(6)*(u+sqrt(3)*v)
>
> It is easy to check that x+y+z=1 is identically true for all u and v, so this is a valid parametric representation of points on your plane. A little more work can show that the Euclidean metric is preserved.
>
> Taking advantage of the identities
>
> dF/du = dF/dx*dx/du + dF/dy*dy/du + dF/dz*dz/du
> dF/dv = dF/dx*dx/dv + dF/dy*dy/dv + dF/dz*dz/dv
>
> you can show that the 2D gradient [dF/du,dF/dv] simply amounts to the orthogonal projection of the 3D gradient [dF/dx,dF/dy,dF/dz] onto the plane.
>
> In the above, the origin of the u, v coordinates was located at x,y,z coordinates (1/3,1/3,1/3). This origin could actually have been placed anywhere on the plane. The u and v axes could also have been rotated around the plane normal by any angle provided they remain mutually orthogonal and you would still have a coordinate system that would relate the two different gradients in the above relation: the u,v 2D gradient being the projection of the x,y,z 3D gradient onto the plane.
>
> With plane coordinates that do not satisfy this metric-preserving condition, the relationship between the 2D (plane) and the 3D gradients would be more complicated.
>
> I hope this serves to clarify the situation of the gradient concept when defined in terms of plane surface coordinates.
>
> Roger Stafford

Subject: Partial differentiation

From: Roger Stafford

Date: 17 Dec, 2010 04:19:20

Message: 5 of 5

"Dean " <dchesterfield@hotmail.com> wrote in message <ieehar$md1$1@fred.mathworks.com>...
> Thanks for your feedback Roger.
>
> You are correct, I did make an error in stating "F(x), R^3-->R", as F does not map all of R^3 to R, just the subset of (x,y,z) that satisfies the constraints x+y+z=1 and x>=0, y>=0, z>=0. The x, y and z in my case represent mole fractions in a liquid, hence the constraints.
>
> I understand that because of the constraints, the domain of valid coordinates is a two-dimensional plane, and therefore only two independent variables are required to define position in the plane. In my case, I just use x and y to define position, and when in future I work out Grad(F), I will just use the first two elements, ie. 2D gradient given by (dF/dx,dF/dy).
>
> My main problem lay in the actual evaluation of dF/dx and dF/dy. F is a function in x, y and z. So in the process of evaluating the partial derivative of F with respect to x, say, I come across a term in F which contains y or z, and am faced with the question of "What does dy/dx and dz/dx equal?". Do they equal 0, or because of the constraint x+y+z=1, do they equal -1?
>
> I appreciate the time you have taken to reply to my post.
>
> Dean
- - - - - - - - - - -
  Your statement that "F is a function in x, y and z" would seem to hint that you might actually have a relationship between F and the quantities x, y, and z that makes sense even when their sum is not restricted to 1. If that is the case, you might be able to make a sensible evaluation of the 3D gradient [dF/dx,dF/dy,dF/dz] which temporarily ignores that constraint.

  The main point of what I was trying to tell you earlier is that however you define a planar set of coordinates, if they preserve the metric of the original x,y,z metric, then the 2D gradient using those coordinates will always be just the orthogonal projection of the 3D gradient. In other words you don't need to invent a set of planar coordinates and take their partial derivatives to get such a 2D gradient. Just project the known 3D gradient orthogonally onto the plane. It is a natural way to obtain a 2D gradient from a 3D gradient.

  For your constraint this is particularly easy to do. If your 3D gradient is [Fx,Fy,Fz], then the projected 2D gradient is simply

 [(2*Fx-Fy-Fz)/3,(2*Fy-Fz-Fx)/3,(2*Fz-Fx-Fy)/3] (1)

It is easy to show that this is a vector parallel to the plane since the sum of its components is zero, which is to say that it is orthogonal to the plane's particular normal direction.

  Formula (1) comes from the general formula for orthogonal projection of a vector onto a plane:

 Vp = V - dot(N,V)*N (2)

where N is a unit normal vector to the plane, V is the vector to be projected onto the plane, and Vp is its projection within the plane. In your case V = [Fx,Fy,Fz] and N = [1/sqrt(3),1/sqrt(3),1/sqrt(3)] which quickly lead to formula (1).

  It seems to me that arbitrarily selecting x and y as your independent variables and restricting z to 1-x-y might tend to bias the resulting gradient by that particular molar fraction selection. Choosing a different pair of fractions could give different results.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us