2 aWrite a wellcommented Matlab function program that calcul
2. a)Write a well-commented Matlab function program that calculates the sum of the squares of the first n integers. b)Write a well-commented Matlab function program to do Newton’s method for a function f until |f(x)| < tol. Let f, f 0 , x0 and tol be the inputs and the final x be the output.
Solution
n=5;
 function sumall = sqare_sum(n)
 sumall = 0;
 for j = 1:n
 sumall = sumall+j^2;
 end
 //disp(answer);
 
 function [ x,sx ] = newtosns( f, f0, x0, tol, m )
 if gn == 3
 tol = 1e-4;
 m = 1e1;
 elseif gn == 4
 m = 1e1;
 elseif gn ~= 5
 error(\'invalid\');
 end
   
 f = inline(f);
 f0 = inline(f0);
 x(1) = x0 - (f(x0)/f0(x0));
 ex(1) = abs(x(1)-x0);
 s = 2;
 while (sx(s-1) >= tol) && (s <= m)
 x(s) = x(s-1) - (f(x(s-1))/f0(x(s-1)));
 sx(s) = abs(x(s)-x(s-1));
 s = s+1;
   
 end
 
 end

