Using 1/3 simpson's rule for evaluating an erf function up to a specific error
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
So a homework problem I am doing asks for me to use the 1/3 Simpson's rule to evaluate the erf function given an X with an absolute true error of 5E-8 with the minimum number of segments (n). However when I make a function that evaluates the erf function for each n it never makes it to the absolute true error of 5E-8 and instead just runs infinitely and at most giving me an error of around 5E-6. So how do I get to such a high accuracy with the 1/3 Simpson's rule?
My code so far.
clear,clc
n = 0;
x = 1;
t = @(x) exp(-x.^2);
format long
error = 1;
while error > 5E-8
n = n+1;
w = (1:1:n);
h = x/n ;
r = linspace(0,x,n);
odd = w(mod(w,2)~=0);
even = w(mod(w,2)==0);
E = r(even);
O = r(odd);
if n == 2
I = h/3*(t(r(1))+t(r(2))+t(r(n)));
else
I = h/3*(t(r(1))+2*sum(t(O))+4*sum(t(E))+t(r(n)));
end
Int = 2/sqrt(pi)*I
error = abs(erf(x) - Int);
end
result = [Int,error,length(w)]
0 Kommentare
Antworten (1)
David Hill
am 9 Jun. 2020
a=0;
b=1;
n=2.1603e7;%limit the loop based on testing
iT=sqrt(pi)/2*erf(b);
error=1;
tol=5e-8;
t = @(x) exp(-x.^2);
while error>tol
n=n+2;
x=linspace(0,1,n);
it = (b-a)/(3*n)*sum(t(x).*([1,repmat([2 4],1,(n-2)/2),1]));
error=abs(iT-it);
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Calculus finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!