Plotting one data set in MATLAB; graph shows two

232 Views Asked by At

I am attempting to teach myself MATLAB from MITOPENCOURSEWARE.

I am working on exercise 10 found here: https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-094-introduction-to-matlab-january-iap-2010/assignments/MIT6_094IAP10_assn01.pdf

Below is my code:

% NAME: JACOB ZELKO
% DATE: 8/3/17
% DESCRIPTION: PART OF ASSIGNMENT 1 OF MITOPENCOURSEWARE
% TO DETERMINE THE CONVERGENCE OF AN INFITE SERIES

% ESTABLISHING FIGURE
figure('Name', 'P = 0.99', 'Color', 'white', 'Numbertitle', 'off');

% WHERE P = .99

% INITIAL CONDITIONS
p = 0.99;
k = [0:1000];

% TERM APPROXIMATIONS
geomSeries = p.^k;
G = 1/(1 - p);

% PLOTTING SERIES VALUES 
plot([0 max(k)], G, 'Color', 'red', 'Displayname', 'Infinite Sum');
hold on;
plot(k, cumsum(geomSeries), 'Color', 'blue', 'Displayname', 'Finite Sum');
hold on;

xlabel('Index');
ylabel('Sum');
title('Convergence of geometric series with p=0.99');
legend('show');

% WHERE P = 2

% ESTABLISHING FIGURE
figure('Name', 'P = 2.00', 'Color', 'white', 'Numbertitle', 'off');

% INITIAL CONDITIONS
p = 2;
n = [1:500];

% TERM APPROXIMATIONS
pSeries = (1./n.^p);
P = pi()^2/6;

% PLOTTING SERIES VALUES 
plot([0 max(n)], P, 'Color', 'red', 'Displayname', 'Infinite Sum');
hold on;
plot(n, cumsum(pSeries), 'Color', 'blue', 'Displayname', 'Finite Sum');
hold on;

xlabel('Index');
ylabel('Sum');
title('Convergence of geometric series with p=2');
legend('show');

My issue is that when I plot these approximations, one plot named, "Infinite Sum", it does not plot the graph (it should be a horizontal line) and it shows two data sets named Infinite Sum on the graph.

Does anyone know what I am doing incorrectly to incur this error? Thank you!

1

There are 1 best solutions below

1
On BEST ANSWER

Since the x-series of this plot has two elements, the y-series needs two elements as well:

plot([0 max(k)], [G G], 'Color', 'red', 'Displayname', 'Infinite Sum');

The same goes for the second figure:

plot([0 max(n)], [P P], 'Color', 'red', 'Displayname', 'Infinite Sum');