How to extract one element from array in function?

I want to extract one element from array, but it is difficult in function statement.
Please someone tell me the way.
function dtheta = odeFun(t, theta)
g = 9.8;
l = 1;
Trq = randi([10 25],1,1); % Generate random numbers from 10 to 25
disp(Trq(1,1)) ← I wanna extract one element here.
% theta(1) = theta, theta(2) = dtheta
dtheta = zeros(2, 1);
dtheta(1) = theta(2);
dtheta(2) = -g/l*theta(1)-Trq;
end

回答 (1 件)

LO
LO 2021 年 7 月 17 日
編集済み: LO 2021 年 7 月 17 日

0 投票

It is not clear what you want the function to do. What is t? and theta?
If your aim is to apply a fixed transformation of theta and create 2 output variables (dtheta and Trq) you need to specify a "Trq output variable" in the function synthax together with dtheta.
test = odeFun(2,45); % I've tried to test value pairs, in absence of any other hint
function [dtheta,Trq] = odeFun(t, theta)
g = 9.8;
l = 1;
Trq = randi([10 25],1,1); % Generate 1 random number from 10 to 25 (it generates only 1 Nr)
dtheta = zeros(2, 1);
dtheta(1) = theta(1); % I've changed theta's index to 1, it did not work otherwise
dtheta(2) = -g/l*theta(1)-Trq;
end

6 件のコメント

ryunosuke tazawa
ryunosuke tazawa 2021 年 7 月 18 日
編集済み: ryunosuke tazawa 2021 年 7 月 18 日
I made that the torque is a random number from 10 to 25. I want to extract only one element from it. However, in the function, all the generated elements will be extracted.
I want fix the value of Trq.
LO
LO 2021 年 7 月 18 日
Well if you have a random number generated for an array of angles, you'll get an array of torques. If you want one output, just use 1 input. In the example above I get 1 output only. Do you mean to use a single randomly generated torque for an array of angles? Then you probably have to generate the torque out of the function and "feed" it to odefun as an input. In that case the same random torque will be used for all calculations
ryunosuke tazawa
ryunosuke tazawa 2021 年 7 月 18 日
I want to make the torque one value of angle and angular velocity.
As it is, all the elements of the generated torque are assigned to the pendulum states.
Instead, one torque value is needed when solving the equation of motion.
I want one torque value to be randomly selected each time it is executed.
Walter Roberson
Walter Roberson 2021 年 7 月 18 日
You cannot use randomness inside a function you use with any of the ode*() routines -- or at least if you do, then the randomness cannot affect the computation.
LO
LO 2021 年 7 月 18 日
編集済み: LO 2021 年 7 月 18 日
Trq = randi([10 25],1,1); % this is your randomly generated torque
test = odeFun(2,45,Trq); % this is your test measure using the torque value
function [dtheta] = odeFun(t, theta, Trq)
g = 9.8;
l = 1;
dtheta = zeros(2, 1);
dtheta(1) = theta(1);
dtheta(2) = -g/l*theta(1)-Trq;
end
Walter Roberson
Walter Roberson 2021 年 7 月 18 日
Using randomness outside of the odeFun and passing the value in, is something that is entirely valid.
If you were wanting to call this from ode45() then see http://www.mathworks.com/help/matlab/math/parameterizing-functions.html

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2021 年 7 月 17 日

コメント済み:

2021 年 7 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by