Summing with multiple variables

18 ビュー (過去 30 日間)
Tristan Blackledge
Tristan Blackledge 2019 年 10 月 28 日
回答済み: Asvin Kumar 2019 年 10 月 31 日
I am trying to plot Phi vs x2 but am finding trouble with the summation function. I need to sum for all values of "n" and place the results in a vector for the range x2 so that the vector is the same length as the vector x2 to plot. Would someone be able to show how that is done. Thank you.
D=2;
Ea=0.1;
vEf=0.12;
delta_a=10*pi;
v=0.22;
Phi_o=1;
t=1;
x1=(-delta_a/2:0.1:delta_a/2);
x2=x1';
n=1:2:5;
Bm=n*(pi/delta_a);
lambdam=v*(D*(Bm.^2)+Ea-vEf);
syms x
f=Phi_o.*cos(Bm*x);
Am=2/delta_a.*int(f,-delta_a/2,delta_a/2);
Phi=sum(Am.*cos(Bm.*x2).*exp(-lambdam*t));
plot(x2,Phi)
xlim(-lambdam/2:lambdam/2)
  1 件のコメント
darova
darova 2019 年 10 月 28 日
Can't use for loop?

サインインしてコメントする。

回答 (1 件)

Asvin Kumar
Asvin Kumar 2019 年 10 月 31 日
Try replacing the appropriate line of code with the following:
Phi=sum(Am.*cos(Bm.*x2).*exp(-lambdam*t),2);
The sum function by default sums along the first array dimension whose size does not equal 1 which in your case is the 1st dimension. Summing along the 2nd dimension should give you output of expected length.
Here’s the documentation for reference: https://www.mathworks.com/help/matlab/ref/sum.html#btv6ok6-1-dim

カテゴリ

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

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by