Clear Filters
Clear Filters

indexing in the nested loop

5 views (last 30 days)
Raushan
Raushan on 4 Oct 2023
Commented: Raushan on 4 Oct 2023
sum_y_2_s=0;
sum_x_2_s=0;
for s=0:1:T-1
sum_y_2_s(s)=sum_y_2_s;
sum_x_2_s(s)=sum_x_2_s;
for z=s:1:T-1
sum_y_2_s(s)=sum_y_2_s(s)+y.^(z-s);
sum_x_2_s(s)=sum_x_2_s(s)+x.^(z-s);
ratio_1=(sum_y_2_s(s)./(sum_x_2_s(s)+K^(1/gamma)*x.^(T-s)));
end
if ratio_1<1
s_asterisk=s(end);
return
end
end
How should I index loop correctly. It shows
Array indices must be positive integers or logical values.
Error in opt_ret_positive_wealth (line 18)
sum_y_2_s(s)=sum_y_2_s
I am trying to find this ratio.
Thank you very much

Accepted Answer

Torsten
Torsten on 4 Oct 2023
Edited: Torsten on 4 Oct 2023
sx = 0;
sy = 0;
for s = T-1:-1:0
sx = sx + x^(T-1-s);
sy = sy + y^(T-1-s);
ratio(s+1) = sy/(sx+K^(1/gamma)*x^(T-s));
end
  3 Comments
Torsten
Torsten on 4 Oct 2023
The answer has changed. If x or y equal 1, you get a division by zero from the old code. The code above should cover all possible cases for x,y > 0. You should compare the results from both codes.
Raushan
Raushan on 4 Oct 2023
I got you, thank you

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!