Repeating for loop until getting a list of solution

Hi to all
I'm not that good in Matlab and I tried to learn it. I used for loop to create a solution for each value of (n) and repeating the for loop. The for-loop is from 1 to 8760 while n is from 0 to 1 with increament of 0.1.
I want to repeat the for-loop for each (n) and finally make a vecto of [n, solution from for-loop]. Attached flow chart may explain more about what I want.

6 件のコメント

Abdullah Al Shereiqi
Abdullah Al Shereiqi 2020 年 3 月 17 日
More clear flow chart attached
Sriram Tadavarty
Sriram Tadavarty 2020 年 3 月 17 日
Can you place the code you tried so far and the issue that came up?
Rik
Rik 2020 年 3 月 17 日
Have a read here and here. It will greatly improve your chances of getting an answer.
It seems you need something like this, but I'm not sure what you are trying to do exactly.
n=0;
for k=1:8760
n=SomeFunction(k);
if n==1
break
else
n=n+0.1;
end
end
Abdullah Al Shereiqi
Abdullah Al Shereiqi 2020 年 3 月 17 日
編集済み: Rik 2020 年 3 月 17 日
Thanks Saruram and Rik for quick support.
I tried to run the code as per Rik comment. But, the value of (s) reach beyond 1 (more than 1000)!!
s=0;
for apv = 1:length(P_wind)
if P_PV(apv)==0 ;
N_PV_1(apv)=0;
else
N_PV_1(apv)= ceil (((s*P_ref(apv)))./(P_PV(apv)));
end
if s==1;
break
else
s = s+0.01
end
end
Rik
Rik 2020 年 3 月 17 日
Apart from the suggestion from Guillaume; I forgot to account for floating point rounding errors.
s=0;
for k=1:10
s=s+0.1;
end
clc
fprintf('%.53f\n',s) %not quite equal to 1
The solution in this case is to use a tolerance:
s==1 %returns false, because s is 0.99999999999999988897769753748434595763683319091796875
abs(s-1)<=2*eps %returns true
Sriram Tadavarty
Sriram Tadavarty 2020 年 3 月 17 日
You could even break when s is greater than 1, example, the condition of if s > 1, can be placed, rather than s == 1 to deal with tolerance issues.
Or it can be directly placed a loop for 100 times, in each time 0.01 can be added.

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

回答 (2 件)

Guillaume
Guillaume 2020 年 3 月 17 日

0 投票

If it's a flow chart you've been given, then you are entitled to complain loudly. That flow chart is very misleading. The layout is a more representative of a while loop than a for loop.
This is what you're meant to do:
for n = 0:0.1:1 %represented by 4 boxes on the flow chart! (n = 0, for-loop, n=1?, increment by 0.1)
%do calculation inside the loop (1 box on the chart)
end
%get a vector of solution (1 box on the chart)
Abdullah Al Shereiqi
Abdullah Al Shereiqi 2020 年 3 月 17 日

0 投票

Thanks to all of you; Saruram , Rik and Guillaume.
It seems working fine after using another for-loop (100 times).
Appreciated your support and guidance

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

質問済み:

2020 年 3 月 17 日

回答済み:

2020 年 3 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by