Sei sulla pagina 1di 4

MATLAB Programming for Numerical Computations

JanMarch 2016

Solutions to Assignment2
1. SMALLEST ERROR FOR DIFFERENTIATION
Code of smallestErrorDiff.m:
% Three point forward difference formula for finding f'(x) for tan^-1 (x)
% at x=1
x = 1;
h = 10.^[-2:-1:-10];
truediff = 1/(1+x^2);
fcalc = (-atan(x+2*h)+4*atan(x+h)-3*atan(x))./(2*h);
err = abs(truediff - fcalc);
[errmin, idx] = min(err);
disp(['Minimum error = ',num2str(errmin),' at h = ',num2str(h(idx))])

Final Result:
Minimum error = 3.2756e-12 at h = 1e-05
Result is: 1e-5
2. ITERATIVE METHOD FOR CUBE ROOT
Code of myCubeRoot.m:
function [x,n] = myCubeRoot(a,tol)
% Use iterative algorithm to compute cube root of a real number
% Inputs
%

Value for which cube-root is to be found

tol Tolerance up to which the result is to be found

% Outputs
%
%

x
n

Approximate value of cube root found


Number of iterations required by the algorithm

x = a/2;

% DO NOT CHANGE THIS!! This is initial guess.

% ----------YOU MAY START EDITING BELOW THIS LINE ---------xNew = 1/2 *(x+(a/x^2));
n =1;
while abs(x - xNew)>tol
x = xNew;
xNew = 1/2 *(x+(a/x^2));

MATLAB Programming for Numerical Computations

JanMarch 2016

n = n+1;
end
x = xNew;
end

Final Results:
>>[x,n] =

myCubeRoot(15.0,1.0e-4)

x =
2.4662
n =
12
>> [x,n] =

myCubeRoot(8.0,1.0e-2)

x =
2.0029
n =
7

3. MACLAURIN SERIES FOR TRIGONOMETRIC FUNCTIONS


Code of maclaurinCos.m (Method-1):
function [cosVal] = maclaurinCos(x,n)
% Template file for Assignment-2
% Compute approximation of cos(x) using MacLaurin
%

Series upto the n-th order term (x^n/n!)

% ----- DO NOT EDIT THIS PART OF THE CODE ----numerator = x.^[1:n];


denom = cumprod(1:n);
vec = [1, numerator./denom];
% ----- DO NOT EDIT ANYTHING ABOVE THIS LINE ----% PLEASE USE "vec" for your further calculations
% ---- YOU MAY START EDITING THE FUNCTION NOW ---cosVal = 0;
j = 0;
for i = 1:n+1
if(rem(i,2)~=0)
cosVal = cosVal + vec(i)*(-1)^j;
j = j+1;
end
end
end

MATLAB Programming for Numerical Computations

JanMarch 2016

Code of maclaurinCos.m (Method-2):


function [cosVal] = maclaurinCos(x,n)
% Template file for Assignment-2
% Compute approximation of cos(x) using MacLaurin
%

Series upto the n-th order term (x^n/n!)

% ----- DO NOT EDIT THIS PART OF THE CODE ----numerator = x.^[1:n];


denom = cumprod(1:n);
vec = [1, numerator./denom];
% ----- DO NOT EDIT ANYTHING ABOVE THIS LINE ----% PLEASE USE "vec" for your further calculations
% ---- YOU MAY START EDITING THE FUNCTION NOW ----

% Get only the terms required for cosVal in vector v1


v1 = vec(1:2:end);
cosVal = sum(v1(1:2:end)) - sum(v1(2:2:end));
end

(Last three lines in bold are the statements added as a part of this assignment. The first 12
lines were provided in the template file.)
Final Result:
>> cosVal = maclaurinCos(0.75,4)
cosVal =
0.7319
>> cosVal = maclaurinCos(3.0,9)
cosVal =
-0.9748

4. MACLAURIN SERIES FOR EXPONENTIAL


One limitation of MATLAB is that the array index starts with 1 and not 0. So, f(1) = 1 is
th

the 0 order term, f(2) = 1 + x is 1st order term, and so on. So, the order of exponent is one
less than the size of vector. This is same as what we saw in vec in previous problem.
Code of expMac.m:
% Maclaurin series for exponential
x = -1.2;
f(2) = 1+x;
f(1) = 1;
vec = [1 x];

MATLAB Programming for Numerical Computations

JanMarch 2016

n = 2;
while ( abs(f(n)- f(n-1))>5e-3)
numerator = x^n;
denom = factorial(n);
vec = [vec, numerator/denom];
f(n+1) = sum(vec);
n = n+1;
end
% The next command is required because f(1) is 0-th order term
n = n-1
expVal = f(end)

Final Result:
>> expMac
n =
6

(Note: n=7 will get partial credit)

expVal =
0.3018

Can you solve Problem-4 using Array Method?


It is also possible to solve this problem without using loops, using an array method. This
can be done in the code given below. This is based on a question asked in the forums. Recall
that the expVal calculated in videos contained all terms using array method. We simply use
the command diff to find the difference between consecutive elements.
Code of expMac.m:
% Maclaurin series for exponential
x = -1.2;
maxOrder = 10;
numerator = x.^[1:maxOrder];
denom = cumprod(1:maxOrder);
vec = [1, numerator./denom];
expVal = cumsum(vec);

% Note that this is what we did in the videos

err = abs(diff(expVal)); % This was what we mentioned in the forum help


n = find(err<0.005)

% This is the number of terms where err < tol

result = expVal(n+1)

% (n+1) because first element is 0-th order

Potrebbero piacerti anche