%% Homework Tips for Math 365
% These are the homework tips for doing Math 365 homework
%% Introduction
% Below are several tips that will help you do your homework.
format short;
%% #1 - Physical constants vs. decimal expansions
% Do not "hardwire" approximations when Matlab can compute them to full
% precision. A rule of thumb for this course is, *You should never need
% your calculator in this course*!
%%%
% *Example:* Compute the area of a triangle wth sides A=5.1 and B=3.2 and
% enclosed angle theta = 20 degrees.
% This is the correct way to solve this problem :
theta = 20*pi/180;
sin_theta = sin(theta); % Good : Let Matlab compute the sin(th) for you.
A = 5.1;
B = 3.2;
Area = 0.5*A*B*sin_theta;
fprintf('Area = %24.16f\n',Area);
%%
% This is the wrong way!
sin_theta = 0.3420; % Where did this number come from? A calculator?
A = 5.1;
B = 3.2;
Area = 0.5*A*B*sin_theta;
fprintf('Area = %24.16f\n',Area);
%%
% Do you notice the difference in the values?
%%
% Especially don't "hardwire" numbers if
%
% * You don't tell me where your hardwired value came from.
%
% * You supply very few digits of a value that we know to full precision.
%
% * You don't indicate how accurate your value is. For example, you might
% get values from a CAD package, and report them to full precision (e.g. 16
% digits). But in fact, it is unlikely that a CAD package can give you
% that much precision. Or if it does, you may well have been able to
% compute the same values in Matlab.
%
% Examples of the above errors are below.
% The exact area is
area_exact = 1.55678393345; % where did this come from?
% The value of e, obtained from my calculator is
e_const = 2.7183; % Much better to use e_const = exp(1)
% These numbers appear to be very "accurate", but who really knows?
x = [1.2348454366677766; 4.556667841225998]; % Really?
y = [4.5677445639358711; -2.345664448941219];
%%
% The obvious exceptions to this rule are when you are asked to supply
% phyiscal constants, such as the acceleration of gravity, a thermal
% conductivity, or some other physical constant in the problem that is only
% given to a limited number of digits. In the above examples, we were
% given the length of the two sides of the triangle to only a single digit.
% As far as this problem is concerned, then, these are the "exact" values
% for these quantites. Here are a few more examples.
g = 9.81; % m/s acceleration due to gravity
beta = 1.2e-4; % Thermal conductivity.
%% #2 - Formatting output
% Don't use the 'disp' command to show your results. Most likely, you
% won't show enough digits.
two_times_pi = 2*pi;
disp(two_times_pi)
%%
% The formatting will depend on the current setting for the last format
% statement you issued, which may or may not be precisely what you want.
% That said, it is rarely a good idea (and generally considered lazy!) to
% use the 'format' command anywhere other than at the command line prompt.
%%
% Instead, use the 'write_file' function to save your results to a file.
% You can check the results in the file by using the 'type' command.
write_file(two_times_pi,'two_times_pi.out');
type two_times_pi.out
%% #3 - Line continuation '...'
% Don't let your homework text exceed the 76 character limit. You can see
% this limit as a thin line near the right edge of the Matlab editor. Don't
% let your code or comments go past this line. To continue your code from
% one line to the next, use the '...' line continuation characters, as the
% example below shows.
x = [1,2,3,4,sin(10), linspace(1,100), rand(1,10), 100004, sin(56), ...
67.5678, atan(-1), sin(456.7)];
%% #4 - Typeset any explanatory text
% Typeset any explanatory text that explains your method, or a particular
% approach that you took.
%%
% This text is typeset (using, for example, a Times Roman font). Notice
% that by typesetting, I can use *bold* and _italics_ in my explanations.
% This text is not typeset and will appear as a comment in the code.
%% #5 - Don't cut and paste from a PDF
% Do not cut and paste PDF text into your homework script. You run the
% risk of introducting hidden characters into your code or comments, and
% Publish may not work.
%% #6 - Writing scripts and functions
% Generally, it is not a good idea to include 'clc' or 'format' inside of
% scripts or functions. These are used mostly at the command line, and
% unless you have a good reason for including them they should be reserved
% for command line use only. This holds for inside of functions as well.
% It is also not a good idea to include a 'clear all' statement at the top
% of functions. The reason for this is that you may end up clearing any
% input arguments that are passed into the function. Even if you don't
% have any input arguments, there is no need for clear all, since you have
% essentially a 'clear' workspace inside of the function. On the other
% hand, the reason for including these commands in scripts is to clear out
% any workspace variables that may interfere with the variable names in the
% script.
% What you may want to include in a script or function is the 'close all'
% command. This will close all figure windows. Note the difference
% between this and 'clf', which only closes the current figure window,
%% #7 - Scientific notation
% Be sure to take advantage of scientific notation and use it to enter
% physical constants that are most conveniently expressed as powers of 10.
% For example,
% This is the correct (and *easier*) way to input powers of 10.
S = 1e4;
b = 1e-3;
e = 5e-3;
% instead of
S = 10^4;
b = 1*10^-3;
e = 5*(10^(-3)); % Too many parentheses!
%% Try publishing your own code
% You can make sure that your code runs publishes using the command
%
% file = publish('homework_tips.m','pdf');
% open(file);
%
% I will run your program using essentially this command, and this is what
% I will be grading. Warning : Do not try to include this command in the
% file you are publishing - this will likely crash Matlab!