Clear Filters
Clear Filters

Taking second partial derivative symbolically with diff()

10 views (last 30 days)
See the attached file to get an outline of what I'm trying to do with my code.
The first part of the code builds the equilibrium function.
The second part of the code takes the partial derivative of this function and populates a matrix with the derivatives.
The third part of the code takes the second partial derivative and populates the matrix with the derivatives.
N = 4; %Sets size of matrices and vectors
w_n = sym('w',[1,N]); %Symbolic vector of N displacement values. All derivatives will be with respet to these values
%Additional symbolic variables
theta_N = sym('t',[1,N]);
h_N = sym('h',[1,N]);
syms L P k;
%Replaces entries in symbolic vectors with symbolic function in terms of
%displacement values
for i = 1:1:N-1
theta_N(i) = asin(w_n(i))-asin((w_n(i+1)-w_n(i))/L);
h_N(i) = sqrt(L^2-(w_n(i+1)-w_n(i)));
%Values are summed
H = sum(h_N);
THETA = sum(theta_N);
%The equilibrium equation is synthesized, and is in terms of [w_n]
V_N = 0.5*k*THETA^2+P*(H);
%Empty first derivative matrices
DV_N1 = sym('DV',[1,N]);
DV_N2 = sym('DV',[1,N]);
syms O
for j = 1:1:N
for k = 1:1:N
DV_N1(j,k) = diff(V_N,w_n(j),w_n(k));
if eq(j,k) == 1
%Replaces matrix diagonal with "0"
DV_N1(j,k) = O;
for m = 1:1:N
%Poplulates matrix diagonal with derivatives
DV_N2(m,m) = diff(V_N,w_n(j));
%Both matrices combined to get complete derivative matrix
DV_N = DV_N1+DV_N2;
%Process is repeated for the second partial derivative
DVV_N1 = sym('DV',[1,N]);
DVV_N2 = sym('DV',[1,N]);
for j = 1:1:N
for k = 1:1:N
DVV_N1(j,k) = diff(V_N,w_n(j),w_n(k),2); %fix this
if eq(j,k) == 1
DVV_N1(j,k) = O;
for m = 1:1:N
DVV_N2(m,m) = diff(V_N,w_n(j),2); %fix this
DVV_N = DVV_N1 + DVV_N2;
%substitutue for w_n as zero here.
equilibrium = zeros(1,N);
S = subs(DVV_N,w_n,equilibrium);
I run into an error on the third part, shown below.
Error using sym/diff (line 71)
Calling DIFF with four or more arguments, all arguments, except for the first, must be variables.
Error in CIE_514_HW4 (line 58)
DVV_N1(j,k) = diff(V_N,w_n(j),w_n(k),2); %fix this

Accepted Answer

Paul on 26 Oct 2021
Edited: Paul on 26 Oct 2021
This line
DV_N1(j,k) = diff(V_N,w_n(j),w_n(k));
doesn't look correct. The third argument to diff() should be n, which tells diff to take the nth derivative of the first argument wrt to the second argument (n defaults to 1 if not specified). So It think that line should be
DV_N1(j,k) = diff(diff(V_N,w_n(j)),w_n(k));
I'm not sure what the line that throws the error is trying to do, so can't say how to correct that, other than that diff() can only take up to three arguments (check doc diff)
As for the rest comparing to the attachment, it looks like the code is taking an extra step. It looks like you're trying to form a single matrix where the diagonal terms are the first derivatives and the off-diagonal terms are the second derivatives, which could be accomplished either with a single loop, or a loop to fill in the off diagonal terms and then another loop to fill in the diagonal, which is what it looks like you're trying to do.

More Answers (0)




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!