- #1
- 323
- 15
Homework Statement
Write code for solving the integral ##\int_{0}^{x}e^{-t^2}dx## using simpsons method and then plot the function from ##x = 0## to ##x = 5## with ##0.1## increment.
Homework Equations
3. The Attempt at a Solution [/B]
I was told that the best way to plot the function is to write it in Matlab so i decided to give it a go since i haven't really worked in MATLAB before.This is my integral solution code:
Matlab:
function y = simpson_method(x)
y1 = exp(-0.^2);
y2 = exp(-(x).^2);
c = 0;
d = 0;
incr = 0.1;
for i = 1 : (x - 0)/incr - 0.5
y = exp(-(0 + i*incr).^2);
if i/2 == floor(i/2)
d = d + y;
continue
else
c = c + y;
continue
end
end
y = incr/3 * (y1 + 4*c + 2*d + y2);
Matlab:
x = 0:0.1:15;
y = simpson_method(x);
plot(x,y)
Which is weird since the function i wrote gives correct values at all points. What is wrong here?