Clear Filters
Clear Filters

"Arrays have incompatible sizes for this operation" problem occur when using integral function

2 views (last 30 days)
안녕하세요.
제가 만든 m file들을 첨부했습니다.
J.mlx 파일을 실행하면 "band.tunneling_current_density()" 라인에서 에러가 뜹니다.
에러 문구는 "이 연산에 대해 배열 크기가 호환되지 않습니다." 입니다.
문제가 되는 라인은 "Tunneling_current_density.m"에서 71번째 라인입니다.
해당 라인은 "integrand = @(x) sqrt(2 .* self.meff .* max(0, (self.func_Ec(x) - E)));" 이고,
이 중 "self.func_Ec(x) - E" 의 - 연산자에서 문제가 발생한 듯 보입니다.
혹시 어디서 문제가 생긴 것인지 알려주실 수 있나요?
  1 Comment
lazymatlab
lazymatlab on 25 Mar 2024
'bandprofile.dat' 파일이 없어서 정확히 확인할 수는 없지만, 71번 줄에서 self.func_Ec(x)와 E의 크기가 다른 것이 아닐까 싶습니다.

Sign in to comment.

Answers (1)

Nivedita
Nivedita on 3 May 2024
안녕하세요.
문의하신 내용은 영어로 답변해 드리겠습니다.
The issue arises from the operation "self.func_Ec(x) - E" due to a mismatch in the dimensions of the arrays involved in the operation. In MATLAB, operations between arrays require that the dimensions of the arrays match.
The function "self.func_Ec(x)" returns values based on the input "x", and "E" is a scalar value. MATLAB tries to perform the operation element-wise when possible, but if "self.func_Ec(x)" returns an array or vector of values (for vector "x"), and "E" is a scalar, normally this should not cause an issue due to MATLAB's broadcasting rules.
But, if the function's return values are structured in a way that does not align with simple element-wise subtraction, it could lead to dimension mismatch errors.
  • Assuming "self.func_Ec(x)" returns a single scalar value and "x" is a scalar, there should be no issue. The problem may occur when "x" is a vector. In such cases, you need to either restrict "x" to scalar values when calling "self.func_Ec" or add logic to handle vector "x" appropriately.
  • If you must handle "x" as a vector, you could use the "arrayfun" function to apply "self.func_Ec" to each element of "x" and then perform the operation with "E". For example, you could modify the "integrand" function as follows:
integrand = @(x) arrayfun(@(xi) sqrt(2 .* self.meff .* max(0, (self.func_Ec(xi) - E))), x);
By doing this, you apply the "self.func_Ec" function to each element of "x", perform the subtraction with "E" for each result, and then apply the "sqrt" function. "arrayfun" applies a function to each element of an input array and returns an array of the results.
For more details on "arrayfun", refer to the the following documentation link:
도움이 되었기를 바랍니다!

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!