MATLAB Answers

I can not find an error in the expression

2 views (last 30 days)
Ilya Kotov
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!
Снимок.PNG
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 '.*'.

Accepted Answer

Katie
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))
  2 Comments
Ilya Kotov
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

Sign in to comment.

More Answers (1)

Jim Riggs
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)
  2 Comments
Jim Riggs
Jim Riggs on 24 Oct 2019
That's true, but it doesn't help Illya understand the rules of Matlab syntaxt.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by