Trying to Differentiate Parametric Equation, Error "Error using diff Difference order N must be a positive integer scalar."

조회 수: 4 (최근 30일)
I am trying to differentiate X_Rotor and Y_Rotor with respect to v. When i try to using diff() it give the following error code
"Error using diff
Difference order N must be a positive
integer scalar."
I am also trying to graph the parametric equations, and lines are connecting my points. Not sure why? When i graph across 2*PI this is the figure i get.
When graphing across only the time point i want this is what i get.
Not sure why the straight lines are connecting those points. Anything helps thanks!!
This is my code below:
% Define symbolic variables
syms R Z e v real
% Define parameters
Z = 3; %Lobes
R = 10; % Set the value of R
e = 1.5; % Set the eccentricity value
% Define intervals for v
v1 = linspace(pi/6, pi/2, 100); % First range: pi/6 to pi/2
v2 = linspace(5*pi/6, 7*pi/6, 100); % Second range: 5pi/6 to 7pi/6
v3 = linspace(3*pi/2, 11*pi/6, 100); % Third range: 3pi/2 to 11pi/6
alpha = linspace(0,2*pi,1000);
v = linspace(0, 2*pi,1000);
% Concatenate v
%v = [v1, v2, v3];
% W is defined as to simplify equations
w = 2 * e * sqrt(1 - (Z*e/R)^2 .* sin(Z * v).^2) .* cos(Z * v);
% Parametric equations
X_Rotor = R * cos(2 * v) - (Z*e.^2 / R) .* sin(2 * Z * v) .* sin(2 * v) + w .* cos(2 * v) - 1.5;
Y_Rotor = R * sin(2 * v) + (Z*e.^2 / R) .* sin(2 * Z * v) .* cos(2 * v) + w .* sin(2 * v);
X_Housing = R * cos(alpha) + e * cos(alpha*Z);
Y_Housing = R * sin(alpha) + e * sin(alpha*Z);
% Plot the curve for specified ranges
figure;
plot(X_Rotor, Y_Rotor, 'r', X_Housing, Y_Housing, 'b');
title('Parametric Plot of the Rotor Curve for Specific Intervals');
xlabel('X');
ylabel('Y');
grid on;
axis equal;
% Derivatives of X and Y with respect to v
dXdv = diff(X, v);
dYdv = diff(Y, v);

답변 (3개)

Walter Roberson
Walter Roberson 2024년 10월 11일
syms R Z e v real
You declare v as symbolic
v = linspace(0, 2*pi,1000);
You overwrite v as numeric.

Steven Lord
Steven Lord 2024년 10월 11일
At the start of your code you defined v as a symbolic variable.
% Define symbolic variables
syms R Z e v real
But then later in the code, you redefined it to be a numeric vector.
v = linspace(0, 2*pi,1000);
You then tried to use it as a symbolic variable at the end of the code.
% Derivatives of X and Y with respect to v
dXdv = diff(X, v);
dYdv = diff(Y, v);
But it's not a symbolic variable anymore.
There's another problem that I'm assuming is an artifact of you omitting part of your code for posting to Answers, nowhere does your code define either X or Y. You define variables whose names start with X and Y, but that's different.

Star Strider
Star Strider 2024년 10월 11일
Your diff call is not going to produce the result you want.
% Define symbolic variables
syms R Z e v real
% Define parameters
Z = 3; %Lobes
R = 10; % Set the value of R
e = 1.5; % Set the eccentricity value
% Define intervals for v
v1 = linspace(pi/6, pi/2, 100); % First range: pi/6 to pi/2
v2 = linspace(5*pi/6, 7*pi/6, 100); % Second range: 5pi/6 to 7pi/6
v3 = linspace(3*pi/2, 11*pi/6, 100); % Third range: 3pi/2 to 11pi/6
alpha = linspace(0,2*pi,1000);
v = linspace(0, 2*pi,1000);
% Concatenate v
%v = [v1, v2, v3];
% W is defined as to simplify equations
w = 2 * e * sqrt(1 - (Z*e/R)^2 .* sin(Z * v).^2) .* cos(Z * v);
% Parametric equations
X_Rotor = R * cos(2 * v) - (Z*e.^2 / R) .* sin(2 * Z * v) .* sin(2 * v) + w .* cos(2 * v) - 1.5;
Y_Rotor = R * sin(2 * v) + (Z*e.^2 / R) .* sin(2 * Z * v) .* cos(2 * v) + w .* sin(2 * v);
X_Housing = R * cos(alpha) + e * cos(alpha*Z);
Y_Housing = R * sin(alpha) + e * sin(alpha*Z);
% Plot the curve for specified ranges
figure;
plot(X_Rotor, Y_Rotor, 'r', X_Housing, Y_Housing, 'b');
title('Parametric Plot of the Rotor Curve for Specific Intervals');
xlabel('X');
ylabel('Y');
grid on;
axis equal;
% Derivatives of X and Y with respect to v
% dXdv = diff(X, v);
% dYdv = diff(Y, v);
Since neither ‘X’ or ‘Y’ appear to exist, however ‘v’ is a numeric vector, so I assume that you want to take the numeric derivative. That is most easily done using the gradient funciton, for example:
dXdv = gradient(X, v);
dYdv = gradient(Y, v);
or in earlier versions:
dXdv = gradient(X) ./ gradient(v);
dYdv = gradient(Y) ./ gradient(v);
The second argument to diff must be a scalar if you are calculating the symbolic derivative.
.

카테고리

Help CenterFile Exchange에서 Get Started with Symbolic Math Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by