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

7 views (last 30 days)
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);

Answers (3)

Walter Roberson
Walter Roberson on 11 Oct 2024
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 on 11 Oct 2024
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 on 11 Oct 2024
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.
.

Categories

Find more on Get Started with Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!