For Loops with Multiple Arrays

조회 수: 6 (최근 30일)
Aryn Sanojca
Aryn Sanojca 2020년 12월 8일
답변: Steven Lord 2020년 12월 8일
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
James Tursa
James Tursa 2020년 12월 8일
You need to rethink this formula. Your starting r value is 0 and you are dividing by sqrt(r).
Aryn Sanojca
Aryn Sanojca 2020년 12월 8일
Right, great point. That's a conceptual oversight - the formula stands, but r = [1:1:50]. Thanks for pointing that out.

댓글을 달려면 로그인하십시오.

답변 (2개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2020년 12월 8일
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
Aryn Sanojca
Aryn Sanojca 2020년 12월 8일
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?

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 2020년 12월 8일
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.

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by