How to code "while" loop for the following problem?

hello friends. I want to write "while" loop for the follwing problem but it don't give the answer which i want. ii starts from 0. The system which i want is like that.
3.749893 , 3.749893 + ii(0) = ii1
5.217726 , ii1 + 5.217726 = ii2
0.412081 , ii2 + 0.412081 = ii3
0.113385 , ii3 + 0.113385 = ii4
1.27062 , ii4 + 1.27062 = ii5
2.740936 , ii5 + 2.740936 = ii6
2.208374 , ii6 + 2.208374= ii7
so on..
I cant code this proble in while loop.
Thanks in advance.
ii = 0;
for sun = 1:1:p ;
if load(sun,1) < pv (sun,1);
h = pv(sun,1)-load (sun,1);
n(sun,1) = double (h); % (PV'den artan Enerji)
deger = sum (n); % (Artan Enerjinin toplam degeri)
Q = find (n > 0);
answer = n(Q);
WW= answer / 4;
k = 1;
while ii < deger ;
g2 = ii + WW;
ii = g2;
end
end

4 件のコメント

infinity
infinity 2019 年 7 月 14 日
Hello,
How about if you put "ii = 0;" inside the for loop? Since when "sun" increases, ii is not reset to 0. Is it what you realy want to do?
Mohammad Sulaiman Stanekzai
Mohammad Sulaiman Stanekzai 2019 年 7 月 14 日
Thak you, I tried ii = 0 inside the for loop but it doesn't work.
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 7 月 14 日
編集済み: KALYAN ACHARJYA 2019 年 7 月 14 日
Sorry, still the question is not clear for me.
Mohammad Sulaiman Stanekzai
Mohammad Sulaiman Stanekzai 2019 年 7 月 14 日
編集済み: Mohammad Sulaiman Stanekzai 2019 年 7 月 14 日
3.749893
5.217726
0.412081
0.113385
1.27062
2.740936
2.208374
above values are belong to WW varaible in ''while'' loop.
I want to add them like;
ii + 3.749893 = ii1 here the first value of ii is '0'
ii1 + 5.217726 = ii2 here the value of ii1 is (3.749893 + 0)
ii2 + 0.412081 = ii3 here the value of ii2 is (ii1 + 5.217726)
ii3 + 0.113385 = ii4 here the value of ii3 is (ii2 + 0.412081)
ii4 + 1.27062 = ii5 here the value of ii4 is ( ii3 + 0.113385)
ii5 + 2.740936 = ii6 here the value of ii5 is ( ii4 + 1.27062)
ii6 + 2.208374= ii7 here the value of ii6 is ( ii5 + 2.740936)
and so on.
I hope you understand what i mean.

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

 採用された回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 7 月 14 日
編集済み: KALYAN ACHARJYA 2019 年 7 月 14 日

1 投票

ww=[3.749893
5.217726
0.412081
0.113385
1.27062
2.740936
2.208374];
% Here considering sample ww only
count=1;
result=0;
while count<=length(ww)
result=ww(count)+result;
count=count+1;
end
disp(result);

12 件のコメント

Mohammad Sulaiman Stanekzai
Mohammad Sulaiman Stanekzai 2019 年 7 月 14 日
編集済み: Mohammad Sulaiman Stanekzai 2019 年 7 月 14 日
Thank you Sir. it just provide me the last answer but i need answer of each process.
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 7 月 14 日
編集済み: KALYAN ACHARJYA 2019 年 7 月 14 日
while count<=length(ww)
result=ww(count)+result;
fprintf('\n The Result with interation number %d and result: %f',count,result);
count=count+1;
end
Mohammad Sulaiman Stanekzai
Mohammad Sulaiman Stanekzai 2019 年 7 月 14 日
I don't know what is wrong but it is not working.
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 7 月 14 日
ww=[3.749893
5.217726
0.412081
0.113385
1.27062
2.740936
2.208374];
count=1;
result=0;
while count<=length(ww)
result=ww(count)+result;
fprintf('\n The Result with interation number %d and result: %f',count,result);
count=count+1;
end
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 7 月 14 日
Result:
The Result with interation number 1 and result: 3.749893
The Result with interation number 2 and result: 8.967619
The Result with interation number 3 and result: 9.379700
The Result with interation number 4 and result: 9.493085
The Result with interation number 5 and result: 10.763705
The Result with interation number 6 and result: 13.504641
The Result with interation number 7 and result: 15.713015>>
Mohammad Sulaiman Stanekzai
Mohammad Sulaiman Stanekzai 2019 年 7 月 14 日
'\n The Result with interation number %d and result: %f'
what should i write here? sir
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 7 月 14 日
編集済み: KALYAN ACHARJYA 2019 年 7 月 14 日
If you dont want, do this one
while count<=length(ww)
result=ww(count)+result
%.....................^ so semi colon, the result reflect on command window
count=count+1;
end
Mohammad Sulaiman Stanekzai
Mohammad Sulaiman Stanekzai 2019 年 7 月 14 日
Thak you sir for everything it worked perfectly and it shows all result in command window. My last question is, how to take all this result in a list like in the following image?hgbgj.GIF
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 7 月 14 日
編集済み: KALYAN ACHARJYA 2019 年 7 月 14 日
result_data=[];
while count<=length(ww)
result=ww(count)+result
result_data(count+1)=result;
%.....................^ so semi colon, the result reflect on command window
count=count+1;
end
Mohammad Sulaiman Stanekzai
Mohammad Sulaiman Stanekzai 2019 年 7 月 14 日
thank you so much :)
Mohammad Sulaiman Stanekzai
Mohammad Sulaiman Stanekzai 2019 年 7 月 14 日
編集済み: Image Analyst 2019 年 7 月 14 日
Hello Sir, i have a question if could help me please,
i have values array like
Y = [24.1013520547196
24.1013520547196
24.1013520547196
24.1013520547196
5.56899020519836
70.5900447343365
74.5811543813954 ]
This time ii = 11698.56
ii - 24.1013520547196 = ii1 here the first value of 'ii' is '11698.56'
ii1 - 24.1013520547196 = ii2 here the value of ii1 is (ii - 24.1013520547196)
ii2 - 24.1013520547196 = ii3 here the value of ii2 is ( ii1 - 24.1013520547196)
ii3 - 24.1013520547196= ii4 here the value of ii3 is (ii2 - 24.1013520547196)
ii4 - 5.56899020519836 = ii5 here the value of ii4 is ( ii3 - 24.1013520547196)
ii5 - 70.5900447343365 = ii6 here the value of ii5 is ( ii4 - 5.56899020519836)
ii6 - 74.5811543813954= ii7 here the value of ii6 is ( ii5 - 70.5900447343365 )
and so on.
how to code it in while loop?
Thanks in advance.
Image Analyst
Image Analyst 2019 年 7 月 14 日
編集済み: Image Analyst 2019 年 7 月 14 日
How about
ii = 11698.56
index = 2;
while index <= length(Y)
ii(index) = ii(index - 1) - Y(index - 1)
index = index + 1;
end

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by