My code has an opstruct error. What does it mean and how to solve it?

39 visualizaciones (últimos 30 días)
Yazhisai
Yazhisai el 27 de Oct. de 2024 a las 14:07
Editada: Pavl M. el 27 de Oct. de 2024 a las 16:39
clc
clear all
f = @(x, y) exp(-(x.^2 + y.^2));
x_min = 0;
x_max = 1;
y_min = @(x) 0;
y_max = @(x) -sqrt(1 - x.^2);
result = integral2(f, x_min, x_max, y_min, y_max);
fprintf('The result of the integral is:', result);
fsurf(@(x, y) exp(-(x.^2 + y.^2)), [0 1 -1 0], 'EdgeColor', 'none');
grid on;

Respuesta aceptada

Umar
Umar el 27 de Oct. de 2024 a las 15:29

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.

Más respuestas (1)

Pavl M.
Pavl M. el 27 de Oct. de 2024 a las 14:36
Editada: Pavl M. el 27 de Oct. de 2024 a las 16:39
format long
clc
clear all
f = @(x,y)exp(-(x.^2+y.^2));
x_min = 0;
x_max = 1;
y_min = 0 ;% it already matched w/o too much text
% better than later proposed @(x) zeros(size(x)), because x_min, x_max are floating point scalars;
y_max = @(x)-sqrt(1-x.^2);
result = integral2(f,x_min,x_max,y_min,y_max,'Method','auto','AbsTol',0,'RelTol',1e-12);
fprintf('The result of the integral is:');
The result of the integral is:
disp(result)
-0.496466325949718
fsurf(@(x,y) exp(-(x.^2+y.^2)),[0 1 0 1],'EdgeColor','green');
title('Original 1st Solution')
xlabel('x')
ylabel('y')
zlabel('f')
grid on;

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by