Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
Sandra
Sandra le 8 Juin 2023
Commenté : Dyuman Joshi le 8 Juin 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:
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 commentaire
Dyuman Joshi
Dyuman Joshi le 8 Juin 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.

Connectez-vous pour commenter.

Réponse acceptée

Alan Stevens
Alan Stevens le 8 Juin 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

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by