This repository has been archived on 2021-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
ICS/hw2/assignment2.m

36 lines
892 B
Mathematica
Raw Normal View History

2020-03-19 11:00:00 +00:00
%% 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