% test_convergence checks for the radius of convergence of a % Taylor series by plotting the sum for a fixed value of x % (supplied by the user) as a function of the number of terms % in the sum. % Programmer: Dick Furnstahl furnstahl.1@osu.edu % % Revision history: % 15-Apr-2005 -- original test_convergence.m for Physics 263 % clear; % this sets variables to zero, so we start with a clean slate x = input('enter a test value of x: '); % get a value of x to try N = 500; % maximum number of terms in the series to sum % We'll create two vectors, nvec and my_sum. The i'th elements of the % vectors are referenced by nvec(i) and my_sum(i), with i starting at 1. nvec(1) = 1; % first element of the vector nvec = (1,2,3,...,N) my_sum(1) = 1 - x; % start with the sum of the x^0 and x^1 terms for n = 2:N % now add in the rest, i.e., n = 2, 3, 4, 5, ..., N nvec(n) = n; % build the nvec vector (1,2,3,4,...,N) entry by entry my_sum(n) = my_sum(n-1) + (-1)^n * (x)^n; % add the n'th term to the sum end % now make a plot of the sum vs. the number of terms plot(nvec,my_sum); % plot(x_vector,y_vector) plots one line xlabel('n'); % add the x-axis label ylabel('sum'); % add the y-axis label