Error using .* Matrix dimensions must agree.
4 views (last 30 days)
Show older comments
Pradipta Panchadhyayee
on 25 Nov 2023
Commented: Dyuman Joshi
on 25 Nov 2023
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
Accepted Answer
Walter Roberson
on 25 Nov 2023
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 Comments
Dyuman Joshi
on 25 Nov 2023
"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?
More Answers (1)
Paul
on 25 Nov 2023
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?
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!