ratio using for loops
1 次查看(过去 30 天)
显示 更早的评论
I am still trying to solve it.
for s_ast=(0:1:T)
ratio_1=(y_2^(s_ast+1)*(1-y_2^T)*(1-x_2)*epsilon_2^(s_ast+1))/(x_2^(s_ast+1)*(1-x_2^T)*(1-y_2));
ratio_2=((1-y_1^(s_ast+1))*(1-x_1)*epsilon_1^(s_ast+1))/((1-y_1)*((1-x_1^(s_ast+1)+K^(1/gamma)*x_1^T*(1-x_1))));
ratio_tax(s_ast+1)=ratio_1/ratio_2;
if (ratio_tax>1)
s_asterisk_tax=s_ast(end);
else
continue
end
end
disp(s_asterisk_tax);
As suggested, I simplified using geometric series and I excluded when x_1 and x_2, y_1 and y_2 are equal to 0.
However, I am getting s_asterisk_tax wrong.
What is wrong in my code?
Thank you,
0 个评论
采纳的回答
Walter Roberson
2023-10-20
编辑:Walter Roberson
2023-10-20
if ratio_tax > 1
s_asterisk_tax = s_ast;
break;
end
And make sure you put in protection for the case that you get through all the s_ast values without the ratio ever being > 1.
3 个评论
Walter Roberson
2023-10-20
ratio_satisfied = false;
for s_ast=(0:1:T)
ratio_1=(y_2^(s_ast+1)*(1-y_2^T)*(1-x_2)*epsilon_2^(s_ast+1))/(x_2^(s_ast+1)*(1-x_2^T)*(1-y_2));
ratio_2=((1-y_1^(s_ast+1))*(1-x_1)*epsilon_1^(s_ast+1))/((1-y_1)*((1-x_1^(s_ast+1)+K^(1/gamma)*x_1^T*(1-x_1))));
ratio_tax(s_ast+1)=ratio_1/ratio_2;
if (ratio_tax>1)
s_asterisk_tax = s_ast;
ratio_satisfied = true;
break;
end
end
if ratio_satisfied
disp(s_asterisk_tax);
else
disp('got through all iterations without the ratio being satisfied');
end
Note in particular that you were using s_ast(end) in your assignment to s_asterisk_tax . However, s_ast is the index variable in your for s_ast and as such it is assigned one value at a time. At any time within the for loop, s_ast is a scalar, so it does not make any sense to index s_ast(end) . It is not "wrong" to do so, but it indicates that you are confused about what you are doing, and it confuses people who read the code, so you should avoid the unnecessary (end)
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!