Summing multiple variables in a function

1 vue (au cours des 30 derniers jours)
SiongTeck Ng
SiongTeck Ng le 28 Fév 2016
Below is part of the main code, please advise on the summation of the variables within the function
function velocity = v
vl1 = @(a)integral(a1,0,1);
vl2 = @(a)integral(a2,1,2);
vl3 = @(a)integral(a3,2,2+2*pi*l/(r*dv));
vl4 = @(a)integral(a4,2+2*pi*l/(r*dv),7+2*pi*l/(r*dv));
vr1 = @(a)integral(a5,0,1);
vr2 = @(a)integral(a6,1,2);
vr3 = @(a)integral(a7,2,2+2*pi*l/(r*dv));
vr4 = @(a)integral(a8,2+2*pi*l/(r*dv),7+2*pi*l/(r*dv));
vl = symsum(vl1,vl2,vl3,vl4)/4;
vr = symsum(vr1,vr2,vr3,vr4)/4;
velocity = symsum(vl,vr)/2;
end
  1 commentaire
Jan
Jan le 28 Fév 2016
a1, a2, ... are not defined, as well as l, r, dv etc. You should get correpconding error message, when you run the code.

Connectez-vous pour commenter.

Réponses (1)

Cam Salzberger
Cam Salzberger le 1 Mar 2016
Hello Siong,
I am a bit confused about what you are trying to do here. If you are trying to get a numeric value for "vl1" and all the other integration results, then you do not need to define them as a function handle. Additionally, you are defining the function variable to be "a", but then using "a1", "a2", etc. within the expression.
Also, unless "a1", "a2", etc. are function handles, then you are only integrating a constant value. Have you defined "a1", etc. to be function handles elsewhere in the code?
Finally, if you are working with just numeric values, there is no need to use "symsum". You can just use addition:
vl = (vl1+vl2+vl3+vl4)/4;
or even "mean":
vl = mean([vl1 vl2 vl3 vl4]);
I hope that this helps point you in the right direction. If not, give us more detail on what you're trying to do.
-Cam

Catégories

En savoir plus sur Function Creation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by