Summation of Function Handle Cells
Afficher commentaires plus anciens
Hello.
I am trying to sum up each individual cell that is produced by a function handle in a for loop that runs from.
My handle looks like this:
for a = 1 : 11;
f{a} = @(u)1
for b = setdiff(1:11, a);
f{a} = @(u)1.*G.*mass_a.*mass_b.*r./(norm_r^3)
%need to sum up all 11 cells produced here
end
end
The function handle spits out something like this (I have only put 2 cells, there are 11):
{@(u)1.*G.*mass_a.*mass_b.*r./(norm_r^3)} {@(u)1.*G.*mass_a.*mass_b.*r./(norm_r^3)}
I need to sum all of the cells from above up.
It seems like the sum function on matlab does not work. I have seen a couple of instances of loops being used, but can't seem to be able to apply them correctly.
Thanks
11 commentaires
Geoff Hayes
le 30 Mar 2020
AeroLad - I don't understand why in the outer loop you do
f{a} = @(u)1
and in the inner loop you overwrite this function handle with
f{a} = @(u)1.*G.*mass_a.*mass_b.*r./(norm_r^3)
? Is this intentional? f is a cell array of function handles. At what point do you want to evaluate the function for a some value? Please clarify.
Walter Roberson
le 30 Mar 2020
f{a} = @(u)1
It is usually better to use
f{a} = @(u) ones(size(u))
and likewise for equations such as
f{a} = @(u)1.*G.*mass_a.*mass_b.*r./(norm_r^3)
it is best to multiply by ones(size(u))
It might possibly not matter for your situation, but it is best to get into the habit of this.
The reason it makes a difference is that if u is a non-scalar then @(u)1 returns a scalar rather than being the same size as u, and that is a problem for vectorized use in functions such as integral().
AeroLad
le 30 Mar 2020
James Tursa
le 30 Mar 2020
Why all the function handles? Why not just calculate the forces directly inside your loops and add them up? Simpler and quicker I would think.
AeroLad
le 30 Mar 2020
Walter Roberson
le 30 Mar 2020
You have an existing f variable in your workspace that is not a cell array.
... Using functions helps reduce this kind of problem.
AeroLad
le 30 Mar 2020
James Tursa
le 30 Mar 2020
Modifié(e) : James Tursa
le 30 Mar 2020
@AeroLad: My point is that the way you are currently building your function handles the radius values r_a and r_b are only snapshots of the current r_a and r_b for that iteration. I.e., all of the function handles you are building and summing are only good for one iteration of your calculations. E.g., If you are using this in an iterative integration scheme, you would be forced to completely rebuild all of your function handles from scratch at each step in the iteration process. Is that what you intend? It seems to me that building these function handles doesn't buy you anything. Just calculating the forces directly and summing them would be quicker. But I don't know the overall goal you have.
AeroLad
le 30 Mar 2020
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
