Function headers/funtions for numerical approximations
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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)]);
0 comentarios
Respuestas (1)
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)
That worked, and the output is the same size as the input.
matrixMultiplication(1:5, 6:10) % does not work
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.]
0 comentarios
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!