% Solutions to Math 230 hour exam 1.


%% Question 1. Write a Matlab expression to calculate and display the value
% of a sum of powers of x (see exam for details).
%
% The simplest answer is an expression using the factorial function to
% calculate factorials. Note there is no semicolon at the end, to ensure
% that Matlab displays the value.

1 - x^2 / factorial(2) + x^4 / factorial(4)


%% Question 2. Plot a square around a line.
%
% This is the code I used to generate the image in the exam. But note that
% the exam question specifically told you that you didn't need the "axis"
% commands at the end.

lineXs = [ -1, 1 ];
lineYs = [ 0, 0 ];

squareXs = [ -2, -2, 2, 2, -2 ];
squareYs = [ -2, 2, 2, -2, -2 ];

plot( lineXs, lineYs, 'b', squareXs, squareYs, 'r' );
axis( [ -3, 3, -3, 3 ] );
axis square;


%% Question 3. Write a function definition and a call on it.

%% Part A. See separate file "q3a.m" for the required function definition.

%% Part B. A call on the function simply consists of its name with the
% arguent value (1) in parentheses. Not ending with a semicolon displays
% the result.

q3a( 1 )


%% Question 4. Generate a table of values of an investment over various
% time spans.

% I used "meshgrid" to generate matrices that could pair all amounts with
% all time spans, then array arithmetic to evaluate the net value
% expression for all the pairs.

years = 100 : 1000;
amounts = 1000 : 2000;

[ Y, A ] = meshgrid( years, amounts );

values = A .* 1.01 .^ Y;