How to code erf(z) summation series?
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Henry B.
el 25 de Mayo de 2021
Comentada: Torsten
el 28 de Mayo de 2021
2 comentarios
Torsten
el 26 de Mayo de 2021
Please insert the code you have so far.
If you did not start yet, make an attempt.
Respuesta aceptada
Torsten
el 27 de Mayo de 2021
Editada: Torsten
el 27 de Mayo de 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 comentarios
Más respuestas (1)
Sulaymon Eshkabilov
el 27 de Mayo de 2021
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
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!