How to use VARARGIN with two types of input?

11 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2024-1-4
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.

请先登录,再进行评论。

采纳的回答

Hassaan
Hassaan 2024-1-4
编辑:Hassaan 2024-1-4
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 个评论
Stephen23
Stephen23 2024-1-4
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 2024-1-4
@Stephen23 I agree vectorization is much better. My purpose was to provide one of the many possible ways to tackle the solution.

请先登录,再进行评论。

更多回答(1 个)

Dyuman Joshi
Dyuman Joshi 2024-1-4
编辑:Dyuman Joshi 2024-1-4
此 个回答 已被 Dyuman Joshi 标记
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

类别

Help CenterFile Exchange 中查找有关 Engines & Motors 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by