代写代考 function [alg1,alg2,alg3,alg4] = compoundInterest(n,x)

function [alg1,alg2,alg3,alg4] = compoundInterest(n,x)
% compute C_n(x) = (1 + x/n)^n, the compound interest formula for
% annual interest rate x, compounded n times per year, by 3 different
% algorithms and print the results, using SINGLE precision.

Copyright By PowCoder代写 加微信 powcoder

% We only need to explicitly use SINGLE in the first operation
% in each algorithm: the rest are single by default, as can be verified
% by displaying the output args.
% Alg 1: repeated multiplication
z = single(1 + x/n); % 1 + single(x/n) is the same
% Alg 2: use the power operator
z = single(1 + x/n);
alg2 = z^n;
% Alg 3: use exp and log, since log(C_n(x)) = n log(1 + x/n)
z = single(1 + x/n);
v = log(z); % natural log, base e
alg3 = exp(n*v);
% Alg 4: use log1p
u = single(x/n);
v = log1p(u); % compute log(1+u) without computing 1+u first
alg4 = exp(n*v);

% the “\t” is a horizontal tab so the output lines up for different n values
% fprintf(‘n = %d \t Alg 1 = %g, Alg 2 = %g, Alg 3 = %g\n’,n,alg1,alg2,alg3)
fprintf(‘n = %d \t Alg 1 = %g, Alg 2 = %g, Alg 3 = %g, Alg4 = %g\n’,…
n,alg1,alg2,alg3,alg4)

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com