output = '##p: 7\n##id: yw\n'; % Q1 x = zeros(1, 6); x(1) = -7; for t = 2:6 x(t) = x(t - 1) - polyval([3, -5, 3, -9, 7, -9], x(t - 1)) / polyval([15, -20, 9, -18, 7], x(t - 1)); end x(1:6) output = [output '##1: ' mat2str(ans) '\n']; % Q2 x = zeros(1, 111); x(1) = 2; for t = 2:111 if polyval([15, -20, 9, -18, 7], x(t - 1)) == 0 x(t) = x(t - 1); else x(t) = x(t - 1) - polyval([3, -5, 3, -9, 7, -9], x(t - 1)) / polyval([15, -20, 9, -18, 7], x(t - 1)); end end x(1:111) output = [output '##2: ' mat2str(ans) '\n']; % Q3 x = zeros(1, 1000); x(1) = -8; f = polyval([3, -5, 3, -9, 7, -9], x(1)); df = polyval([15, -20, 9, -18, 7], x(1)); t = 2; while abs(f) >= 0.005 && abs(f) <= 10^7 && df ~= 0 x(t) = x(t - 1) - f / df; f = polyval([3, -5, 3, -9, 7, -9], x(t)); df = polyval([15, -20, 9, -18, 7], x(t)); t = t + 1; end x(1:t-1) output = [output '##3: ' mat2str(ans) '\n']; % Q4 x = zeros(1, 7); x(1:2) = [-7 -6]; f = polyval([3, -5, 3, -9, 7, -9], x(2:-1:1)); for t = 3:7 x(t) = (x(t - 2) * f(1) - x(t - 1) * f(2)) / (f(1) - f(2)); f(2) = f(1); f(1) = polyval([3, -5, 3, -9, 7, -9], x(t)); end x(1:7) output = [output '##4: ' mat2str(ans) '\n']; % Q5 x = zeros(1, 112); x(1:2) = [2 3]; f = polyval([3, -5, 3, -9, 7, -9], x(2:-1:1)); for t = 3:112 if f(1) == f(2) x(t) = x(t - 1); else x(t) = (x(t - 2) * f(1) - x(t - 1) * f(2)) / (f(1) - f(2)); end f(2) = f(1); f(1) = polyval([3, -5, 3, -9, 7, -9], x(t)); end x(1:112) output = [output '##5: ' mat2str(ans) '\n']; % Q6 x = zeros(1, 1000); x(1:2) = [-8 -7]; f = polyval([3, -5, 3, -9, 7, -9], x(2:-1:1)); t = 3; while abs(f(1)) >= 0.005 && abs(f(1)) <= 10^7 && f(1) ~= f(2) x(t) = (x(t - 2) * f(1) - x(t - 1) * f(2)) / (f(1) - f(2)); f(2) = f(1); f(1) = polyval([3, -5, 3, -9, 7, -9], x(t)); t = t + 1; end x(1:t-1) output = [output '##6: ' mat2str(ans) '\n']; % Q7 [-6 11; 0 17 * phi(2.5)] output = [output '##7: ' mat2str(ans) '\n']; % Q8 x = -6:17/9:11; m = x + 17/18; [x; 0 17/9 * phi(m(1:9))]; output = [output '##8: ' mat2str(ans) '\n']; % Q9 either same as Q8 or actually random x = sort(rand(1, 10)) .* 17 - 6; m = ([0 x] + [x 0]) .* 0.5; [x; 0 diff(x) .* phi(m(2:10))] output = [output '##9: ' mat2str(ans) '\n']; % Q10 and write to file output = [output '##10: None']; file = fopen('P7.txt', 'wt'); fprintf(file, output); fclose(file); function p = phi(x) z = (x - 3) ./ 5; p = 1 / (5 * sqrt(2 * pi)) .* exp(-0.5 .* z .* z); end