Calculating Integral using Array Input

11 vues (au cours des 30 derniers jours)
J_P
J_P le 9 Juin 2021
Commenté : J_P le 9 Juin 2021
Hi there,
I'm trying to calculate the following, and was wondering whether anyone could suggest a quicker way of doing it, other than introducing a loop. Ideally, I want something that is similar to integral but can work with an array input also (an "elementwise" integral?).
Thanks a lot!
R = [5e-07,1e-06,1.75e-06,2.5e-06,3.5e-06]
phi = 40.2594
b = R/cosd(phi)
n = [1,3,10,17,35]
R_1 = R(1)
b_1 = b(1)
n_1 = n(1)
fun = @(x) (1-(R_1-x).*(b_1-x)/(R_1*b_1)).^(n_1-1).*(((R_1-x)+(b_1-x))/(R_1*b_1)).*n_1.*x
lambda_max = integral(fun,0,R_1)
R_2 = R(2)
b_2 = b(2)
n_2 = n(2)
fun = @(x) (1-(R_2-x).*(b_2-x)/(R_2*b_2)).^(n_2-1).*(((R_2-x)+(b_2-x))/(R_2*b_2)).*n_2.*x
lambda_max = integral(fun,0,R_2)
%...

Réponse acceptée

Jan
Jan le 9 Juin 2021
Modifié(e) : Jan le 9 Juin 2021
You do need a loop to get the best solution. Remember that integral is an adaptive method, which creates finer grids until a certain accuracy is reached. Using a set of different parameters does not allow to use the optimal grid for all values, such that it is expected, that you need more function evaluations and get a less accurate solution due to the accumulated rounding errors.
Use a loop and provide the parameters one after the other.
You can use parfor to accelerate the computations.
  1 commentaire
J_P
J_P le 9 Juin 2021
Ah fair enough, I did not think of the grid itself. Well, worth a try, thanks a lot!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements 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!

Translated by