First of all, two quick corrections to the first lecture:
1) As I suspected, the newline character in fprintf format
specification is "\n", not "/n"
2) To make the fprintf work properly with lists, you should use
fprintf("%g %g \n", [x; y])
not fprintf("%g %g \n", x, y) as I've written in class: this will
ensure that the print statement cycles properly through the x, y data
pairs (the way I've written it, first all x-values are printed,
followed by all y-values, instead of printing the pairs of values - we
don't want that to happen).
Here is a correct example for making a table:
i=1:10;
x=0.1:0.1:1.0;
y=sin(x);
fprintf('No. x sin(x)\n'); fprintf('%2d %3g %8g\n',[i;x;y]);
In fact, you can cut-and-paste these four lines directly into your
MATLAB command window, and see the result.
The corresponding table using the "for" loop would look like this:
for i=1:10
x = 0.1 * i;
fprintf('%2d %8g %8g \n', i, x, sin(x))
end
Note that in the above two examples I am specifying the total width of
the printed values, even for the integer, and not the number of
decimals following the decimal point; this way I ensure that the
columns line up nicely.