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

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

The error message seems quite clear.
In your command
t4 = diff(t2,B1)
B1 is not a positive integer.
Best wishes
Torsten.
But B is a symbolic expression, not a real number. I also upload the generated gradient function this time.

댓글을 달려면 로그인하십시오.

 채택된 답변

You appear to have a problem with your MATLAB path. I suggest using
resetdefaultpath
and seeing if that helps.

댓글 수: 5

Thank you for your reply,Walter. However, after I resotredefaultpath (the suggested resetfaultpath seems not work in my 2012a version Matlab) but the same error continues.It's a little bit strange that the code runs well before some small adjustments, mainly due to how I extract the lower triangle part of a matrix and stack it as a vector. But I serious doubt it's the source of this problem.
The generated gradient of a older version of code, which is without any diff function
function dfb = gradient(B1,B2,B3)
%GRADIENT
% DFB = GRADIENT(B1,B2,B3)
% This function was generated by the Symbolic Math Toolbox version 5.8.
% 06-Dec-2016 01:03:01
t2 = B1-1.0;
t3 = B1.*2.0;
t4 = t3-2.0;
t5 = B3.*t4;
t6 = t2.^2;
t7 = B1.*B3.*2.0;
t8 = t5+t7;
t9 = B1.^2;
t10 = t6+t9+1.0;
t13 = B3.*t2;
t14 = B1.*B3;
t11 = B3-t13-t14;
t15 = B1.*t2;
t12 = B1-t15-1.0;
dfb = reshape([t8,-t5,-B3,0.0,0.0,0.0,0.0,0.0,t8,t11,-B3,0.0,0.0,0.0,0.0,t8,t11,-B3,0.0,0.0,0.0,t8,t11,-B3,0.0,0.0,t8,t11,-B3,0.0,t8,t11,-B3,t8,0.0,t8,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0,0.0,0.0,1.0,0.0,1.0,t10,-t6,-B1,0.0,0.0,0.0,0.0,0.0,t10,t12,-B1,0.0,0.0,0.0,0.0,t10,t12,-B1,0.0,0.0,0.0,t10,t12,-B1,0.0,0.0,t10,t12,-B1,0.0,t10,t12,-B1,t10,0.0,t10],[36, 3]);
Ah yes, I keep thinking restoredefaultpath is named resetdefaultpath.
What shows up for
which -all diff
built-in (C:\Program Files\MATLAB\R2012a\toolbox\matlab\datafun\diff)
built-in (C:\Program Files\MATLAB\R2012a\toolbox\matlab\datafun\@uint8\diff) % uint8 method
built-in (C:\Program Files\MATLAB\R2012a\toolbox\matlab\datafun\@uint16\diff) % uint16 method
built-in (C:\Program Files\MATLAB\R2012a\toolbox\matlab\datafun\@uint32\diff) % uint32 method
built-in (C:\Program Files\MATLAB\R2012a\toolbox\matlab\datafun\@uint64\diff) % uint64 method
built-in (C:\Program Files\MATLAB\R2012a\toolbox\matlab\datafun\@int8\diff) % int8 method
built-in (C:\Program Files\MATLAB\R2012a\toolbox\matlab\datafun\@int16\diff) % int16 method
built-in (C:\Program Files\MATLAB\R2012a\toolbox\matlab\datafun\@int32\diff) % int32 method
built-in (C:\Program Files\MATLAB\R2012a\toolbox\matlab\datafun\@int64\diff) % int64 method
built-in (C:\Program Files\MATLAB\R2012a\toolbox\matlab\datafun\@single\diff) % single method
built-in (C:\Program Files\MATLAB\R2012a\toolbox\matlab\datafun\@double\diff) % double method
built-in (C:\Program Files\MATLAB\R2012a\toolbox\matlab\datafun\@logical\diff) % logical method
C:\Program Files\MATLAB\R2012a\toolbox\symbolic\symbolic\@char\diff.m % char method
C:\Program Files\MATLAB\R2012a\toolbox\symbolic\symbolic\@sym\diff.m % sym method
C:\Program Files\MATLAB\R2012a\toolbox\finance\ftseries\@fints\diff.m % fints method
C:\Program Files\MATLAB\R2012a\toolbox\ident\ident\@iddata\diff.m % iddata method
C:\Program Files\MATLAB\R2012a\toolbox\mbc\mbcmodels\@localtruncps\diff.m % localtruncps method
C:\Program Files\MATLAB\R2012a\toolbox\mbc\mbcmodels\@localpspline\diff.m % localpspline method
C:\Program Files\MATLAB\R2012a\toolbox\mbc\mbcmodels\@localpoly\diff.m % localpoly method
C:\Program Files\MATLAB\R2012a\toolbox\robust\robust\@umat\diff.m % umat method
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')
Thank you so much, @Roberson, it works.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2016년 12월 5일

댓글:

2016년 12월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by