# Help needed for the below logic

1 view (last 30 days)

Show older comments

I have implemented a simple logic as shown below. Can I implement the below logic without using for loop? Is it possible to do that without looping by using some algorithm logic? I need to find all values of x for all values of range from 1:1000 due to which we get different values of various quantities

All are constants except x.

for q = 1:1000

v = a*(((b*t(q))/(pi))^(3/2));

k = 2*(((b*t(q))/(pi))^(3/2));

h1 = (t(q));

j2 = 1.17 - (4.73e-4*((t(q))^2))/(t(q) + 636);

eq2 = @(x) ((v)*exp(-(j3-x)/h1)) + ((ff1)/(1+4*exp(-(x-j1)/h1))) - (((k)*exp(-(x-j1)/h1)) + ((ff2)/(1+2*exp(-(j2-x)/h1))));

x2 = [0 1.73];

kk(q) = fzero(eq2, x2);

end

##### 6 Comments

Jan
on 16 Mar 2022

### Answers (1)

Jan
on 15 Mar 2022

Why do you want to avoid a loop? If it is running, everything is fine.

The calculation of the parameters v, k, h1, ... could be done in vectoprized form before the loop and ll and mm could be determined after the loop. Maybe this saves some time, but as long as fzero() is the most time consuming part, this is not serious. The actual call of fzero cannot be vectorized. Here only using a better initial guess is usful for an acceleration.

##### 2 Comments

Jan
on 16 Mar 2022

You can replace:

for q = 1:1000

v = a*(((b*t(q))/(pi))^(3/2));

k = 2*(((b*t(q))/(pi))^(3/2));

h1 = (t(q));

j2 = 1.17 - (4.73e-4*((t(q))^2))/(t(q) + 636);

...

end

by:

v = a * (b * t / pi) .^ (3/2);

k = 2 * (b * t / pi) .^ (3/2);

h1 = t; % Is this useful?!

j2 = 1.17 - 4.73e-4 * t .^ 2 ./ (t + 636);

This assumes, that t has the 1000 elements which are accessed in the loop. If the operations , / or ^ are applied to arrays, you need the elementwise forms ., ./ and .^ instead.

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!