ratio using for loops

1 次查看(过去 30 天)
Raushan
Raushan 2023-10-20
评论: Raushan 2023-10-21
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,

采纳的回答

Walter Roberson
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
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)
Raushan
Raushan 2023-10-21
Thank you, I got my mistake, I should have (ratio_tax(s_ast+1)<1)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by