Help needed for the below logic

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

Avoid an overkill with parentheses. Matlab is not the preprocessor of C.
v = a*(((b*t(q))/(pi))^(3/2)); % Original
v = a * (b * t(q) / pi) ^ (3/2); % Easier to read
% Original:
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))));
% Easier to read:
eq2 = @(x) v * exp((x - j3) / h1) + ff1 / (1 + 4 * exp((j1 - x) / h1)) - ...
k * exp((j1 - x) / h1) - ff2 / (1 + 2 * exp((x - j2) / h1));
Do you see that a leaner notation and some spaces let typos be much easier to find?
What is the purpose of defining S11, S12, S13, if they are overwritten in each iteration?
Amy Topaz
Amy Topaz 2022년 3월 16일
편집: Amy Topaz 2022년 3월 16일
t varies from 10 to 10000 with scale of 1000.
t = linspace (10,2000,1000)
"for q = 1:1000" , here 1000 is the scale of t
I need to find the value of x for all these values of t. Since t varies, so the dependent variables like v, k, h1 etc would vary for each value of t right? So I would get a matrix of values over this range of variation of t. Let me know how else this could be done?
q = 1:1000;
h1 = t(q);
k = 2*(b*h1/pi).^1.5;
v = k/2*a;
j2 = 1.17 - (4.73e-4*h1^2)./(h1 + 636);
x2 = [0, 1.73];
for i = 1:1000
eq2 = @(x) v(i) * exp((x - j3) / h1(i)) + ff1 / (1 + 4 * exp((j1(i) - x) / h1(i))) - ...
k(i) * exp((j1(i) - x) / h1(i)) - ff2 / (1 + 2 * exp((x - j2(i)) / h1(i)));
kk(i) = fsolve(eq2, x2);
x2 = kk(i);
end
Amy Topaz
Amy Topaz 2022년 3월 16일
Thank you,
So in the last line why is it written as x2 = kk(i)?
Amy Topaz
Amy Topaz 2022년 3월 16일
편집: Amy Topaz 2022년 3월 16일
Actually I am getting the below error while trying to implement the same equation with the mentioned method:
Kindly help
Jan
Jan 2022년 3월 16일
"So in the last line why is it written as x2 = kk(i)?" - this is an educated guess of a good initial point for the next search by fzero: The solution for a specific parameter can be assumed to be near to the one of the former iteration.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2022년 3월 15일

0 개 추천

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

Amy Topaz
Amy Topaz 2022년 3월 16일
How can I determine 'v, k, h1, ... could be done in vectoprized form before the loop' ? Like it has a variable t which varies over 1:1000. How can I do this calculation without for loop?
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2022년 3월 15일

댓글:

Jan
2022년 3월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by