Error using .* Matrix dimensions must agree.
4 次查看(过去 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?
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!