Docsity

Mathematics Lab Exercises: Loops, Plots, and Series Convergence, Study notes of Calculus

Toggle sidebar
bg1
Lab 0: Loops and plots
Adrian Down
January 21, 2006
Exercise 1
Problem
Pick some N, and compute s1, s2,...,sN. Plot these on R. Label these
points.
Code
clear;
dissp = .02; % Display spacing
N=7; % Number of iterations; small to clarify display
s=1; % Initialize partial sums
for k=1:N % Loop to compute partial sums
s(k+1)=s(k)+(-1)^k/(2*k+1); % Add next term to partial sum
end
plot(s,0*s,’go’) % Plot partial sums on number line
hold on; % Still need to add labels...
axis([.5, 1, -.75, .75]); % Fixed axes limits
for k=1:N+1 % Loop to add labels
text(s(k),0-dissp,[’s’,int2str(k-1)]) % Place text labels
end
title(’Exercise 1 output’) % Title of graph
1
pf3
pf4
pf5

Partial preview of the text

Download Mathematics Lab Exercises: Loops, Plots, and Series Convergence and more Study notes Calculus in PDF only on Docsity!

Lab 0: Loops and plots

Adrian Down

January 21, 2006

Exercise 1

Problem

Pick some N, and compute s 1 , s 2 ,... , sN. Plot these on R. Label these points.

Code

clear; dissp = .02; % Display spacing N=7; % Number of iterations; small to clarify display s=1; % Initialize partial sums for k=1:N % Loop to compute partial sums s(k+1)=s(k)+(-1)^k/(2k+1); % Add next term to partial sum end plot(s,0s,’go’) % Plot partial sums on number line hold on; % Still need to add labels... axis([.5, 1, -.75, .75]); % Fixed axes limits for k=1:N+1 % Loop to add labels text(s(k),0-dissp,[’s’,int2str(k-1)]) % Place text labels end title(’Exercise 1 output’) % Title of graph

Results

0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 1

−0.

−0.

−0.

0

s1 s3 s5s7 s6s4 s2 s

Exercise 1 output

while abs(sigma-pi/4) >= tol N = N+1; sigma = s; % Make sigma = s_n; this is not the final form of sigma s = s + (-1)^N/(2*N+1); % Find next partial sum sigma = (sigma + s)/2; % Add new s_n+1 to sigma to find final value end N format long sigma

Results

N = 354 σN = 0. 78539716873481

Exercise 4

Problem

Create the plot of x^2 − 2. Put a tick mark at x = 4. Make a line up to the plot. Draw the tangent line, and find the point where it intersects the axis. Repeat for each successive intercept point.

Method

I want to find the x intercept, call it xi+1 of the tangent to the curve y = f (x) at some point xi. The slope of this tangent curve can be found in two ways. One is using the derivative,

slope = f ′(xi)

Another way to calculate the slope is to use the idea of “rise over run”. In this case, the “rise”, or change in y, of the line is simply f (x 1 ), whereas the “run”, or change in x, of the line is xi − xi+1.

slope =

f (xi) xi − xi+

I consider the particular case of y = x^2 − 2 ⇒ y′^ = 2x. I set the two expressions for slope equal to each other and solve for xi+1.

2 xi =

x^2 i − 2 xi − xi+

xi − xi+1 =

xi 2

xi

xi+1 =

xi 2

xi

I use this recursive formula to find xi+1 in the algorithm below.

Code

clear; a=linspace(0, 5, 100); % Create x values for y = x^{2}- b=a.^2-2; % Calculate y N=100; % Number of successive tangents to be calculated x(1) = 4; % Initialize the array of x points x(2) = 4; % Two x points for eachh x value, one on % curve, one on x axis y(1) = 0; % y value for x axis point y(2) = x(2)^2-2; % y value for curve point for i=1:N % Loop for specified number of tangents x(2i+1) = x(2i)/2 + 1/x(2i); % Calculated value of next x corodinate x(2i+2) = x(2i + 1); % Fill both x point array positions y(2i+1) = 0; % y value for x axis point y(2i+2) = x(2i+2)^2-2; % y value for curve point end plot(a, b) % Plot y = x^2 - 2 axis([1,5,-5,15]); hold on; plot(a, 0*a,’g’) % Plot x axis plot(x, y,’r’) % Plot points of intersect with tangents x(end) % Display last x point % (the root, by Newton’s method)