How to use a symbolic variable to refer to a specific cell on an array
4 views (last 30 days)
Show older comments
Georgios Skarmoutsos
on 22 Sep 2020
Commented: Georgios Skarmoutsos
on 25 Sep 2020
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 Comments
Walter Roberson
on 24 Sep 2020
What is the value of l as in exp(2*l-z) ?
Where did the coth() term come from?
Accepted Answer
Walter Roberson
on 23 Sep 2020
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 Comments
Walter Roberson
on 25 Sep 2020
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]) );
More Answers (0)
See Also
Categories
Find more on Bessel functions in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!