How to use a symbolic variable to refer to a specific cell on an array
조회 수: 4 (최근 30일)
이전 댓글 표시
I am trying to plot this function for ρ=constant(380) and z=constant(12) (so as a function of φ) but when I run the code I have a problem because i use a double symsum to refer to the Smn values of an array. I know that a symbolic variable cannot be used as an index of an array but I don't know how to overcome this problem. Jm is the mth Bessel waveform, Smn is the nth root of the mth Bessel waveform.The file that generates the J array is the BesselJ.m and the file that generates the roots of the mth bessel function is the Smn.n along with his function bz.m.
The error code is: Error using sym/subsindex (line 855)
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function
body must be sym expression.
Error in Untitled11 (line 10)
b=symsum(symsum(vpaintegral(J(2*m-1,:)*380/Ro,r,[0
Ro]).*interp1(v,J(2*m-1,:),s(2*m-1,2*n-1)*380/Ro).*cos((2*n-1)*pi*phi/tp)*(exp(s(2*m-1,2*n-1)*z/Ro)+exp((2*l-z)*s(2*m-1,2*n-1)/Ro))./((2*n-1)*pi*interp1(v,J(2*m,:).^2,s(2*m-1,2*n-1))),n,1,10),m,1,10);
The main code is:
phi=0:0.25:40*pi;
tp=74.61;
Ro=460;
z=12;
syms n m r
b=symsum(symsum(vpaintegral(J(2*m-1,:)*380/Ro,r,[0 Ro]).*interp1(v,J(2*m-1,:),s(2*m-1,2*n-1)*380/Ro).*cos((2*n-1)*pi*phi/tp)*(exp(s(2*m-1,2*n-1)*z/Ro)+exp((2*l-z)*s(2*m-1,2*n-1)/Ro))./((2*n-1)*pi*interp1(v,J(2*m,:).^2,s(2*m-1,2*n-1))),n,1,10),m,1,10);
B=double(b);
plot(phi,B);
The issue is how to refer on a specific cell of the S array and the Bessel waveform array using the symbolic variables m and n. I know that with symbolic variables this cannot really happen so i am looking for a convertion command in order to keep the symbolic summation.
댓글 수: 5
Walter Roberson
2020년 9월 24일
What is the value of l as in exp(2*l-z) ?
Where did the coth() term come from?
채택된 답변
Walter Roberson
2020년 9월 23일
Symbolic variables can never be used to index an array. In particular, you are attempting J(2*m-1,:) . The ':' tells us that J is almost certainly an array, so you are trying to index using a symbolic variable.
When you have a finite set of things to index in an expression, you should not use symsum() or symprod(): instead you should create the full list of values as arrays, and sum() or prod() as appropriate.
You might need to vectorize along a 3rd or even 4th dimension to get your calculations right to end up with a 2D array as the overall result.
댓글 수: 5
Walter Roberson
2020년 9월 25일
If you mean one particular numeric m = n, then
squeeze(all_together(that_value, that_value, :))
If you want all of the m=n, but added together, sum of the diagonal for each theta, then perhaps the easiest approach would be
mask = repmat(diag(size(all_together,1), size(all_together,2)), 1, 1, size(all_together,3));
f_rho_phi_z = squeeze( sum( all_together .* mask, [1 2]) );
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Bessel functions에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!