Start with the smallest possible problem to understand, what the loops do.
Please run this in your command window. Does this explain, what the loop does?
WHILE and FOR loops are equivalent:
FOR loops are useful, if you know in advance how many iterations you will get. If this is not clear, use a WHILE loop:
x = input('Type in a number: ');
Now to your code:
[ and ] are the Matlab operator for a concatenation. -5:0.1:5 is a vector already and you concatenate it with nothing. So x=-5:0.1:5 is sufficient already.
y(x) means the x.th element of y. Therefore x must be a positive integer, but this is not the case e.g. for -5 or -4.9 . David has explained this already. One of the solutions:
y(k) = a * x(k) .^ 2 + b * x(k) + c;
You can do this without a loop in Matlab also:
y = a * x .^ 2 + b * x + c;
But I assume, the idea of this homework is to learn how to use loops.
Best Answer