Você está na página 1de 3

Solutions to Assignment 2

Problem 1: Smallest Error in Differentiation


The MATLAB code to solve the problem is given below:
f=@(x) x*exp(x);
a=1;
trueVal=(1+a)*exp(a);
fwdDif=@(a,h) (3*f(a)-4*f(a-h)+f(a-2*h)) / (2*h);
for i=2:10
h=10^-i;
numVal=(3*f(a)-4*f(a-h)+f(a-2*h)) / (2*h);
err=abs(trueVal-numVal);
disp([num2str(h), ' --> ', num2str(err)]);
end

The above MATLAB code gives the following output:


0.001 --> 3.621e-06
0.0001 --> 3.6232e-08
1e-05 --> 2.8251e-10
1e-06 --> 1.2274e-09
1e-07 --> 2.3376e-09
1e-08 --> 1.6443e-07

As you can see from the bolded line above, the answers for the questions can be obtained as
1. Step-size that gives minimum error is 10#$ .
2. The minimum value of error obtained at that value is 2.83 × 10#*+ .

Problem 2: MacLaurin Series for Sine


The function to calculate MacLaurin Sine is given below:
function [sinVal] = maclaurinSine(x,n)
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 ----
sinVal=sum(vec(2:4:end)) - sum(vec(4:4:end));
end
The line in bold is the only one that I have added. The following are the solutions to the two
questions in this problem
3. The result of sinVal(1.2,16) is 0.9320.

The second example:


4. The result of sinVal(3.1,9) is 0.0476.
Several students wrote to us saying that they got the answer as -0.0253. This is because the
actual value is given by:
𝑥4 𝑥$ 𝑥8 𝑥:
sin(𝑥) = 𝑥 − + − +
3! 5! 7! 9!
The reason several of you got incorrect answer is because in the calculation of “sinVal”,
you have used: sum(2:4:n). Note that vector is given by:
𝑥@ 𝑥4 𝑥B
𝑣𝑒𝑐 = ?1 𝑥 ⋯ D
2! 3! 𝑛!
Hence, there are n+1 elements in the vector. So, the last number should be “n+1”.

This problem was formulated in such a way on purpose. If you ended the sum with “n”, you
will get 50% of credit and not 100%.

If you got 50% of marks by solving the problem on your own, you are on the right track.

Problem 3: Infinite Series


The code for n=100 is given below:
n=100;
S=sum([1:n]./[2:n+1]);
disp(S);
Please modify the value of n and run the code to get the required results.

Alternatively, you could also use the following loop to solve all of this in one go:
for n=[100, 250, 500, 1000]
S=sum([1:n]./[2:n+1]);
disp(S);
end

The results obtained are as follows:


5. n=100 à S=95.8
6. n=250 à S=244.9
7. n=500 à S=494.2
8. n=1000 à S=993.5
Problem 4: Step-wise Computation using MacLaurin Series
I am giving a cryptic solution (to show the power on MATLAB). Can you understand it?
Several of you may not. Keep this solution for future and refer back to it after you become
more expert in MATLAB.
x=0.75;
trueVal=1/(1-x);
iVal=1+cumsum(x.^[1:100]);
err=abs(trueVal-iVal);
i1=find(err<0.02,1,'first');
disp([i1,iVal(i1)]);
i2=find(err<0.001,1,'first');
disp([i2,iVal(i2)]);

The solutions obtained are:


9. For tol=0.02, the result of 3.983 is obtained (in 18 iterations)
10. For tol=0.001, it takes 28 iterations (to reach the result of 3.999)

Let us also write a regular code for this:


x=0.75;
trueVal=1/(1-x);

% Tolerances and errors


tol=0.001;
i=0;
err=1; % Required to ensure MATLAB gives no error
% Initialization
fTerm=1; % Individual term
fVal=1; % Total value

% Computation
while(err>tol)
fTerm=fTerm*x; % This gets x^i term
fVal=fVal+fTerm;
err=abs(trueVal-fVal);
i=i+1;
end
% Display results
disp([i, fVal]);

Você também pode gostar