%% Assignment 2 % Name: Claudio Maggioni % % Date: 19/3/2019 % % This is a template file for the first assignment to get started with running % and publishing code in Matlab. Each problem has its own section (delineated % by |%%|) and can be run in isolation by clicking into the particular section % and pressing |Ctrl| + |Enter| (evaluate current section). % % To generate a pdf for submission in your current directory, use the following % three lines of code at the command window: % % >> options.format = 'pdf'; options.outputDir = pwd; publish('assignment2.m', options) % %% Problem 3 syms x; f = exp(x); df = diff(f); x_0 = 1; err = zeros(10, 1); h = zeros(10, 1); for i = 1:10 h(i) = 10^(-i); err(i) = abs(subs(df,x,x_0) - my_diff(f, x, x_0, h(i))); end loglog(h, err) function approx = my_diff(f, x, x_0, h) approx = (subs(f,x,x_0 + h) - subs(f, x, x_0)) / h; end