# I can not find an error in the expression

4 views (last 30 days)
Ilya Kotov on 24 Oct 2019
Edited: Jim Riggs on 25 Oct 2019
Hi all,
I'm new to matlab, so sorry for the stupid question.
I tried to fix the errors, but could not find another. I give the code of the expression that I want to get in Python and ask to fix this expression for matlab. Also, if not difficult, please share some good materials for learning matlab.
Thanks!
In MatLab with error:
fun = @(r,t,p) r.^2*sin(p).*(exp(i*(r.*sin(t).*cos(p) + r.*sin(t).*sin(p) + r.*cos(t))))*4*pi/(r.^2 - 4)
q = integral3(fun,0,1.99,0,pi,0,pi*2)
Error using * Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To
perform elementwise multiplication, use '.*'.

Katie on 24 Oct 2019
Hi! It looks like you're really close! You're just missing a couple dots in front of some of your multiplication and division operators. Below is the fixed expression. I also added in a couple sets of parentheses.
fun = @(r,t,p) (r.^2).*sin(p).*(exp(i*(r.*sin(t).*cos(p) + r.*sin(t).*sin(p) + r.*cos(t)))).*((4*pi)./(r.^2 - 4))
Summary of changes:
• placed the first term r.^2 in parenteses
• Added a dot in front of the first sin()
• added a dot in front of the last division operator
• added parentheses to the last term as below:
((4*pi)./(r.^2 -4))
Jim Riggs on 24 Oct 2019
Edited: Jim Riggs on 24 Oct 2019
Note that here it is assumed that t, r and p are all vectors of the same length. If not, this also will not work and will generate a similar error message due to incorrect matrix dimentions
Ilya Kotov on 24 Oct 2019
Thanks a lot, it's hard to get used to the strict syntax with tensors when you write python code

Jim Riggs on 24 Oct 2019
Edited: Jim Riggs on 25 Oct 2019
It's hard to say without seeing how the variables r, t, and p are defined. If r and p are vectors of different length, then the expression:
r.^2*sin(p)
will cause a problem, because it implies a matrix multiplication of a vector of length r and one of length p. (here r.^2 is a vector with the same length as r, and sin(p) is a vector with the same length as p)
In Matlab, if you want to multiply vector r and vector p, they both must be the same length, and one must be a row vector, and the other a column vector. For example, if r and p are the same size but both are row vectors, then r*p will give an error (This works in python, but not in matlab, although in python, this will perform element-wise multiplication). To perform the matrix multiply in matlab you would have to transform one of them:
r*p'
If you want to perform element-wise multiplication, r and p must still be the same length, and you use .*
r.*p
or
r.^2.*sin(p)
Katie on 24 Oct 2019
For this particular case, you don't need to define r, t,and p. They are terms in the expression 'fun' that is being integrated over from 0<r<1.99, 0<t<pi, and 0<p<2pi. When you run the two lines of code you get the following:
fun = @(r,t,p) (r.^2).*sin(p).*(exp(i*(r.*sin(t).*cos(p) + r.*sin(t).*sin(p) + r.*cos(t)))).*((4*pi)./(r.^2 - 4))
q = integral3(fun,0,1.99,0,pi,0,pi*2)
%output:
q =
1.1211e-07 - 1.2308e+02i
Jim Riggs on 24 Oct 2019
Edited: Jim Riggs on 24 Oct 2019
That's true, but it doesn't help Illya understand the rules of Matlab syntaxt.

### Categories

Find more on Scope Variables and Generate Names 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!