loop function help please

1 view (last 30 days)
Vhugala  Raphunga
Vhugala Raphunga on 29 Mar 2020
Answered: VBBV on 28 Nov 2021
Use for loops to give the values of the following function: f(x) = 3x2 + 5x – 7 x < 0 3x + 4 x >= 0
for -10 <= x <= 10 in steps of 1

Answers (2)

David Hill
David Hill on 29 Mar 2020
x=-10:10;
for k=1:length(x)
if x(k)<0
f(k)=3*x^2+5*x-7;
else
f(k)=3*x+4;
end
end

VBBV
VBBV on 28 Nov 2021
x=-10:1:10;
for i=1:length(x)
if x(i)<0
F(i)=3*x(i)^2+5*x(i)-7;
else
F(i)=3*x(i)+4;
end
end
plot(x,F)
try this

Community Treasure Hunt

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

Start Hunting!