Summing multiple variables in a function

1 回表示 (過去 30 日間)
SiongTeck Ng
SiongTeck Ng 2016 年 2 月 28 日
回答済み: Cam Salzberger 2016 年 3 月 1 日
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
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
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

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by