Summing multiple variables in a function
10 views (last 30 days)
Show older comments
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 Comment
Jan
on 28 Feb 2016
a1, a2, ... are not defined, as well as l, r, dv etc. You should get correpconding error message, when you run the code.
Answers (1)
Cam Salzberger
on 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
0 Comments
See Also
Categories
Find more on Function Creation 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!