# How to initialize the values corresponding to the input argument of a function and use it in the following way as mentioned in the code

조회 수: 11 (최근 30일)
Laxmi Akshaya Thela 2021년 9월 5일
답변: Lokesh 2024년 3월 20일
function [x_new, stageCost, unfeas, engTrq, emTrq] = hev(x, u, w, veh, fd, gb, eng, em, batt)
dt = 1; % s
%% Vehicle Model
% Wheels
% Tractive Force (N)
rolling_friction = veh.mass .* veh.gravity .* (veh.first_rrc + veh.second_rrc.*w{1});% how can we initialize the variables by using the input argument veh as mentioned in this equation
vehForce = (wheelSpd~=0) .* (rolling_friction + veh.aero_coeff.*w{1}.^2 + veh.mass.*w{2});
% Wheel torque (Nm)
wheelTrq = (vehForce .* veh.wh_radius + veh.axle_loss .* (wheelSpd~=0));

댓글을 달려면 로그인하십시오.

### 답변 (1개)

Lokesh 2024년 3월 20일
Hi Laxmi,
As per my understanding, you want to know how the variables are initialised using the input argument 'veh' as shown in the equation.
To initialize variables using the input argument 'veh', you essentially use the fields of the 'veh' structure to access specific parameters. The 'veh' argument is expected to be a structure with fields corresponding to various parameters such as 'wh_radius' , 'mass', 'gravity', 'first_rrc', 'second_rrc', 'aero_coeff', and 'axle_loss'.
Assuming the 'veh' structure is correctly passed to your function, it might look something like this:
%Random values are used for demonstration
veh = struct('wh_radius', 0.3, 'mass', 1500, 'gravity', 9.81, ...
'first_rrc', 0.015, 'second_rrc', 0.0005, 'aero_coeff', 0.3, ...
'axle_loss', 50);
To access a field of a structure in MATLAB, you use the dot (.) notation. For example, 'veh.wh_radius' accesses the 'wh_radius' field of the 'veh' structure. Once you have accessed the necessary fields, you can directly use these variables in your equations, such as for rolling friction:
rolling_friction = veh.mass .* veh.gravity .* (veh.first_rrc + veh.second_rrc.*w{1});
In this equation, 'veh.mass', 'veh.gravity', 'veh.first_rrc', and 'veh.second_rrc' are all parameters accessed from the 'veh' structure.
Please refer to the following documentation to know more about 'struct' in MATLAB:

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by