Error: "Difference order N must be a positive integer scalar" when called symbolic gradient function

1 view (last 30 days)
In my program, I first use command jacobian generate a symbolic gradient of my objective function.Then I call the function(gradient.m) and feed it with numeric value. But Matlab warned me like
Warning: Function "diff" is not verified to be a valid MATLAB
function.
Warning: Function "diff" is not verified to be a valid
MATLAB function.
Warning: Function "diff" is not verified to be a valid
MATLAB function.
Error using diff
Difference order N must be a positive integer scalar.
Error in gradient (line 10)
t4 = diff(t2,B1);
Error in mini_dist_noheter (line 60)
dfb = gradient(b(1),b(2),b(3));
The gradient function file, main file and data see the attachment.
  2 Comments
Torsten
Torsten on 5 Dec 2016
Edited: Torsten on 5 Dec 2016
The error message seems quite clear.
In your command
t4 = diff(t2,B1)
B1 is not a positive integer.
Best wishes
Torsten.
Lu zhang
Lu zhang on 5 Dec 2016
Edited: Lu zhang on 5 Dec 2016
But B is a symbolic expression, not a real number. I also upload the generated gradient function this time.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 5 Dec 2016
You appear to have a problem with your MATLAB path. I suggest using
resetdefaultpath
and seeing if that helps.
  5 Comments
Walter Roberson
Walter Roberson on 6 Dec 2016
I did not realize you were using such an old version.
The messages about diff not being verified to be a MATLAB function are coming from your matlabFunction() call applied to the result of jacobian(temp,v) . The jacobian that is generated has a bunch of diff() calls in it, and matlabFunction is warning that converting them into numeric calls might fail. Which indeed happens.
The diff() calls that are being left in are of the form
diff(conj(VARIABLE), VARIABLE)
because it that MATLAB version, the symbolic engine does not know how to take that derivative.
My research suggests that if the variable is permitted to be complex valued then that derivative does not exist; see https://www.quora.com/What-is-the-differentiation-dz%CC%85-dz-of-a-complex-conjugate-by-the-complex-variable-If-it-is-zero-how-does-it-become-so for two proofs.
Why does the jacobian have a bunch of diff(conj(VARIABLE),VARIABLE) calls in it? It is because your variable temp has a bunch of conj(VARIABLE) in it.
Why does your variable temp have a bunch of conj(VARIABLE) in it? It is because in fcn_noheter near the end you have
trans = dif';
and ' is the complex conjugate transpose operation.
You need to ask yourself whether you instead wanted just
trans = dif .';
for the regular transpose. If you do not want just regular transpose, then when you create B1, B2, B3 you need to also
assume(B, 'real')

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!