MATLAB: For a range of x, say x=10, i need for x=1 to 3, y= f(x); for x= 4 to 7, y=g(x); for x=8 to 10, y=h(x); and how to present graph of x and y with x=1 to 10 using matlab coding

for loop

x=1:1:10;
for z=1:length(x);
if x(z)>=1 & x(z)<3,
y=2*x;
plot (x,y);
else if x(z)>=3 & x(z)<=7
plot (x,y);
y=4*x;
else if x(z)>7 & x(z)<=10
y=5*x;
plot (x,y);

Best Answer

There are many ways to make your code neater, but for minimal changes to what you have done, you need to assign results to an array e.g.
y(z) = 2*x(z);
and just plot once at the end. It isn't the neatest way to solve the problem, but in keeping with your use of a for loop approach at least. I'm never sure in these cases whether provided an entirely different neater solution is in any way helpful rather than just suggesting the changes needed to make your own solution work. It depends where you are in the learning process.
Incidentally, you might as well just use x directly rather than creating z = 1:length(x) in this case since that will give the exact same vector as x.
Related Question