필터 지우기
필터 지우기

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
Sulaymon Eshkabilov 2021년 5월 31일
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에서 Deployment, Integration, and Supported Hardware에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by