Summing with multiple variables
9 次查看(过去 30 天)
显示 更早的评论
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 个)
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!