In: Mechanical Engineering
Your task is to take the below code, and insert comments (using the “%” symbol) next to each line of code to make sure that you know what every line does.
clc
clear
close all
NMax = 100;
partialSum = 0;
exactAnswer = pi^2;
for k=1:NMax
partialSum = partialSum + 6/k^2;
percentDiff(k) = abs(partialSum -
exactAnswer)/exactAnswer*100;
end
NVector = [1:NMax];
plot(NVector,percentDiff);
xlabel('{{Noob}}');
ylabel('% Difference');
clc
clear
close all
NMax = 100; % maximum number upto which difference between exact answer and partial sum is compared
partialSum = 0; % starting value of partial sum
exactAnswer = pi^2; % answer against which partial sum is compared
for k=1:NMax % k will go from 1 to Nmax with increment of 1
partialSum = partialSum + 6/k^2; % formula to calculate partial sum
percentDiff(k) = abs(partialSum - exactAnswer)/exactAnswer*100; % '% difference between partal sum and exact answer'
end
NVector = [1:NMax]; % vectror created to plot
plot(NVector,percentDiff); % plotting Nvector on x axis and difference on y axis
xlabel('{{Noob}}'); % label of x axis
ylabel('% Difference'); % label of y axis