Plotting infinite series on interval
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have the following function for error approximation
function y = Approx(x,N);
Approx=zeros(1,length(x));
for n=0:N
Approx=Approx+(((-1)^n)*x^(2*n+1))/((factorial(n))*(2*n+1));
end
Approx=(2/sqrt(pi))*Approx
My question is how would I plot this on a given interval say -1,1 as well as plotting it again the built in erf in Matlab
0 comentarios
Respuestas (1)
Deepak
el 17 de En. de 2025
We can plot the approximation of the error function against built-in "erf" function in MATLAB by defining a MATLAB function "Approx" that computes the approximation using a series expansion. In a script, we generate a set of "x" values over the interval [-1, 1] and use the "Approx" function to calculate the approximation for these values. We also compute the built-in "erf" function for the same "x" values. Finally, we plot both the approximation and the built-in "erf" on the same graph for comparison, using distinct line styles and colors to differentiate them, and include labels and a legend for clarity.
Below is the MATLAB code to achieve the same:
% Define the approximation function
function y = Approx(x, N)
Approx = zeros(size(x)); % Preallocate the Approx array for efficiency
for n = 0:N
Approx = Approx + (((-1)^n) * x.^(2*n+1)) / ((factorial(n)) * (2*n+1));
end
y = (2/sqrt(pi)) * Approx;
end
% Main script to plot the functions
% Define the interval and number of terms
x = linspace(-1, 1, 1000); % 1000 points between -1 and 1
N = 10; % Number of terms in the approximation
% Calculate the approximation and the built-in erf
approximation = Approx(x, N);
builtin_erf = erf(x);
% Plot the results
figure;
plot(x, approximation, 'r-', 'LineWidth', 2); % Plot approximation in red
hold on;
plot(x, builtin_erf, 'b--', 'LineWidth', 2); % Plot built-in erf in blue dashed line
hold off;
% Add labels and legend
xlabel('x');
ylabel('Function Value');
title('Approximation vs. Built-in erf');
legend('Approximation', 'Built-in erf');
grid on;
Please find attached the documentation of functions used for reference:
I hope this helps in resolving the issue.
0 comentarios
Ver también
Categorías
Más información sobre Polar Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!