integral() doesn't accept scalar function as input
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Julian Groß-Funk
le 5 Juil 2023
Commenté : Star Strider
le 5 Juil 2023
Psi = @(x) [1 x]
fun = @(x) abs(Psi(x)*Psi(x)')
integral(fun,0,1)
It is necessary for me to numerically integrate over a function (fun), that gets a vector-function (Psi) as input. fun is NOT array-valued. Why doesn't this work?
0 commentaires
Réponse acceptée
Star Strider
le 5 Juil 2023
It works correctly with the 'ArrayValued' name-value pair —
Psi = @(x) [1 x]
fun = @(x) abs(Psi(x)*Psi(x)')
integral(fun,0,1, 'ArrayValued',1)
.
2 commentaires
Plus de réponses (1)
Jayant
le 5 Juil 2023
The error you encountered occurs because the function fun you defined is not compatible with the integral function in MATLAB. The integral function expects the integrand function to have a scalar output for each input value, but in your case, the fun function returns a vector output.
To resolve this issue, you can modify the fun function to return a scalar value by taking the norm (magnitude) of the vector output of Psi(x):
Psi = @(x) [1 x];
fun = @(x) norm(Psi(x))^2;
integral(fun, 0, 1)
Please note that I used norm(Psi(x))^2 instead of abs(Psi(x)*Psi(x)') to calculate the squared norm, as it is more efficient and mathematically equivalent.
3 commentaires
Jayant
le 5 Juil 2023
I misunderstood your previous statement. To resolve the error, you can modify the fun function to handle array-valued inputs by setting the 'ArrayValued' option to true in the integral function. This will inform MATLAB that the integrand function is array-valued. Below is the modified code:
Psi = @(x) [1 x];
fun = @(x) abs(Psi(x)*Psi(x)');
integral(fun, 0, 1, 'ArrayValued', true)
By including the 'ArrayValued', true option in the integral function, the error related to array-valued inputs should be resolved, and the integration should proceed correctly.
Please try the updated code & let me know if you face any further issues. Apologies for the confusion.
Voir également
Catégories
En savoir plus sur Numerical Integration and Differentiation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!