Write a user defined function psum.m to approximate the value of the sum K2 1223
ID: 3282282 • Letter: W
Question
Write a user defined function psum.m to approximate the value of the sum K2 12232 by adding terms to the sum until the difference (error) between the sum and/6 is smaller than a given tolerance tol. The function input is tol, and its outputs are the sum, the number of steps that it took to obtain it, and the error. Therefore the first line of the function m-file should read as: function [sum, steps,error-psum (tol). Note: psum must contain a while-loop; prior to the loop it will require an initial "partial sum", the actual "answer", and an initial error (magnitude of difference between answer and partial sum) to initialize the loop (a) Call your function psum from Project 2.m with each of five tolerance values 10.1, 0.05, 0.01, 0.005, 0.001. Save the error corresponding to each tolerance in a vector called errors, and the number of steps for each tolerance in a vector called total-Steps. This can be done by o with 5 iterations that inputs a new tolerance into psum each calling psum within a for-loop time, OR you may repeat the call to psum 5 times separatelyExplanation / Answer
function dummy=project2()
clc;
clear all;
tol=[0.1 0.05 0.01 0.005 0.001];
for i=1:5
disp('__________________')
tolernece =tol(i)
disp('__________________')
[sum,steps,error]=psm(tol(i))
end
function [sum,steps,error]=psm(tol)
sum=0;
k=1;
steps=0;
error=1;
while(error>tol)
sum=sum+1/k^2;
k=k+1;
error=abs(sum-pi^2/6);
steps=steps+1;
end
end
end
%%% Solution %%%
__________________
tolernece =
0.1000
__________________
sum =
1.5498
steps =
10
error =
0.0952
__________________
tolernece =
0.0500
__________________
sum =
1.5962
steps =
20
error =
0.0488
__________________
tolernece =
0.0100
__________________
sum =
1.6350
steps =
100
error =
0.0100
__________________
tolernece =
0.0050
__________________
sum =
1.6399
steps =
200
error =
0.0050
__________________
tolernece =
1.0000e-03
__________________
sum =
1.6439
steps =
1000
error =
9.9950e-04
>>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.