The 'diff()' function is running incorrectly in this program (Error using diff Difference order N must be a positive integer scalar)
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
T_stop=10;
T_sample = 0.002;
x0 = [0;0;1/2*pi;0];
options=odeset('RelTol',1.0e-6,'AbsTol',1.0e-6,'BDF','on');
[t,x]=ode45(@(t,x) RIP_Sysm(t,x),[0:T_sample:T_stop],x0,options); % ode45解常微分方程
figure(1)
plot(t,x(:,1));title('时间-滑块A角度');
xlabel('t[s]');ylabel('\theta_A[rad]')
figure(2)
plot(t,x(:,3));title('时间-滑块D角度');
xlabel('t[s]');ylabel('\theta_D[rad]')
function dx = RIP_Sysm(t,x)
%% Constants
ml = 15;
Rp = 42.31;
dl = 127;
H0 = 124.8;
R = 124.24;
L = 147.72;
d = 9.4;
alfa = 12.83*pi/180;
Ix = 1000;
Iy = 1000;
Iz = 1000;
J = 10;
A(x) = (dl^2 + H0^2 + Rp^2 + R^2 - L^2 - 2 * dl * R * sin((x(1) - x(3))/2)) / (2 * Rp);
B(x) = (R^2 * (cos((x(1) - x(3))/2))^2 + H0^2)^0.5;
C(x) = R * cos((x(1) - x(3)) / 2);
b(x) = acos( C / B ) + alfa - acos( A / B );
a(x) = (x(1) - x(3)) / 2;
db_A = diff(b(x),x(1));
db_D = diff(b,x(3));
da_A = diff(a,x(1));
da_D = diff(a,x(3));
0 commentaires
Réponses (2)
VBBV
le 12 Avr 2023
T_stop=10;
T_sample = 0.002;
x0 = [0;0;1/2*pi;0];
options=odeset('RelTol',1.0e-6,'AbsTol',1.0e-6,'BDF','on');
[t,x]=ode45(@(t,x) RIP_Sysm(t,x),[0:T_sample:T_stop],x0,options); % ode45解常微分方程
figure(1)
plot(t,x(:,1));title('时间-滑块A角度');
xlabel('t[s]');ylabel('\theta_A[rad]')
figure(2)
plot(t,x(:,3));title('时间-滑块D角度');
xlabel('t[s]');ylabel('\theta_D[rad]')
function dx = RIP_Sysm(t,x)
%% Constants
ml = 15;
Rp = 42.31;
dl = 127;
H0 = 124.8;
R = 124.24;
L = 147.72;
d = 9.4;
alfa = 12.83*pi/180;
Ix = 1000;
Iy = 1000;
Iz = 1000;
J = 10;
A = (dl^2 + H0^2 + Rp^2 + R^2 - L^2 - 2 * dl * R * sin((x(1) - x(3))/2)) / (2 * Rp);
B = (R^2 * (cos((x(1) - x(3))/2))^2 + H0^2)^0.5;
C = R * cos((x(1) - x(3)) / 2);
b = real(acos( C ./ B ) + alfa - acos( A ./ B ));
a = (x(1) - x(3)) / 2;
db_A = (b-x(1));
db_D = (b-x(3));
da_A = (a-x(1));
da_D = (a-x(3));
dx = [db_A;db_D; da_A;da_D];
end
3 commentaires
VBBV
le 12 Avr 2023
Ok. Then as Walter suggested you need to declare a symbolic parameter that can be used in diff function.
Walter Roberson
le 12 Avr 2023
There are two major diff() functions.
You thought you were using calculus derivative, diff . However, that diff() function only applies if at least one of the parameters passed to diff() is symbolic or symfun
The diff() that applies in most cases where parameters are not symbolic, is diff which is approximately diff(x) = x(2:end) - x(1:end) . When you use that diff() function, a non-empty second parameter must be a positive integer scalar indicating the number of times that the subtraction operator is to be repeated.
The values you are passing in to diff() are purely numeric, so you are getting the successive-differences version of diff()
I suggest that you rewrite your code to create your equations using the Symbolic Toolbox, and then you follow the workflow shown in first example for odeFunction in order to generate a function handle that can be used with ode45()
0 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

