Why do I have this error when I try to derivative a function with variable x?

1 次查看(过去 30 天)
I dont know why i have this error. Please help me.
This is my code:
function [dX,s,x,o,dU,B] = fcn( nref,dnref,ddnref,e1,de1, u)
lamda1 = [15.6 10.6;10.6 10.4];
s = de1+ lamda1*e1;
g1 = 5;
g2 = 1;
g3 = 1;
n = nref-e1;
dn = dnref+lamda1*e1-s;
x = [e1';s'];
M = [g1+2*g2*cos(n(2)) g3+g2*cos(n(2));g3+g2*cos(n(2)) g3];
C = [-g2*sin(n(2))*dn(2) -g2*sin(n(2))*(dn(1)+dn(2));g2*sin(n(2))*dn(1) 0];
Y = -M^-1*C*s;
A = [-lamda1*e1+s;Y;dnref;ddnref];
b1 = zeros(2,2);
b2 = zeros(4,2);
B = [b1;M^-1;b2];
dX = A+B*u;
U = [x(1)^2;x(1)*x(2);x(2)^2;x(3)^2;x(3)^2*cos(n(2));x(3)*x(4);x(3)*x(4)*cos(n(2));x(4)^2];
o = diff(U,x)*dX;
dU = diff(U,x);
Coder error: Difference order N must be a positive integer scalar in the range 1 to intmax('coder.internal.indexInt') in 'o' function.

采纳的回答

Sulaymon Eshkabilov
Hi,
here you are trying to compute numerical differentiation and thus, you need to use these commands:
o = (diff(U)./diff(x))*dX;
dU = diff(U)./diff(x);
Should you want to get symbolic differentiation, then you'd need introduce symbolic variables, e.g.:
syms U(x)
dU = diff(U(x), x)
ddU = diff(dU, x)
...
  1 个评论
chuyen hoangcao
chuyen hoangcao 2021-6-1
编辑:chuyen hoangcao 2021-6-1
Wow, thank you so much for helping me answer the question. The numerical differentiation is not wrong, but in matlab function block it does not support for symbolic differentiation with syms U(x). Maybe I will use the numerical differentiation.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by