Summing multiple variables in a function
10 次查看(过去 30 天)
显示 更早的评论
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 个评论
Jan
2016-2-28
a1, a2, ... are not defined, as well as l, r, dv etc. You should get correpconding error message, when you run the code.
回答(1 个)
Cam Salzberger
2016-3-1
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!