Calculating Integral using Array Input
14 views (last 30 days)
Show older comments
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)
%...
0 Comments
Accepted Answer
Jan
on 9 Jun 2021
Edited: Jan
on 9 Jun 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.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!