Hi @Yazhisai,
After reviewing the documentation provided at the link below,
https://www.mathworks.com/help/matlab/ref/integral2.html
you need to ensure that both y_min and y_max return outputs that are compatible with the input size. Here is how you can modify your code:
% Define the function to integrate f = @(x, y) exp(-(x.^2 + y.^2));
% Define the limits of integration x_min = 0; x_max = 1;
% Define the lower and upper limits for y y_min = @(x) zeros(size(x)); % Lower limit is constant (0) y_max = @(x) sqrt(1 - x.^2); % Upper limit based on x
% Perform the double integral result = integral2(f, x_min, x_max, y_min, y_max);
% Display the result fprintf('The result of the integral is: %.4f\n', result);
% Visualize the function using fsurf fsurf(@(x, y) exp(-(x.^2 + y.^2)), [0 1 0 1], 'EdgeColor', 'none'); grid on; title('Surface Plot of f(x, y) = e^{-(x^2 + y^2)}'); xlabel('x-axis'); ylabel('y-axis'); zlabel('f(x, y)');
Please see attached.
In the above code snippet, changed y_min = @(x) 0; to y_min = @(x) zeros(size(x)); to ensure that it returns an array of zeros matching the size of input x. The upper limit function remains unchanged as it already returns an output consistent with the input size.
This updated code should now work correctly without any errors, allowing you to compute and visualize the desired double integral.
If you encounter further issues with performance or accuracy during integration, consider adjusting tolerance settings or using different integration methods (like iterated or tiled) based on your specific case.
Please let me know if you have any further questions.