Using Inequality in a For loop

I am attempting to perform a sensitivity analysis on an inequality of two variables, of the form: 4x < y < 40x. I want to use a for loop to make an array of y values for each incremental change in x, and create a plot to visualize the values of y for each x. My biggest issue has been representing the inequality within the for loop, here is the code that I have so far:
%% Sensitivity Analysis
D_rs = 8; %Ratio of D_85(s) and D_15(s), for any value of each
i=0;
for D15_s = 0:0.1:2
4*D15_s < D15_f < (5*D_rs)*D15_s; % <<<issue with representing this inequality>>>
i=i+1;
D15_sg(i)=D15_s; % storing the varied input D15_s in an array
D15_fg(i)=D15_f; % storing the output variable D15_f in an array
end
plot(D15_sg,D15_fg)
xlabel('-')
ylabel('-')
title('-')

 採用された回答

Les Beckham
Les Beckham 2020 年 4 月 11 日

1 投票

Walter has correctly shown how to express mutiple inequality conditions in Matlab (you must include the & (and) operator). However, even replacing that line of code with the correct syntax results in no effect on the rest of your code. The conditional will 'return' a logical value but you are doing nothing with the result.
Are you trying to apply upper and lower limits to D15_f?
If so, perhaps this is what you mean:
D15_f = max(D15_f, 4*D15_s); % apply the lower limit (max returns the larger of the two numbers)
D15_f = min(D15_f, (5*D_rs)*D15_s) % apply the upper limit (min returns the smaller of the two numbers)
If you replace your 'inequality' line by this code, you will be forcing D15_f to stay between these limits.
The rest of your code looks OK as far as I can tell.

4 件のコメント

Ruben
Ruben 2020 年 4 月 11 日
When I run the code with this change, I get this error: Undefined function or variable 'D15_f'. Also to clarify, I am attempting to plot the possible valves of "D15_f" within the specified range given by the inequality against each value of "D15_s" generated in the for loop.
%% Sensitivity Analysis
% input data
D_rs = 8; %Ratio of D_85(s) and D_15(s), for any value of each
i=0; % a counter for the arrays
for D15_s = 0:0.1:2
D15_f = max(D15_f, 4*D15_s); % apply the lower limit (max returns the larger of the two numbers)
D15_f = min(D15_f, (5*D_rs)*D15_s); % apply the upper limit (min returns the smaller of the two numbers)
i=i+1; % increasing the counter by 1
D15_sg(i)=D15_s; % storing the varied input D15_s in an array
D15_fg(i)=D15_f; % storing the output variable D15_f in an array
end
% Output data
%plot(D15_sg,D15_fg)
%xlabel('-')
%ylabel('-')
%title('-')
Les Beckham
Les Beckham 2020 年 4 月 11 日
I guess that I assumed that this was defined before your loop.
If you are just trying to find out and visualize what the limits are as a function of D15_s, this should do that:
%% Sensitivity Analysis
% input data
D_rs = 8; %Ratio of D_85(s) and D_15(s), for any value of each
D15_s = 0:0.1:2; % input
% Calculate min and max values
D15_f_min = 4*D15_s;
D15_f_max = (5*D_rs)*D15_s;
% Output data
plot(D15_s, D15_f_min, D15_s, D15_f_max)
xlabel('D15_s', 'Interpreter', 'None')
ylabel('D15_f', 'Interpreter', 'None')
legend('min', 'max', 'Location', 'best')
Ruben
Ruben 2020 年 4 月 11 日
This is more or less what I'm looking for, thank you for the assistance!
Les Beckham
Les Beckham 2020 年 4 月 11 日
You are welcome. Glad I could help.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 4 月 11 日

1 投票

4*D15_s < D15_f & D15_f < (5*D_rs)*D15_s

カテゴリ

質問済み:

2020 年 4 月 10 日

コメント済み:

2020 年 4 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by