How can I express this very long summation in Matlab? I'm having trouble

4 views (last 30 days)
I'm looking to plot the velocity contours of a fluid flow over a cross section (part e). I already have the two variables (eta and zeta) and I also know their bounds so I was able to succesfully setup the meshgrid. Now I just need to write my function but I am blanking out hard. Is there an easy way to express everything to the right of the summation in Matlab? Also, the summation does not have to go from 0 to infinity, my teacher said it's fine to evaluate just the first 5 terms. I would really appreicate any input.
  4 Comments
Jason Lee
Jason Lee on 25 Mar 2020
[z,n]=meshgrid(-0.5:0.005:0.5,-1:0.005:1);
w = 0;
for i = 0:5
w = w + (((-1).^i)/(3*i + 1).^3) .* ((cosh((2*i + 1) .* pi .* z))./(cosh((2*i + 1) .* pi))) .* cos((2*i + 1).* (pi/2) .* n);
end
Velocity = 1 - n.^2 -1.03 .* w
axis on
contour(z, n, Velocity);
Here's what I have so far. My problem is definitely in the loop somewhere but I'm just blanking out on how to fix it
Walter Roberson
Walter Roberson on 25 Mar 2020
A for loop starting from 0 is not a problem.
If you need to use the for loop value as an index, add 1 to it to get the index.

Sign in to comment.

Accepted Answer

David Hill
David Hill on 25 Mar 2020
I believe that below should work for you. I did not try it out since you did not provide all the information, but you have to compute the calculation for each (eta,zeta) pair in the meshgrid.
m=0:10;
[eta,zeta]=meshgrid()
for k=1:size(eta,1)
w(k,:)=arrayfun(@(et,ze)K*b^2/2*(1-et^2-4*(2/pi)^3*sum((-1).^m.*cosh((2*m+1)*pi*a*ze/2/b).*...
cos((2*m+1)*pi*et/2)./((2*m+1).^3.*cosh((2*m+1)*pi*a/2/b)))),eta(k,:),zeta(k,:));
end
  3 Comments
David Hill
David Hill on 25 Mar 2020
It works and runs for me. I did not know what the constants were, so I set them to 1.
m=0:10;
[eta,zeta]=meshgrid(-0.5:0.005:0.5,-1:0.005:1);
K=1;
b=1;
a=1;
w=zeros(size(eta));
for k=1:size(eta,1)
w(k,:)=arrayfun(@(et,ze)K*b^2/2*(1-et^2-4*(2/pi)^3*sum((-1).^m.*cosh((2*m+1)*pi*a*ze/2/b).*...
cos((2*m+1)*pi*et/2)./((2*m+1).^3.*cosh((2*m+1)*pi*a/2/b)))),eta(k,:),zeta(k,:));
end
Jason Lee
Jason Lee on 25 Mar 2020
That's exactly what I was looking for actually. Thank you so much! I think I just accidently forgot to copy something when I copied and pasted it in my screen.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!