Using quadgk inside a loop
Afficher commentaires plus anciens
How do I get quadgk to work inside a "for" loop?
I'm trying to solve an integral equation coupled with a first order ODE numerically. Therefore I need to evaluate an integral for many different steps of solving the ODE (I'm using the Euler method).
I can't seem to get my code to loop through quadgk which is the problem.
1 commentaire
Richard Brown
le 16 Avr 2012
You need to be a bit more specific about your problem - how about posting what you've tried. Then we can help.
Réponse acceptée
Plus de réponses (1)
Mike Hosea
le 16 Avr 2012
I agree with Richard. We'll probably need to see one of your attempts, but I can take a guess. Most likely you're having trouble with the vectorization of the integrand, and it's probably erroring in mtimes something, telling you about a dimension mismatch. If that's the problem, use ARRAYFUN, e.g. if the integrand is fun(x) but fun isn't vectorized (i.e., needs to be given scalars and will not operate elementwise on an input vector), try
quadgk(@(x)arrayfun(fun,x),a,b)
This works with the new INTEGRAL function as well, and it is probably the way to go, but with INTEGRAL there's another possibility:
integral(fun,a,b,'ArrayValued',true)
Although you might not think of the integrand as returning an array, when the 'ArrayValued' option is true, the integrand is only called with scalar input.
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!