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

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Loops 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