The rows of varStore
correspond to the evaluation of f
at all the points in xRange
, where row i
has had f
evaluated with a
set to aValues[i]
.
That is, row 1 of varStore
is the evaulation of f
at all points in xRange
, with a=0.25
.
Row 2 is the evaulation of f
at all points in xRange
, with a=0.5
, and so forth up to i=5
and a=1.25
.
Notice that we have preallocated the array varStore
to prevent issues with growing arrays.
Also, we could have directly allocated the i
-th row of varStore
, cutting out the need for vals
by using:
xRange = linspace(0,1,250);
varStore = zeros(5,250);
aValues = [0.25 0.5 0.75 1.0 1.25];
for i = 1:5
varStore(i,:) = f(xRange, 0, aValues(i), 1);
end %for, i
This avoids having to define another array vals
whose values are only temporary (we overwrite them on each loop, and only use it to hold the values each time) which speeds up the process even further.
for
me
The missing code should be filled in as follows, and produce the following output:
clear;
close all;
xRange = linspace(0,1,250);
aValues = 0.5:0.1:1.5; %remember: this generates an array of the values from 0.5 to 1.5, going up in steps of 0.1
%create a new figure with ID 1
fig = figure(1);
%ensure that all plotting is done on this figure, without overwriting
hold on;
for i=1:length(aValues)
%compute the values for f at the i-th value of a, and graph the data you get onto the current figure.
%don't worry about trying to specify line colours, MATLAB will do it for you
plot(xRange, f(xRange, 0, aValues(i), 1));
end %for, i
%set axis labels for your figure
xlabel('$x$','interpreter','latex');
ylabel('$f(x,a,0,1)$','interpreter','latex');
%ensure that we don't overlay plots anymore
hold off;
Notice how MATLAB is clever enough to change the colour of your lines automatically when you plot to the same figure!
The while
loop needs to be fixed as follows:
pi_approx = 0;
k = 1;
err = abs(pi - pi_approx); %the abs function in MATLAB returns the absolute value
tol = 1e-1; %tol is short for tolerance; this quantifies the accuracy to which we want our value
while err>tol
pi_approx = pi_approx + 4 * (-1)^(k+1)/(2*k-1); %add on the next term in the series
err = abs(pi - pi_approx); %update the error with our new pi_approx value
k = k+1; %move onto the next term in the sequence in preparation for next time
end %while, err>tol
fprintf('Terms needed to compute pi to tolerance of %.1e was: %d \n', tol, k-1)
%note: there's a k-1 here because we add 1 to k at the end of the loop, anticipating that we will need the next term
%as such, if we only need 1 term, we will still update k from 1 to 2, even though we don't use the second term
%hence, there's a -1 in here!
fprintf('The error in the final approximation was %.2e \n', err)
Terms needed to compute pi to tolerance of 1.0e-01 was: 10 The error in the final approximation was 9.98e-02
As you vary tol
, you should notice a marked increase in the number of iterations required for the series to get close to the value of pi
.
Indeed, we can automate the process by using a for
-loop:
tol_values = logspace(-1,-4,16); %tolerances between 1e-1 and 1e-4
terms_needed = zeros(1,16); %preallocate memory for storing the number of iterations
for i=1:16
pi_approx = 0;
k = 1;
err = abs(pi - pi_approx);
tol = tol_values(i); %this time, we use the i-th value in tol_store for the tolerance
while err>tol
pi_approx = pi_approx + 4 * (-1)^(k+1)/(2*k-1); %add on the next term in the series
err = abs(pi - pi_approx); %update the error with our new pi_approx value
k = k+1; %move onto the next term in the sequence in preparation for next time
end %while, err>tol
terms_needed(i) = k-1; %store the number of terms needed in the i-th position in the array terms_needed
end %for, i
f = figure(1);
loglog(tol_values, terms_needed);
xlabel('Tolerance requested');
ylabel('Terms required');
You should find that the number of terms needed scales with the inverse of the tolerance you request the computation go to.