- 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:
I can not find an error in the expression
조회 수: 1 (최근 30일)
이전 댓글 표시
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 '.*'.
댓글 수: 0
채택된 답변
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:
((4*pi)./(r.^2 -4))
댓글 수: 2
추가 답변 (1개)
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
Katie
2019년 10월 24일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!