For Loops with Multiple Arrays

12 views (last 30 days)
Aryn Sanojca
Aryn Sanojca on 8 Dec 2020
Answered: Steven Lord on 8 Dec 2020
I'm not able to figure out how to create a for loop that has multiple arrays as inputs. The input data is:
m = [1:1:11]
r = [1:1:100]
z = [0:1:50]
H = 50
z1 = 10
The equation is:
p(r,z) = sin((m*pi*z)/H)*sin((m*pi*z1)/H)*(1/(sqrt(r )))
I do not know how to set it up to get values of p(r,z) summed over the range of m. I also don't know how to get the values out of the loop so they can be used in other equations and plots. I've scoured the Answers board but I have not been able to construct any meaningful script yet - any and all insight is appreciated!
  2 Comments
James Tursa
James Tursa on 8 Dec 2020
You need to rethink this formula. Your starting r value is 0 and you are dividing by sqrt(r).
Aryn Sanojca
Aryn Sanojca on 8 Dec 2020
Right, great point. That's a conceptual oversight - the formula stands, but r = [1:1:50]. Thanks for pointing that out.

Sign in to comment.

Answers (2)

KALYAN ACHARJYA
KALYAN ACHARJYA on 8 Dec 2020
r=1:1:100;
z=1:1:50;
H=50;
z1=10;
z=?? % Define z, I assumed it as a scalar (single value)
syms m
p=vpa(symsum(sin((m*pi*z)/H)*sin((m*pi*z1)/H)*(1./sqrt(r)),m,1,11))
  1 Comment
Aryn Sanojca
Aryn Sanojca on 8 Dec 2020
Thanks Kalyan. I'm installing the symbolic math toolbox now to try this solution.
z is not a scalar, but rather defined as z = 1:1:50. Would this solution iterate through the calculation of p for all values of r at each value for z, and all values of z for each value for r? Or is a for loop required to do that?

Sign in to comment.


Steven Lord
Steven Lord on 8 Dec 2020
m = [1:1:11];
r = [1:1:100];
z = [0:1:50];
whos
Name Size Bytes Class Attributes m 1x11 88 double r 1x100 800 double z 1x51 408 double
The variable m is a 1-by-11 vector. The variable z is a 1-by-51 vector. What size would you expect sin(m*pi*z) to be?
  1. 1-by-11? If so how do you create 11 elements using z to perform the element-wise multiplication?
  2. 1-by-51? If so how do you create 51 elements using m to perform the element-wise multiplication?
  3. 11-by-51 or 51-by-11? These are actually possible using implicit expansion.
  4. Something else? If so what size and how would you create that using just m and z?
A11_51 = m.'.*z;
A51_11 = m.*z.';
whos A11_51 A51_11
Name Size Bytes Class Attributes A11_51 11x51 4488 double A51_11 51x11 4488 double
FYI the sinpi function may be of use to you.

Categories

Find more on Performance and Memory in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!