How do I find the element address in an array involved in a loop by applying conditions?

1 view (last 30 days)
I am deriving solution for finite difference method. I have to determine at what value of n does value of T(151) reaches 25. How do I do that?
here m = 200.
Updated syntax:
t=0;
dt = 0.0056;
m = 2000;
for n = 1:15000
T(1) = 30;
t = t + dt;
T0 = T;
for i = 2:m
T(i) = T0(i) - C*T0(i+1) + C*T0(i-1) + D*T0(i+1) - D*2*T0(i) + D*T0(i-1);
end
T(m+1) = T(m);
ifT(151) == 25
fprintf('Value is %d',n);
end
end
The temperature at i=151 is 25 by analytical solution but the program does not return any value of n.
Please help

Accepted Answer

Guillaume
Guillaume on 7 May 2019
You haven't shown us how T is defined. Presumably it's an array with at least m+1 elements.
This is not going to help with your problem, but your inner loop is unnecessary:
for n = 1:15000
T(1) = 30;
t = t + dt;
%The following line replace your inner line. T0 is unnecessary
T(2:m) = T(2:m) - C*T(3:m+1) + C*T(1:m-1) + D*T(3:m+1) - 2*D*T(2:m) + D*T(1:m-1);
T(m+1) = T(m);
end
The T calculation can be further simplified to:
T(2:m) = (1-2*D)*T(2:m) + (C+D)*T(1:m-1) + (D-C)*T(3:m+2);
Now, with your problem, it's very unlikely that any value of T will ever be exactly equal to 25. Even if theoretically, it should be equal to 25, the limited precision of computers means that in practice that 25 will be off by a minute amount. When dealing with floating point numbers resulting from calculations you should never compare numbers with ==. You should always compare the absolute difference with your target to a small tolerance appropriate to your problem. So
if abs(T(125) - 25) < 1e-10 %1e-10 may or may not be appropriate for your problem.
fprintf('Value is %d\n', n); %this is the correct fprintf syntax by the way.
end
For more information about floating point comparison, see why is 0.3-0.2-0.1 not equal to 0.

More Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 7 May 2019
Edited: KALYAN ACHARJYA on 7 May 2019
As your code seems mess to me, althoght I have given the answer you asked for.
when T array, Tth 125 element equal to 25, if condition true, so it print n values at present exucation.
if T(125)==25
fprintf('Value is n is %d',n);
end
Suggestion to avoid multiples loops for efficient code (If possible).
  5 Comments
KALYAN ACHARJYA
KALYAN ACHARJYA on 7 May 2019
Edited: KALYAN ACHARJYA on 7 May 2019
@Anush There are multiple thread in this forum and public domain, how to avoid loops (If possible), please go through. Link 1 Link 2 or logical indexing do Google search.
That was my suggestion only.Your actual question was
How do I find the element address in an array involved in a loop by applying conditions?
Here T is a array, you can check during exucation, whenther T(125)=25 or not,
As you have tried in your code, its works
if T(125)==25
fprintf('Value is n is %d',n);
end
Anotherway
If you have T array, want to know wheather T(125) = 25 or not.
condition=find(T(125)==25);
if true, condition return 1 value, then only print the n value.
Or
After calculation, you can find the index position, where elements value is 25, as mention below-
>> a=[10 21 33 40 50 50];
>> idx=find(a(:)==50)
idx =
5
6

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!