Summing over indices using symsum

조회 수: 1 (최근 30일)
RH
RH 2023년 3월 17일
편집: Torsten 2023년 3월 17일
I am trying to calculate electric field at position due to charges
, at positions .
The equation is .
If I can figure out how to for example just get this sum , I would be able to figure out the equation for the electric field. I tried
u=symunit;
syms k
e0=8.854E-12*(u.s)^4*(u.A)^2/((u.kg)*(u.m)^3);
Q1=10E-9*(u.C);
Q2=15-9*(u.C);
Q3=7E-9*(u.C);
r1=[1 4 2]*1E-3*(u.m);
r2=[-2 0 5]*1E-3*(u.m);
r3=[-1 -2 3]*1E-3*(u.m);
r=[5 5 5]*1E-3*(u.m);
Q=symsum(Qk,k,1,3)
Unrecognized function or variable 'Qk'.
That obviously didn't work. Is there a way to make it work with 'symsum'?
  댓글 수: 5
Walter Roberson
Walter Roberson 2023년 3월 17일
If you have somehow created a list of numbered variables that is too long to make it practical to put them into a list by name, then what you should do is...
Not create a list of numbered variables that is too long to make it practical to put them into a list by name.
Dyuman Joshi
Dyuman Joshi 2023년 3월 17일
편집: Dyuman Joshi 2023년 3월 17일
Note that the sum isn't just over Q, but rk as well
(Without syms and symunit but values according SI units)
e0=8.854E-12;
Q=[10E-9 15E-9 7E-9];
R=[1 4 2;-2 0 5;-1 -2 3]*1E-3;
r=[5 5 5]*1E-3;
r0=vecnorm(r-R,2,2).^3;
out=sum(Q.*(r-R)./(4*pi*e0*r0),1)
out = 1×3
1.0e+06 * 4.3424 3.1998 1.5735

댓글을 달려면 로그인하십시오.

채택된 답변

Torsten
Torsten 2023년 3월 17일
u=symunit;
Q(1)=10E-9*(u.C);
Q(2)=15E-9*(u.C);
Q(3)=7E-9*(u.C);
Qsum = sum(Q)
Qsum = 
  댓글 수: 1
Torsten
Torsten 2023년 3월 17일
편집: Torsten 2023년 3월 17일
u=symunit;
e0=8.854E-12*(u.s)^4*(u.A)^2/((u.kg)*(u.m)^3);
Q(1)=10E-9*(u.C);
Q(2)=15E-9*(u.C);
Q(3)=7E-9*(u.C);
rk(1,:)=[1 4 2]*1E-3*(u.m);
rk(2,:)=[-2 0 5]*1E-3*(u.m);
rk(3,:)=[-1 -2 3]*1E-3*(u.m);
r=[5 5 5]*1E-3*(u.m);
Qsum = zeros(1,3);
for k = 1:3
Qsum = Qsum + Q(k)*(r-rk(k,:))/(norm(r-rk(k,:)))^3;
end
Qsum = Qsum/(4*pi*e0);
simplify(Qsum.')
ans = 
Data = separateUnits(Qsum.');
double(Data)
ans = 3×1
1.0e+06 * 4.6438 2.2614 2.1837

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2023년 3월 17일
You cannot use a symbolic variable as an index.
I think it is unlikely that it will ever be supported to use symbolic variables as indices.
Create the definite values and sum() the definite values.

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by