Evaulating a multiple valued function at multiple points
Show older comments
I have a function that is a family of similar but different functions. An example would be 1*sin(1*x) and 2*sin(2*x). I have computed special points (the minimum) of this family of equations. I would like to be able to evaluate each equation within the family at the special point for that specific equation. When I try, I get a n-by-n sym array, because Matlab evaluates each equation at the special point for each equation, instead of evaluating each equation at the special point of that equation.
In slightly less abstract terms, I'm attempting to calculate the trajectory of a rocket, and I have a family of acceleration curves (in type symfun) that differ only in the time to reach a certain altitude. The function that provides the acceleration family of curves also provides a vector of the time at which the accel is at it's minimum. When I try to evaluate the accel curve family at the (vector valued) minimum accel time, I end up with a 1x6 cell, each of which consists of a 1x6 sym.
Is there some way to call my acceleration function with the (vector valued) min accel time in a single line or command? I've even tried to make an empty vector of the appropriate size and fill in each value individually, but I haven't been able to get anywhere.
Example code:
[a_v, v_v, p_v, t_ascent, t_flip, t_max_decel] = rocketLibrary.getLinearCurves(targetAltitude, a_y_0, true, [0.75 0.85 0.95 1.05 1.15 1.25])
maxDecel = zeros(1, length(t_max_decel))
for i = 1:length(t_max_decel)
maxDecel(1, i) = a_v((double(t_max_decel(i))))(i) % Error on this line, as it can't access
%the ith element of the vector that a_v passes back.
end
Accepted Answer
More Answers (2)
Is there some way to call my acceleration function with the (vector valued) min accel time in a single line or command?
Yes, One way is to have the individual functions be held in cells, e.g.,
syms t
for i=1:6, a_v{i}=symfun(t+i,t); end
t_max_decel=(10:10:60); %times
out = arrayfun(@(f,t)double( f{1}(t) ), a_v , t_max_decel)
2 Comments
Joel McReynolds
on 28 Oct 2023
You could do something like this,
syms t
f(t)= [1,2,3]*sin(t) %a vector valued symfun
a_v=cellfun(@(z)symfun(z,t), sym2cell(f(t)) ,'uni',0); %a cell array of symfuns
a_v{:}
Another way is to make it so that the time variable t is vector-valued as well:
syms t [1,4]
f(t)= t.^(1:4)
Times=3:6; %evaluate at these times
T=num2cell(Times);
f(T{:})
Categories
Find more on Conversion Between Symbolic and Numeric in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!