The er f(t) function in MATLAB is defined as follows: er f (t) = 2/Squareroot pi
ID: 3824611 • Letter: T
Question
The er f(t) function in MATLAB is defined as follows: er f (t) = 2/Squareroot pi integral^t_0 e^-x^2 dx. The MATLAB command er f (t) returns the exact value of er f(t). Please use the trapezoid rule to evaluate the er f (1) function for the following sequence of increasing npanel (decreasing h): npanel = 50: 10: 100. Compare your results with the exact er f(1) value and list all absolute errors. Repeat using the simpson function instead of the trapezoid function, and compare the absolute to the ones you from Problem 3.Explanation / Answer
Matlab function Trapezoidal.m
function S = Trapezoidal(fun,a,b,n)
x = linspace(a,b,n); % X vector
S = fun(a)+fun(b); % Computing rhe integral sum of first and last terms
for k = 2:n-1
S = S+2*fun(x(k)); % Summation of intermediate terms
end
S = (x(2)-x(1))*S/2; % multiplying by h/3
end
Matlab function Simpson.m
function v = Simpson(fun,a,b,n)
x = linspace(a,b,n); % X vector
v = fun(a)+fun(b); % Computing rhe integral sum of first and last terms
for k = 2:2:n-2
v = v+4*fun(x(k))+2*fun(x(k+1)); % Summation of intermediate terms
end
v = (x(2)-x(1))*v/3; % multiplying by h/3
end
Calling the functions for Computing erf(1)
f = @(x) 2*exp(-(x^2))/sqrt(pi); % function
t = 1; % Value of t
a = 0; % Lower Limit
b = t; % Upper limit
Exa = erf(t); % Correct solution from matlab
for npanel = 50:10:100
S = Trapezoidal(f,a,b,npanel); % Computing the integration using Trapezoidal rule
Err = abs(S-Exa); % Absolute error
fprintf('Using Trapezoidal erf(1) = %f using npanel = %d absolute error = %f ',S,npanel,Err);
end
for npanel = 50:10:100
S = Simpson(f,a,b,npanel); % Computing the integration using Simpsons rule
Err = abs(S-Exa); % Absolute error
fprintf('Using Simpsons erf(1) = %f using npanel = %d absolute error = %f ',S,npanel,Err);
end
Results
Using Trapezoidal erf(1) = 0.842672 using npanel = 50 absolute error = 0.000029
Using Trapezoidal erf(1) = 0.842681 using npanel = 60 absolute error = 0.000020
Using Trapezoidal erf(1) = 0.842686 using npanel = 70 absolute error = 0.000015
Using Trapezoidal erf(1) = 0.842690 using npanel = 80 absolute error = 0.000011
Using Trapezoidal erf(1) = 0.842692 using npanel = 90 absolute error = 0.000009
Using Trapezoidal erf(1) = 0.842694 using npanel = 100 absolute error = 0.000007
Using Simpsons erf(1) = 0.839819 using npanel = 50 absolute error = 0.002881
Using Simpsons erf(1) = 0.840316 using npanel = 60 absolute error = 0.002385
Using Simpsons erf(1) = 0.840666 using npanel = 70 absolute error = 0.002034
Using Simpsons erf(1) = 0.840927 using npanel = 80 absolute error = 0.001774
Using Simpsons erf(1) = 0.841129 using npanel = 90 absolute error = 0.001572
Using Simpsons erf(1) = 0.841289 using npanel = 100 absolute error = 0.001412
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.