フィルターのクリア

Solving a system of inequalities using an eigenfunction and if, else.

1 回表示 (過去 30 日間)
Sandra
Sandra 2023 年 6 月 8 日
コメント済み: Dyuman Joshi 2023 年 6 月 8 日
Hi, I have a task to do, but something is wrong here.
What I need to do: Make and test function for x=1,...,10, f(x) is in photo, and make a plot for it.
This is what I done:
function [y]=Zad1_3(x)
r1=x^2-2;x;
r2=5,x;
r3=x+3,x;
for x = 1:10
if (r1<0)
y=x^2-2,x
elseif (r2==0)
y=5,x
elseif (r3>0)
y=x+3,x
end
end
  1 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 6 月 8 日
I don't understand why you have put x after r1, r2, and r3 and in if-else statements.
And why create an extra variable when you can directly compare x according to the conditions?
Also, when you use x as the index for for loop, you overwrite the input that was provided to the function. And the final output y will correspond to x = 10, regardless of what you will input.

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

採用された回答

Alan Stevens
Alan Stevens 2023 年 6 月 8 日
Your function needs to be more like this, for example. Set values of x outside the function, call the function with these values, assign the results to y (say) and plot(x,y)
function f = Zad1_3(x)
for i = 1:numel(x)
if x(i) < 0
f(i) = x(i).^2 - 2;
elseif x(i) == 0
f(i) = 5;
else
f(i) = x(i) + 3;
end
end
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by