Error using .* Matrix dimensions must agree.

4 views (last 30 days)
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);
Arrays have incompatible sizes for this operation.

Error in solution>@(x,y)(x.*u).*(y.*v).*A(x,y) (line 6)
fun = @(x,y) (x.*u).*(y.*v).*A(x,y);

Error in integral2Calc>integral2t/tensor (line 228)
Z = FUN(X,Y); NFE = NFE + 1;

Error in integral2Calc>integral2t (line 55)
[Qsub,esub] = tensor(thetaL,thetaR,phiB,phiT);

Error in integral2Calc (line 9)
[q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);

Error in integral2 (line 105)
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
Fp = F.*conj(F)*u.*v;
surf(u, v, Fp)
shading interp;
colormap jet
  1 Comment
Pradipta Panchadhyayee
Pradipta Panchadhyayee on 25 Nov 2023
Thanks for your reply. F will be a function of only u and v when its will be integrated over x and y.

Sign in to comment.

Accepted Answer

Walter Roberson
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
Pradipta Panchadhyayee
Pradipta Panchadhyayee on 25 Nov 2023
@ Bring the definitions of den and A out of the loos as they are not varying with the loops.
Thanks for your reply. I admit. I take it a representative program. If I deal with long expressions of x and y in den and A the runtime is increased with loop structures. But making this change the runtime becomes same as before. I want to get rid of loops if there is another way to make runtime error minimum.
@ There's no need of defining C1 and C2 in the loop, nor are you using the variables afterwards in the code, so remove those two lines.
I admit. 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.
Dyuman Joshi
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?

Sign in to comment.

More Answers (1)

Paul
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?
  1 Comment
Pradipta Panchadhyayee
Pradipta Panchadhyayee on 25 Nov 2023
Thanks for your reply. F will be a function of only u and v when its will be integrated over x and y.

Sign in to comment.

Categories

Find more on Just for fun in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!