How to create a vector whose elements are functions of a variable?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Grigorios Chatziandreou
am 29 Mär. 2022
Kommentiert: Star Strider
am 29 Mär. 2022
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);
0 Kommentare
Akzeptierte Antwort
Star Strider
am 29 Mär. 2022
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
Result = Tmd(dphi)
.
2 Kommentare
Star Strider
am 29 Mär. 2022
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_COSINE
Since the functions are not functions of ‘k’ they simply repeat the same values in each row.
.
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!