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!