Scalar input in the integral function became a 150-column matrix...
1 次查看(过去 30 天)
显示 更早的评论
Hello. I'm Korean high school student in introductory Matlab.
I created an INTEGRAND function and put it into INTEGRAL to do an integral over PHI.
The integrand numel is 1, but when I put it into the integral, the integral numel is 150...
>>Potential(0,0.05,0)
You can't substitute because the left and right sides have different numbers of elements.
But when I run DMP only, numel is 1, but I only get this error when importing after Potential.
If I remove the 'ArrayValued', it does resize, but then I get this error again.
>>Potential(0,0.05,0)
The output of the function must be the same size as the input. If FUN is an integration function with array values, set the 'ArrayValued' option to true.
I asked a friend and he said that for some reason the integrand becomes a vector and therefore an integral....
Does anyone know how to fix it...?
I'm posting the full code because I think you might be able to troubleshoot the issue if you see the full code.
The problematic part is the part of the DMP function that has three for statements.
function B = DMP(x, y, z)
mu = 4 * pi * 10^(-7);
a = 3.175 * 10^(-3);
M = 0.903 * 10^6;
L = 25.4 * 10^(-3);
% Define the u, w, s functions as anonymous functions
u = @(k) x + a * (-1)^k;
w = @(k) z + (L / 2) * (-1)^k;
s = @(phi, k) sqrt((u(k) - a * cos(phi)).^2 + (y - a * sin(phi)).^2);
% Define the integrand function for numerical integration
integrand = @(phi, n, m, i) (cos(phi) .* (-1)^n) ./ ...
(w(n).^2 + s(phi, m).^2 + w(n) .* sqrt(w(n).^2 + s(phi, m).^2)) .* ...
((i == 1) * (u(m) - a .* cos(phi)) + ...
(i == 2) * (y - a .* sin(phi)) + ...
(i == 3) * (w(n) + sqrt(w(n).^2 + s(phi, m).^2)));
% Sum over the required indices with numerical integration
sum_f = zeros(3,1);
for n = 1:2
for m = 1:2
for i = 1:3
% Perform numerical integration for each combination of n, m, and i
int_result = integral(@(phi) integrand(phi, n, m, i), 0, 2 * pi,'ArrayValued',true);
sum_f(i) = sum_f(i) + int_result;
end
end
end
% Calculate DMP
B = (mu * M * a) / (4 * pi) * sum_f;
end
function U = Potential(x, y, z)
rho = 1680;
chi = -2 * 10^(-4);
g = 9.81;
mu = 4 * pi * 10^(-7);
b = 2.85 * 10^(-4);
l = 4.87 * 10^(-3);
% PDM_size 함수 정의
function size = DMP_size(w)
dmp = DMP(x, y, w);
size = sum(dmp .^ 2);
end
% integral 함수를 사용하여 적분 수행
int_B = integral(@DMP_size, z - l / 2, z + l / 2);
% Potential 계산
U = rho * g * y - chi * int_B / (mu * (chi + 2));
%B_cal = @(w) dot(DMP(x, y, w), DMP(x, y, w));
%int_B = integral(B_cal, z - l/2, z + l/2);
%U = rho * g * y - chi * int_B / (mu * (chi + 2));
end
1 个评论
lazymatlab
2024-6-20
编辑:lazymatlab
2024-6-20
I think the function integral tries to calculate the function values simulataneously, generating 150 values between z - l/2 and z + l/2 which makes z in DMP an array, w an array, int_result an array.
How about using trapzoidal rule directly, not integral?
回答(1 个)
prabhat kumar sharma
2024-7-4
Hello,
The error message you’re seeing, “The output of the function must be the same size as the input,” indicates that the integrand function is returning an array instead of a scalar. This discrepancy causes the error when you try to integrate it.
Here’s what’s happening:
- Your DMP function computes the integrand for different combinations of n, m, and i.
- When you use the integral function, it evaluates the integrand over the specified range, resulting in an array of values.
- Since the integrand is an array, the output of the integral becomes an array as well.
To resolve this issue, you can follow these steps:
- Set the ‘ArrayValued’ Option: When calling the integral function, set the 'ArrayValued' option to true. This tells MATLAB that the integrand function returns an array, and the integral should handle it accordingly. Modify your integral calls like this:int_result = integral(@(phi) integrand(phi, n, m, i), 0, 2 * pi, 'ArrayValued', true);
- Ensure Consistent Output Size: Make sure that the integrand function consistently returns a scalar value for each combination of n, m, and i. If it still produces an array, investigate why and adjust your integrand accordingly.
- Check Other Functions: Verify that other functions (such as u, w, and s) within your integrand are also returning scalar values.
I hope it will help you to resolve your issue!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 빅 데이터 처리 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!