a)
Code Block |
---|
|
function result = f(x)
result = exp(-(x.^2));
end
% eller f = @(x) exp(-x.^2); |
og b)
Code Block |
---|
|
x =-2:0.01:2;
plot(x,f(x)) |
c)
Code Block |
---|
|
hold on
x = -2:0.5:2;
plot(x,f(x)) |
d)
Code Block |
---|
|
function area = trapezoidArea(a, b, w)
area = (a+b)/2 * w;
end |
e)
Code Block |
---|
|
function area = trapezoidMethod(start, stop, n, fn)
areafunction result = trapes(a,b,N,f)
result = 0;
h = (bstop -a start)/(N-1) / n;
for i = 0:N-1start : h : (stop - h)
ra = ffn(h*i+a);
sb = ffn((i +1)* h+a);
resultarea = resultarea + trapezAreatrapezoidArea(ra,s b, h);
end
end
function A = trapezArea(r,s,h)
A = (r+s)*h/2;
end |
Dersom vi øker n, vil vi få en bedre approksimasjon.