%% Homework #0
% First Last
% Math 365, Spring 2017
%% Introduction
% The purpose of this homework is to become familar with the Matlab publish
% command and to see how to turn in homework problems.
function hmwk0()
hmwk_problem(@prob1,'prob1');
file_check('torus.out');
hmwk_problem(@prob2,'prob2');
hmwk_problem(@prob3,'prob3');
hmwk_problem(@prob4,'prob4');
file_check('z.out');
hmwk_problem(@prob5,'prob5');
file_check('h.out');
hmwk_problem(@prob6,'prob6');
hmwk_problem(@prob7,'prob7');
hmwk_problem(@prob8,'prob8');
hmwk_problem(@prob9,'prob9');
end
%%%%%%%%%%%%%%%%%%%%%%% Homework utility routines %%%%%%%%%%%%%%%%%%%%%%
function hmwk_problem(prob,msg)
try
prob()
fprintf('%s : Ran to completion.\n',msg);
catch me
fprintf('%s : Something went wrong.\n',msg);
fprintf('%s\n',me.message);
end
fprintf('\n');
end
function write_file(data,filename)
save(filename,'data','-ascii','-double');
end
function file_check(fname)
if ~exist(fname,'file')
fprintf('WARNING! You have not written out file ''%s''\n',fname);
fprintf('Use function ''write_file'' to write out your data to a file\n');
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Problem #1 : Surface area of a torus
% In this problem, we compute the surface area of a torus whose
% inner radius is 3.21 and whose outer radius is 3.56.
% The result is saved to the file 'torus.out'
function prob1()
% Your work goes here
end
%% Problem #2 : Richter Scale
function prob2()
% Your work goes here
end
%% Problem #3 : Smartphones
function prob3()
% Your work goes here
end
%% Problem #4 : Vectorizing calculations
function prob4()
%%%
% Matlab makes it very easy to manipulate vectors and arrays. Many
% commands that would normally require a "for" loop can be "vectorized",
% creating code that is generally faster and more compact than
% non-vectorized equivalents.
% Create arrays x and y
% Use a "loop" to create a vector z = x + y
% ....
% Vectorize this loop!
% ....
% Store your z array to a file.
write_file(z,'z.out');
end
%% Problem #5 : Plot the graph of a function
function prob5()
%%%
% Create function handles for two functions and
% construct a third composite function.
% Create Anonymous function handles
f = @(x) sin(x);
% ...
% Construct a vector of equally spaced points using 'linspace'
% x = ...
% Plot the results; add a title and axes labels.
write_file(h(4.3),'h.out');
end
%% Problem #6 : Continued fraction approx. to pi
function prob6()
%%%
% Use a continued fraction to approximate $\pi$. Make sure that your
% code doesn't run off of the edge of the page.
%%%
% Publish allows you to insert mathematical expressions (although they
% don't show up so well in PDF).
% $\pi \approx d_1 + \frac{1}{d_2 + \frac{1}{d_2 + \frac{1}{d_3} ...}}$
% Your approximation goes here!
% pi_approx = ...
pi_approx = 3; % not correct!
fprintf('%15s %24.16f\n','pi (approx)',pi_approx);
fprintf('%15s %24.16f\n','pi (exact)',pi);
fprintf('%15s %24.4e\n','Error',abs(pi-pi_approx));
end
%% Problem #7 - Continued fraction approx. to e
function prob7()
% Your work goes here
end
%% Problem #8 : Loading data from a file
function prob8()
%%%
% Load data from a file and report the mean and standard deviation.
h = load('heights.dat');
fprintf('%12s %12d\n','Count',length(h));
fprintf('%12s %12.2f\n','Min',min(h));
% Add remaining computations here
% Create a histogram of the data.
hist(h,30);
end
%% Problem #9 : How to succeed in Math 365
function prob9()
%%%
% Publish allows you to create lists, use different font styles, and include
% preformatted code
%
% *How to succeed in Math 365*
%
% * _Always_ start your homework early
% * _Don't_ spend too much time googling for answers
% * _Read_ the
% !
%
% *Steps for getting help on homework problems.*
%
% # Read the Matlab tutorials available on the course website
% # Read lecture notes and demo codes on the online website.
% # Use Matlab online "help" system for help on Matlab commands.
% # Read the
% # Email the professor for help, if you can't find answers in the above.
% # Do not spend too much time with Prof. Google or Dr. YouTube. This
% is likely going to be a waste of time! Spend more time thinking about
% what you have learned in class, and debugging your own code.
%%%
% Include sample code that you don't want run by "formatting" the code
% like this. Use exactly three spaces between the percent sign and
% your code.
%
% curly = 4*pi;
% larry = sin(curly);
% moe = tan(curly + larry);
%
%%%
% There are lots of helpful hints for publishing by issuing the command
%
% >> doc publishing markup
end