Reputation: 21
I am preparing for a written test in numerical analysis.
There are multiple-answer questions of the type:
The code below gives a print-out closest to...
N=100;
dt=0.5/N;
x=1;
for n=1: N-1
x=x+dt*x*x;
end
display(x);
Correct option: 2
The code below gives a print-out closest to...
N=100;
x=1;
y=1;
for n=1: N-1
x = x - (x-exp(-x))/(1+exp(-x));
y = y - (y-exp(-y));
end
display(x-y)
Correct option: 0
The code below has a print-out that most often will be...
s=0.0;
N=10000;
for n=1:N
x=rand(1);
s=s+3*x*x+x;
end
display(s/N)
Correct option: 1.5
For the first question I assumed it was some use of Eulers method but I could not arrive at the number 2. I'm not sure how to tackle the second and third one.
Is there some kind of general strategy I can use to figure out what iterations like this should converge towards (without the use of a computer) ?
Upvotes: 1
Views: 144
Reputation: 8854
The general strategy is to look at what the iteration that is being implemented is doing.
First question
This is dx/x^2=dt
, i.e. 1/x0-1/x=t
, i.e. x=x0/(1-x0*t)=1/(1-1*0.5)=2
.
Second question
The equation for x
is Newton's method for f(x)=x-exp(-x)
, i.e. f'(x)=1+exp(-x)
, so the solution is the root of x=exp(-x)
.
The equation for y
is fixed point iteration for y=exp(-y)
. These both have the same solution x=y=0.5671 (4sf)
.
Third question
rand(1)
is the uniform distribution on [0,1] with mean 1/2
and variance 1/12
so <x^2>=1/12+1/4=1/3
and 3<x^2>+<x>=3/3+1/2=1.5
.
Upvotes: 3