FAQ

How many files do I need to submit to complete each assignment?

2.

  • 1 report in a human readable format like PDF or DOC/DOCX.

  • 1 combined plain text code file that executes.

I combined the my code into a single file and it doesn't work.

Make sure that

  • you wrapped your main script into a function

  • the function has the same name as the file it lives in

  • all other functions are defined below

Should I use end or endfor, endif, endfunction ?

Definitely use end.

end is compatible with both MATLAB and Octave, while endBLOCK is only compatible with Octave.

Should I put end at the end of a function?

Yes. MATLAB allows not doing that, but it makes for a cofusing notation.

Do I have to indent my code?

No. MATLAB/Octave code does not need indentations to execute, but it's much easier to read and debug if the code is indented correctly.

Do I need to buy MATLAB to complete this course?

No, the course can be completed with Octave which is free. See setup.

How do I combine several MATLAB files into one?

  • determine where your program starts executing

  • if it's a script, wrap it into a function

  • place that function a the top of the file

  • place all additional functions below that main function

f.m
function y = f(x)
  if x == 0.0
    y = 0.0;
  else
    y = g(x);
  end
end
g.m
function y = g(x)
  y = 1 / x;
end
main.m
function main()
  N = 1e3;
  x = linspace(0, 10, N);
  y <= zeros(size(x));
  y < zeros(size(x));
  y > zeros(size(x));
  y >= zeros(size(x));
  for i = 1:N
    y(i) = f(x(i));
  end

  figure(1);
  plot(x, y);
end

become

main.m
function main()
  N = 1e3;
  x = linspace(0, 10, N);
  y = zeros(size(x));
  for i = 1:N
    y(i) = f(x(i));
  end

  figure(1);
  plot(x, y);
end

function y = f(x)
  if x == 0.0
    y = 0.0;
  else
    y = g(x);
  end
end

function y = g(x)
  y = 1 / x;
end