I can not find an error in the expression

조회 수: 3(최근 30일)
Ilya Kotov
Ilya Kotov 2019년 10월 24일
편집: Jim Riggs 2019년 10월 25일
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 '.*'.

채택된 답변

Katie
Katie 2019년 10월 24일
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
Ilya Kotov
Ilya Kotov 2019년 10월 24일
Thanks a lot, it's hard to get used to the strict syntax with tensors when you write python code

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Jim Riggs
Jim Riggs 2019년 10월 24일
편집: Jim Riggs 2019년 10월 25일
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
Jim Riggs
Jim Riggs 2019년 10월 24일
편집: Jim Riggs 2019년 10월 24일
That's true, but it doesn't help Illya understand the rules of Matlab syntaxt.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by