필터 지우기
필터 지우기

Error using .* Matrix dimensions must agree.

조회 수: 2 (최근 30일)
Pradipta Panchadhyayee
Pradipta Panchadhyayee 2023년 11월 25일
댓글: Dyuman Joshi 2023년 11월 25일
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
Pradipta Panchadhyayee
Pradipta Panchadhyayee 2023년 11월 25일
Thanks for your reply. F will be a function of only u and v when its will be integrated over x and y.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
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
Pradipta Panchadhyayee
Pradipta Panchadhyayee 2023년 11월 25일
@ 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 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
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?
  댓글 수: 1
Pradipta Panchadhyayee
Pradipta Panchadhyayee 2023년 11월 25일
Thanks for your reply. F will be a function of only u and v when its will be integrated over x and y.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by