Error using .* Matrix dimensions must agree.
조회 수: 2 (최근 30일)
이전 댓글 표시
clc; clear all;
den = @(x,y) x.^2 + y.^2;
A = @(x,y) 1./den(x,y);
[u,v] = meshgrid(-1:0.5:1,-1:0.5:1);
fun = @(x,y) (x.*u).*(y.*v).*A(x,y);
F = integral2(fun,0,1,0,1);
Fp = F.*conj(F)*u.*v;
surf(u, v, Fp)
shading interp;
colormap jet
채택된 답변
Walter Roberson
2023년 11월 25일
integral2() passes in 2D arrays of variable but equal size to the function handle. For example one time it might pass in a pair of 14 x 14 arrays, and another time it might pass in 2 x 3 arrays.
fun = @(x,y) (x.*u).*(y.*v).*A(x,y);
Those arrays of arbitrary size do not happen to match up with the size of u or v
The function you pass to integral2() must return an array the same size as the inputs. It is completely invalid to try to return a mesh of points per x y pair .
I would point out that your u and v appear as local constants in the functions. You could substitute 1 for u and v and get out an scalar integral value, and then multiply that scalar result by u.*v to get the grid you were hoping to calculate.
댓글 수: 5
Dyuman Joshi
2023년 11월 25일
"But making this change the runtime becomes same as before."
No, There will definitely be a change in runtime.
"I was checking with long expressions of x and y in den and A and checking with which speed the values are calculated. That's why I used C1 and C2 in the loop."
This makes no sense, atleast to me.
Did you make the changes I said and then compared the runtime speed?
추가 답변 (1개)
Paul
2023년 11월 25일
The anonymous function fun won't work unless the input arguments x and y are compatible for elementwise-multiplication with u and v respectively. That's unlikely to be the case with fun is called from integral2 and probably isn't what you want even if it were. Can you explain or show mathemetically what F is supposed to be?
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!