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

1 view (last 30 days)
Sandra on 8 Jun 2023
Commented: Dyuman Joshi on 8 Jun 2023
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:
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
Dyuman Joshi on 8 Jun 2023
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 on 8 Jun 2023
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)
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

### Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!