How to code erf(z) summation series?

2 Comments

Please insert the code you have so far.
If you did not start yet, make an attempt.
%Final Lab
clear all
clc
z = 0;%intialize variable z
n = 0;%intialize variable n
N0 = zeros(1,21); %preallocate array for solutions of 1-erf(z)
fh = @(n,z) 1-erf(z)%create function handle for 1-erf(z)
N0(1) = n;%set n to first position in array
z = 0:0.1:2%array of z values stepping in 0.1 to 2
i = 0;%intialize index
for (i=1:20)%loop counter
while(abs(N0(i+1)) < eps(N0(i)))%loop condition
N0(i+1) = N0(i)+fh(n,z)(i)%sum series
end
end
plot(N0, z)%plot graph
z2 = [0.1 0.5 1 2]%values for erfc(z2)
errgraph=erfc(z2)%errgraph function
hold%place lines on same plot
plot(errgraph)%plot graph on same plot to see convergence.
I first attempted to use the the scientific expansion but I couldnt figure out how to implement the sigma notation to properly have n= 0 to infinity, so I tried using 1-erf(z) but it gives me a plot in the opposite direction of erfc function.

Sign in to comment.

 Accepted Answer

Torsten
Torsten on 27 May 2021
Edited: Torsten on 27 May 2021
function main
z = [0.1 0.5 1 2];
for i=1:numel(z)
[erfc1(i),n(i)] = myerfc1(z(i));
end
erfc_matlab = erfc(z);
X = [z.',erfc1.',n.',erfc_matlab.'];
disp(X)
end
function [erfc1,n] = myerfc1(z)
erf1 = 0.0,
term = z;
n = 0;
while abs(term) >= eps
erf1 = erf1 + term;
n = n+1;
term = term * (-1)/n * z^2 * (2*n-1)/(2*n+1);
end
erf1 = erf1 * 2/sqrt(pi);
erfc1 = 1-erf1;
end

12 Comments

I gave this a shot but I when I plot them to see the convergence they are shown seperately as points and vertical straight lines that do not intersect? I tried plotting them by using plot(erf1,z) and plot (erfc1,z)
I also tried plotting vs the n values and that just gave me two points so that wasnt it either.
Could you explain to me why yours is a graph with with just a few points and why when I graph it using the above the code it gives a line for erfc(z) function?
I don't make a graph, I just output the results for the four prescribed z-points.
Please show the code you are using where you get a plot.
I added it to the code myself using the plot function. Im trying to visually check that the values for z at those four points in the graph of erfc(z) do converge with graph values of 1 - erf(z)
function [erfc1,n] = myerfc1(z)
erf1 = 0.0,
term = z;
n = 0;
while abs(term) >= eps
erf1 = erf1 + term;
n = n+1;
term = term * (-1)/n * z^2 * (2*n-1)/(2*n+1);
end
erf1 = erf1 * 2/sqrt(pi);
erfc1 = 1-erf1;
plot(term,z)
hold
plot(erfc1,z,'b')
end
Is erf1 supposed to be defined as 2/sqrt(pi) but you have erf1 * 2/pi? I see now how you compared the values and they are same for the table. I guess my idea of convergence is wrong. I thought the values would make a line on graph of the same behavior such as exponential behavior with similiar slopes approaching the same value(converging)?
You have to include plot commands in the calling function main because myerfc1 is only called with one single number for z.
So at the end of function main, you could include
plot(z,erfc1,z,erfc_matlab)
And please change in myerfc1 the line
erf1 = erf1 * 2/pi
to
erf1 = erf1 * 2/sqrt(pi)
Torsten
Torsten on 27 May 2021
Edited: Torsten on 27 May 2021
I don't understand your last question.
"term" is the ( n+1)-th summand of the infinite series. "erf1" is the series of erf, summed up to the n-th summand. Thus "erf1" will approach erf(z) * sqrt(pi)/2 while "term" approaches zero.
For your exercise, you will have to remove n from the list of output parameters of myerfc1 because you are asked to write a function with input argument z and output argument erfc(z).
Thank you for your help. I did the edit and moved the code for plot and got this which looks much better than my previous graph just from intuition on how graphs behave I knew something was wrong. Before my lines intersected in opposite directions. Now they do this and both have the same slopes and behaviour and both lines no appear to converge to the same value. the first graph is the corrected plot.
Although I don't know what the graps show: it's fine that you are satisfied with the result :-)
I still dont get the what my teacher wants as result because when I expand the coded and remove the semicolons and look in the command line the n ouputs from the function dont match values for the erfc(z)
but somehow do in the output?
Include your final code to see what's going wrong.

Sign in to comment.

More Answers (1)

Hi,
Here is the corrected code:
clearvars; clc; close all
N0 = zeros(1,21); %preallocate array for solutions of 1-erf(z)
fh = @(z) 1-erf(z); %create function handle for 1-erf(z)
z = 0:0.1:2; %array of z values stepping in 0.1 to 2
for ii=1:20%loop counter
while(abs(N0(ii+1)) < eps(N0(ii))) %loop condition
N0(ii+1) = N0(ii)+fh(z(ii+1)) ; %sum series
end
end
plot(N0, z) %plot graph
z2 = [0.1 0.5 1 2] %values for erfc(z2)
errgraph=erfc(z2) %errgraph function
hold %place lines on same plot
plot(errgraph) %plot graph on same plot to see convergence

1 Comment

Thanks and I was wondering about the convergence and now I feel silly because that was the first function handle I used. Are the slopes supposed to go in the opposite direction of each other?

Sign in to comment.

Asked:

on 25 May 2021

Commented:

on 28 May 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!