a)
function result = f(x) result = exp(-(x.^2)); end % eller f = @(x) exp(-x.^2); |
b)
x =-2:0.01:2; plot(x,f(x)) |
c)
hold on x = -2:0.5:2; plot(x,f(x)) |
d)
function area = trapezoidArea(a, b, w) area = (a+b)/2 * w; end |
e)
function area = trapezoidMethod(start, stop, n, fn)
area = 0;
h = (stop - start) / n;
for i = start : h : (stop - h)
a = fn(i);
b = fn(i + h);
area = area + trapezoidArea(a, b, h);
end
end |
Dersom vi øker n, vil vi få en bedre approksimasjon.