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

1 visualización (últimos 30 días)
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 comentario
Dyuman Joshi
Dyuman Joshi el 8 de Jun. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Alan Stevens
Alan Stevens el 8 de Jun. de 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)
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

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by