How to use VARARGIN with two types of input?

11 visualizaciones (últimos 30 días)
Joseph
Joseph el 4 de En. de 2024
Editada: Hassaan el 4 de En. de 2024
I want to preface this by saying I'm a MechEng student & MatLab beginner, so I hope I've explained my problem clearly
I'm trying to create a function that calculates the equivalent rotational inertia of a gearbox for any number of shafts. The mathematical operation would be as follows:
((Rotational Inertia 1)*(Angular Velocity 1)^2 + (Rotational Inertia 2)*(Angular Velocity 2)^2 ...) up to N
How would I go about defining the function input? I've looked into varargin but I'm not sure how I would seperate between Rotational inertia inputs and angular velocities. Let me know if I need to clarify anything
  1 comentario
Stephen23
Stephen23 el 4 de En. de 2024
The name MATLAB comes from "MATrix LABoratory". Because its magic power lies in using vectors, matrices, and arrays.
Whenever you find yourself writing pseudo-indices on the end of variable names like this:
+((Rotational Inertia 1)*(Angular Velocity 1)^2 + (Rotational Inertia 2)*(Angular Velocity 2)^2 ...) up to N
% ^ ^ ^ ^
that is a big sign that should be thinking in terms of vectors, matrices, and arrays, to use MATLAB effectively.

Iniciar sesión para comentar.

Respuesta aceptada

Hassaan
Hassaan el 4 de En. de 2024
Editada: Hassaan el 4 de En. de 2024
Example 1: Without using varargin
One array will be for the rotational inertias and the other for the corresponding angular velocities. Here's the function with some dummy values:
% I have called this function with dummy values for inertias and velocities
% Assume inertias for three shafts
inertias = [0.5, 1.2, 0.3]; % in kg*m^2
% Assume angular velocities for three shafts
velocities = [100, 150, 200]; % in rad/s
% Call the function
J_eq = equivalentRotationalInertia(inertias, velocities);
% Display the result
disp(['The equivalent rotational inertia is ', num2str(J_eq), ' kg*m^2']);
The equivalent rotational inertia is 44000 kg*m^2
function J_eq = equivalentRotationalInertia(inertias, velocities)
% Check if the number of inertias and velocities are the same
if length(inertias) ~= length(velocities)
error('The number of inertias must be equal to the number of velocities.');
end
% Calculate the equivalent rotational inertia
J_eq = sum(inertias .* velocities.^2);
end
In this example, inertias contains three dummy inertia values and velocities contains three dummy angular velocity values. These are passed to the equivalentRotationalInertia function to calculate the equivalent rotational inertia of the gearbox.
Example 2: With using varargin
Using varargin that can accept an arbitrary number of inertia and velocity pairs and calculate the equivalent rotational inertia.I will be use some dummy values as inputs when calling the function
% I have called this function with dummy values for inertias and velocities
% Assume dummy values for three pairs
I1 = 0.5; % in kg*m^2
w1 = 100; % in rad/s
I2 = 1.2; % in kg*m^2
w2 = 150; % in rad/s
I3 = 0.3; % in kg*m^2
w3 = 200; % in rad/s
% Call the function
J_eq_varargin = equivalentRotationalInertiaVarargin(I1, w1, I2, w2, I3, w3);
% Display the result
disp(['The equivalent rotational inertia using varargin is ', num2str(J_eq_varargin), ' kg*m^2']);
The equivalent rotational inertia using varargin is 44000 kg*m^2
function J_eq = equivalentRotationalInertiaVarargin(varargin)
% Check if the number of arguments is even
if mod(nargin, 2) ~= 0
error('Inputs must be in pairs of inertia and velocity.');
end
% Initialize equivalent inertia
J_eq = 0;
% Loop through pairs of arguments
for i = 1:2:length(varargin)
inertia = varargin{i};
velocity = varargin{i + 1};
% Sum up the equivalent rotational inertia
J_eq = J_eq + inertia * velocity^2;
end
end
In this example, I1, w1, I2, w2, I3, w3 are dummy inertia and velocity values. They are passed in pairs to the equivalentRotationalInertiaVarargin function, which loops through each pair, calculates the product of inertia and the square of velocity, and sums them up to give the equivalent rotational inertia.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
  2 comentarios
Stephen23
Stephen23 el 4 de En. de 2024
With the note that storing lots of scalar numeric in a cell array and performing operations on them one-at-a-time is an inefficient use of MATLAB. Using vectors is much better.
Hassaan
Hassaan el 4 de En. de 2024
@Stephen23 I agree vectorization is much better. My purpose was to provide one of the many possible ways to tackle the solution.

Iniciar sesión para comentar.

Más respuestas (1)

Dyuman Joshi
Dyuman Joshi el 4 de En. de 2024
Editada: Dyuman Joshi el 4 de En. de 2024
Dyuman Joshi ha marcado con alerta este/a respuesta
You do not need to use varargin here.
Store both set of values as a vectors of same dimension (i.e. both 1xN or Nx1) and define the function like this -
%Function definition
function out = calculateInertia(Rot_Inertia, Ang_Vel)
out = sum(Rot_Inertia.*Ang_Vel.^2);
end

Categorías

Más información sobre Engines & Motors en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by