フィルターのクリア

How to create a vector whose elements are functions of a variable?

1 回表示 (過去 30 日間)
Grigorios Chatziandreou
Grigorios Chatziandreou 2022 年 3 月 29 日
コメント済み: Star Strider 2022 年 3 月 29 日
I am working on a project and let me explain the situation quick:
We are testing different motors given a specific torque requirement. For each motor we have specified the characteristic torque-speed curve (a straight line). We are using ode45 dolver for the complex dynamics taking place in our system.
PROBLEM: Imagine the torque-speed line to be having negative gradient, starting from an initial value (stall torque) and as rotational velocity increases, torque decreases. However, once torque becomes zero it has to stay zero (no negative values). ODE45 does not accept piecewise function inputs. Hence, i approximated the line with a fourier series.
PROBLEM 2: I figured out the Fourier expansion, but I cannot make it work in Matlab.
Please help
NOTE: I used n = 1:10 rather than infinity - its good approx
Tmd = motor torque during convertible roof deployment
dphi = angular velocity of motor (rad/s)
Nmmaxd = Maximum (terminal) rotational velocity in rpm (rounds/mnute)
Suppose my function is correct. How can I save the values? The error when running the code says:
for n = 1:10
TmdFOURIER_TERM(n) = @(dphi) (2/(n*pi))*(1-cos(n*pi/3))*cos((n*10/Nmmaxd)*dphi);
end
Tmd = @(dphi) Tm0d/6 + sum(TmdFOURIER_TERM);
Nonscalar arrays of function handles are not allowed; use cell arrays instead.
Error in Mattedit_2 (line 67)
TmdFOURIER_TERM(n) = @(dphi) (2/(n*pi))*(1-cos(n*pi/3))*cos((n*10/Nmmaxd)*dphi);

採用された回答

Star Strider
Star Strider 2022 年 3 月 29 日
This term:
sum(TmdFOURIER_TERM(dphi))
needs to be defined as a function with its argument in order to evaluate it.
Nmmaxd = 42; % Missing, Assume Scalar
Tm0d = 24; % Missing, Assume Scalar
n = 1:10;
TmdFOURIER_TERM = @(dphi) (2./(n*pi)).*(1-cos(n*pi/3)).*cos((n*10./Nmmaxd).*dphi); % Vectorize & Evaluate
Tmd = @(dphi) Tm0d/6 + sum(TmdFOURIER_TERM(dphi));
dphi = randn(size(n)) % Missing, Assume Vector
dphi = 1×10
0.2342 -0.7797 -0.3676 -0.5379 0.7426 -0.0967 -0.2845 0.5260 1.2620 -3.0428
Result = Tmd(dphi)
Result = 5.4523
.
  2 件のコメント
