Function headers/funtions for numerical approximations

2 visualizaciones (últimos 30 días)
Brendan
Brendan el 26 de Feb. de 2024
Respondida: Steven Lord el 26 de Feb. de 2024
I am trying to compute the double integral of a function that cannot be analytically integrated. I know that the integral2 funtion can do that, but I'm having trouble defining the funstion header for the first arguement so that it won't give me errors. Code is posted below, any advice?
% Define the function to be integrated
f = @(theta, phi) 1 + (2 * (1 + ((sin(0.785) * sin(theta) * cos(phi) - cos(theta) * cos(phi))^2))) / ((cos(theta) * cos(0.785))^3) + (4 * (5 - 1)) / ((cos(theta) * cos(0.785))^1.5 * (5 - (sin(theta) * sin(0.785) * cos(phi) + cos(theta) * cos(phi)))^2);
% Define the limits of integration
theta_lower = 0;
theta_upper = pi/2;
phi_lower = 0;
phi_upper = pi;
% Perform the double integration
result = integral2(f, theta_lower, theta_upper, phi_lower, phi_upper);
disp(['The result of the double integration is: ', num2str(result)]);

Respuestas (1)

Steven Lord
Steven Lord el 26 de Feb. de 2024
From the integral2 documentation page: "The function fun must accept two arrays of the same size and return an array of corresponding values. It must perform element-wise operations." You're using matrix operations not array operations. Compare the two functions below.
arrayMultiplication = @(x, y) x.*y;
matrixMultiplication = @(x, y) x*y;
Let's call them each with the same vectors.
arrayMultiplication(1:5, 6:10)
ans = 1×5
6 14 24 36 50
That worked, and the output is the same size as the input.
matrixMultiplication(1:5, 6:10) % does not work
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for elementwise multiplication.

Error in solution>@(x,y)x*y (line 2)
matrixMultiplication = @(x, y) x*y;
That didn't work. You probably received an error like the last line of code above, right?
Modify all your *, /, and ^ operators to use the array operators instead. [The + and - operators don't have ".+" and ".-" versions.]

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by