How do get past the first interation in this nested loop?

old=Rod_Temp_Gradient;
new=zeros(size(Rod_Temp_Gradient));
count=0;
while max(new)- min(new)<0.5
for col=length(old)
new=[mean(old(1:2)), conv(old,[1,1,1]/3,'valid'), mean(old(end-1:end))];
end
old=new;
count=count+1;
end

8 件のコメント

Rik
Rik 2018 年 2 月 2 日
If you correctly format your question (select you code and hit the {}Code button) and mention the error (all the text in red), we can help you much more easily.
KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 2 月 2 日
Pls, clarify Past or Pass?
Josh Neff
Josh Neff 2018 年 2 月 2 日
old=Rod_Temp_Gradient;
new=zeros(size(Rod_Temp_Gradient));
count=0;
while max(new)- min(new)<0.5
for col=1:length(old)
new=[mean(old(1:2)), conv(old,[1,1,1]/3,'valid'), mean(old(end-1:end))];
end
old=new;
count=count+1;
end
I do not receive any errors, but I can look at the vector and see the max and min are not within 0.5 of each other and it won't preform past the first iteration. It needs to preform several until equilibrium is reach in the heat distribution in a 1 d array rod or until the max and min are within range. it won't keep going, it stops after the first iteration.
Josh Neff
Josh Neff 2018 年 2 月 2 日
It doesn't make it to iteration 2, 3, 4, .....
Jan
Jan 2018 年 2 月 2 日
Please do not explain, what the computer does not do, but what it does do instead.
Rik
Rik 2018 年 2 月 2 日
Can you share an example of Rod_Temp_Gradient so we can run this code to see what happens?
Josh Neff
Josh Neff 2018 年 2 月 2 日
100 96.0789439152323 92.3116346386636 88.6920436717158 85.2143788966211 81.8730753077982 78.6627861066553 75.5783741455726 72.6149037073691 69.7676326071031 67.0320046035639 64.4036421083141 61.8783391806141 59.4520547970194 57.1209063848815 54.8811636094027 52.7292424043049 50.6616992365590 48.6752255959972 46.7666427009909 44.9328964117222 43.1710523429080 41.4782911681581 39.8519041084514 38.2892885975112 36.7879441171442
Matt J
Matt J 2018 年 2 月 2 日
I just wanted to point out that putting the line
new=[mean(old(1:2)), conv(old,[1,1,1]/3,'valid'), mean(old(end-1:end))];
inside the following for-loop makes no sense. The loop is just repeating the exact same calculation multiple times.
for col=1:length(old)
new=[mean(old(1:2)), conv(old,[1,1,1]/3,'valid'), mean(old(end-1:end))];
end

サインインしてコメントする。

回答 (2 件)

Rik
Rik 2018 年 2 月 2 日

3 投票

The problem lies with your condition in the while command. It keeps executing while some statement is true, not until some statement is true. The problem is that you initialize new with only zeros, so the first iteration the condition is true, as max-min is 0.
Rod_Temp_Gradient=[100 96.0789439152323 92.3116346386636 88.6920436717158 85.2143788966211 81.8730753077982 78.6627861066553 75.5783741455726 72.6149037073691 69.7676326071031 67.0320046035639 64.4036421083141 61.8783391806141 59.4520547970194 57.1209063848815 54.8811636094027 52.7292424043049 50.6616992365590 48.6752255959972 46.7666427009909 44.9328964117222 43.1710523429080 41.4782911681581 39.8519041084514 38.2892885975112 36.7879441171442];
old=Rod_Temp_Gradient;
new=zeros(size(Rod_Temp_Gradient));
count=0;
while (max(new)- min(new))>0.5 || count==0
for col=length(old)
new=[mean(old(1:2)), conv(old,[1,1,1]/3,'valid'), mean(old(end-1:end))];
end
old=new;
count=count+1;
end
The trick is to force the first run with count==0 and then have the inversion of your original statement.
Jan
Jan 2018 年 2 月 2 日

0 投票

Use the debugger to step through your code line by line: https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html. This allows you to examine, what's going on in which command.

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2018 年 2 月 2 日

コメント済み:

2018 年 2 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by