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

3 views (last 30 days)
Grigorios Chatziandreou on 29 Mar 2022
Commented: Star Strider on 29 Mar 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.
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 on 29 Mar 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
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 CommentsShowHide 1 older comment
Star Strider on 29 Mar 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_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.
.

R2022a

### Community Treasure Hunt

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

Start Hunting!