a function just valid over a certain range

12 ビュー (過去 30 日間)
Satriyo
Satriyo 2023 年 7 月 31 日
コメント済み: Satriyo 2023 年 7 月 31 日
Hello everyone, let me ask
a formula is expressed mathematically as follows
Y = x^2 + 3x + 1 , for 2<x<8;
Y = 0 for else (x<2 or x>8)
then I would plot Y value for Y within 0<x<10.
in excel I succeeded by writing the following formula
=IF(AND(x >= 2, x<=8), x^2+3x+1, 0)
with similar logic, I write this in Matlab
x = 0:0.1:10 ;
if 2 <= x <= 8
y = x.^2+3.*x+1 ;
else
y = 0
end
plot(x,y)
please see the graph above, which for the value of Y at x < 2 is not 0
  1 件のコメント
Stephen23
Stephen23 2023 年 7 月 31 日
The simple MATLAB approach is to use logical indexing:
x = 0:0.1:10;
y = x.^2+3.*x+1;
y(x<2 | x>8) = 0;
plot(x,y)

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

採用された回答

VBBV
VBBV 2023 年 7 月 31 日
x = 0:0.1:10 ;
for k = 1:length(x)
if x(k) >= 2 & x(k)<= 8
y(k) = x(k).^2+3.*x(k)+1 ;
else
y(k) = 0;
end
end
plot(x,y)
  2 件のコメント
VBBV
VBBV 2023 年 7 月 31 日
Use the logical condition to check for values lying between 2 and 8 as below
if x(k) >= 2 & x(k)<= 8
Satriyo
Satriyo 2023 年 7 月 31 日
Thanks for your answer.
Best Regards,

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

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by