Summing multiple variables in a function

조회 수: 4 (최근 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 CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by