Error using integral2C​alc>integr​al2t/tenso​r (line 231) Input function must return 'double' or 'single' values. Found 'sym'.

3 Ansichten (letzte 30 Tage)
I'd like to know where the mistake in this code, and the solution
h = 0.00172; rho = 1620;
L = 100*h; c=L*L/4;
M=10; N=10;
syms xi eta
shape_w=sym(zeros(M,N)); x_m=sym(zeros(M,1)); y_n=sym(zeros(N,1));
for m=1:M
for n=1:N
x_m(m)=(xi^m)*(1+xi);
y_n(n)=(eta^n)*(1-eta)*(1+eta)^2;
shape_w(m,n)=x_m(m)*y_n(n);
end
end
fun=(shape_w*shape_w');
M_w =c*(rho*h)*integral2(@(xi,eta)fun,-1,1,-1,1);
  1 Kommentar
khaled Elmorabie
khaled Elmorabie am 2 Feb. 2021
I have used
fun=matlabFunction(shape_w*shape_w');
but obtain another error
Error using integral2Calc>integral2t/tensor (line 231)
Input function must return 'double' or 'single' values. Found 'function_handle'.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Harshavardhan
Harshavardhan am 25 Jun. 2025
The error in your original code arises because “integral2” cannot directly integrate symbolic expressions or matrices. It requires a numeric function handle that returns double or single values. To integrate the full matrix resulting from “shape_w * shape_w'”, you need to:
  • Convert each element of the resulting matrix into a numeric function using matlabFunction.
  • Integrate each element individually using integral2.
Here is the updated code after calculating “fun”:
% Initialize result matrix
integrated_matrix = zeros(M, M);
% Integrate each element of the matrix
for i = 1:M
for j = 1:M
integrand = matlabFunction(fun(i, j), 'Vars', [xi, eta]);
integrated_matrix(i, j) = c * rho * h * integral2(integrand, -1, 1, -1, 1);
end
end
For more information on “matlabFunction” and “integral2” refer to their respective links below: https://www.mathworks.com/help/symbolic/sym.matlabfunction.html

Walter Roberson
Walter Roberson am 27 Jun. 2025
h = 0.00172; rho = 1620;
L = 100*h; c=L*L/4;
M=10; N=10;
syms xi eta
shape_w=sym(zeros(M,N)); x_m=sym(zeros(M,1)); y_n=sym(zeros(N,1));
for m=1:M
for n=1:N
x_m(m)=(xi^m)*(1+xi);
y_n(n)=(eta^n)*(1-eta)*(1+eta)^2;
shape_w(m,n)=x_m(m)*y_n(n);
end
end
fun=(shape_w*shape_w');
M_w = c * (rho*h) * int(int(fun, eta, -1, 1), xi, -1, 1)
M_w = 
d_M_w = double(M_w)
ans = 10×10
0.0081 0.0061 0.0052 0.0043 0.0039 0.0034 0.0031 0.0028 0.0025 0.0023 0.0061 0.0052 0.0043 0.0039 0.0034 0.0031 0.0028 0.0025 0.0023 0.0022 0.0052 0.0043 0.0039 0.0034 0.0031 0.0028 0.0025 0.0023 0.0022 0.0020 0.0043 0.0039 0.0034 0.0031 0.0028 0.0025 0.0023 0.0022 0.0020 0.0019 0.0039 0.0034 0.0031 0.0028 0.0025 0.0023 0.0022 0.0020 0.0019 0.0018 0.0034 0.0031 0.0028 0.0025 0.0023 0.0022 0.0020 0.0019 0.0018 0.0017 0.0031 0.0028 0.0025 0.0023 0.0022 0.0020 0.0019 0.0018 0.0017 0.0016 0.0028 0.0025 0.0023 0.0022 0.0020 0.0019 0.0018 0.0017 0.0016 0.0015 0.0025 0.0023 0.0022 0.0020 0.0019 0.0018 0.0017 0.0016 0.0015 0.0014 0.0023 0.0022 0.0020 0.0019 0.0018 0.0017 0.0016 0.0015 0.0014 0.0014
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  1 Kommentar
Walter Roberson
Walter Roberson am 27 Jun. 2025
You can use matlabFunction() on fun, but the result would be a function handle that returns a 10 x 10 array, which is something that is incompatible with integral2(). integral2() passes arrays of coefficients to the given function, and the given function must return an array the same size as the arrays that were passed in.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by