MATLAB Answers

Summing with multiple variables

43 views (last 30 days)
Tristan Blackledge
Tristan Blackledge on 28 Oct 2019
Answered: Asvin Kumar on 31 Oct 2019
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 Comment

darova
darova on 28 Oct 2019
Can't use for loop?

Sign in to comment.

Answers (1)

Asvin Kumar
Asvin Kumar on 31 Oct 2019
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 Comments

Sign in to comment.

Sign in to answer this question.

Tags

Products


Release

R2019b