Grigorios Chatziandreou
Grigorios Chatziandreou 2022 年 3 月 29 日
Hello and thank you for the quick reply.
In your suggestion (which makes total sense) you skipped the for-loop. Will that work? How can I save this data and plot them for all motors? (The code i sent was part of the code. There is a big for loop for k=1:10 for 10 different motors. How can you create a matrix and inglude the argyment. Something like:
Tmd(k) = @(dphi) Tm0d/6 + sum(TmdFOURIER_TERM(dphi));
is this possible?
If not, I have ended up doing this (slightly miserable but does the job)
for n = 1:10
TmdFOURIER_TERM(k,n) = (2/(n*pi))*(1-cos(n*pi/3));
TmdFOURIER_COSINE(k,n) = (n*10/Nmmaxd);
end
Tmd = @(dphi) Tm0d/6 + TmdFOURIER_TERM(k,1)*cos(TmdFOURIER_COSINE(k,1)*(dphi)) + TmdFOURIER_TERM(k,2)*cos(TmdFOURIER_COSINE(k,2)*(dphi)) + TmdFOURIER_TERM(k,3)*cos(TmdFOURIER_COSINE(k,3)*(dphi)) + TmdFOURIER_TERM(k,4)*cos(TmdFOURIER_COSINE(k,4)*(dphi)) + TmdFOURIER_TERM(k,5)*cos(TmdFOURIER_COSINE(k,5)*(dphi)) + TmdFOURIER_TERM(k,6)*cos(TmdFOURIER_COSINE(k,6)*(dphi)) + TmdFOURIER_TERM(k,7)*cos(TmdFOURIER_COSINE(k,7)*(dphi)) + TmdFOURIER_TERM(k,8)*cos(TmdFOURIER_COSINE(k,8)*(dphi)) + TmdFOURIER_TERM(k,9)*cos(TmdFOURIER_COSINE(k,9)*(dphi)) + TmdFOURIER_TERM(k,10)*cos(TmdFOURIER_COSINE(k,10)*(dphi));
Star Strider
Star Strider 2022 年 3 月 29 日
My pleasure!
In your suggestion (which makes total sense) you skipped the for-loop. Will that work?
The loop did not appear to be necessary. The vectorization approach is more efficient. (Thank you!)
There is a big for loop for k=1:10 for 10 different motors.
That apparently did not make it into the original question. It is certainly possible to evalute the motors in a loop with the functions that were provided, and put the resullts into a matrix.
These:
for n = 1:10
TmdFOURIER_TERM(k,n) = (2/(n*pi))*(1-cos(n*pi/3));
TmdFOURIER_COSINE(k,n) = (n*10/Nmmaxd);
end
can be made as functions of ‘n’ as well without the ‘n’ loop.
Nmmaxd = 42; % Missing, Assume Scalar
n = 1:10;
for k = 1:10
TmdFOURIER_TERM(k,:) = (2./(n*pi)).*(1-cos(n*pi/3));
TmdFOURIER_COSINE(k,:) = (n*10/Nmmaxd);
end
TmdFOURIER_TERM
TmdFOURIER_TERM = 10×10
0.3183 0.4775 0.4244 0.2387 0.0637 0 0.0455 0.1194 0.1415 0.0955 0.3183 0.4775 0.4244 0.2387 0.0637 0 0.0455 0.1194 0.1415 0.0955 0.3183 0.4775 0.4244 0.2387 0.0637 0 0.0455 0.1194 0.1415 0.0955 0.3183 0.4775 0.4244 0.2387 0.0637 0 0.0455 0.1194 0.1415 0.0955 0.3183 0.4775 0.4244 0.2387 0.0637 0 0.0455 0.1194 0.1415 0.0955 0.3183 0.4775 0.4244 0.2387 0.0637 0 0.0455 0.1194 0.1415 0.0955 0.3183 0.4775 0.4244 0.2387 0.0637 0 0.0455 0.1194 0.1415 0.0955 0.3183 0.4775 0.4244 0.2387 0.0637 0 0.0455 0.1194 0.1415 0.0955 0.3183 0.4775 0.4244 0.2387 0.0637 0 0.0455 0.1194 0.1415 0.0955 0.3183 0.4775 0.4244 0.2387 0.0637 0 0.0455 0.1194 0.1415 0.0955
TmdFOURIER_COSINE
TmdFOURIER_COSINE = 10×10
0.2381 0.4762 0.7143 0.9524 1.1905 1.4286 1.6667 1.9048 2.1429 2.3810 0.2381 0.4762 0.7143 0.9524 1.1905 1.4286 1.6667 1.9048 2.1429 2.3810 0.2381 0.4762 0.7143 0.9524 1.1905 1.4286 1.6667 1.9048 2.1429 2.3810 0.2381 0.4762 0.7143 0.9524 1.1905 1.4286 1.6667 1.9048 2.1429 2.3810 0.2381 0.4762 0.7143 0.9524 1.1905 1.4286 1.6667 1.9048 2.1429 2.3810 0.2381 0.4762 0.7143 0.9524 1.1905 1.4286 1.6667 1.9048 2.1429 2.3810 0.2381 0.4762 0.7143 0.9524 1.1905 1.4286 1.6667 1.9048 2.1429 2.3810 0.2381 0.4762 0.7143 0.9524 1.1905 1.4286 1.6667 1.9048 2.1429 2.3810 0.2381 0.4762 0.7143 0.9524 1.1905 1.4286 1.6667 1.9048 2.1429 2.3810 0.2381 0.4762 0.7143 0.9524 1.1905 1.4286 1.6667 1.9048 2.1429 2.3810
Since the functions are not functions of ‘k’ they simply repeat the same values in each row.
.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by