Why wont my program connect the x markers together in plot, despite having the '-x' function?

6 visualizzazioni (ultimi 30 giorni)
for j=0:9
sv=sv+1/factorial(j);
plot(j,sv,'b--x');
end
title('Graph Showing Eulers Formula and Convergance Line at y=e')
fprintf('The sum of the first 10 terms is:%0.15f \n',sv)
pd=((sv-exp(1))/((exp(1)+sv)/2))*100;
fprintf('The percentage difference between the above and MatLab value for')
fprintf(' e is:%0.15f%%',pd)
plot([0,9],[exp(1),exp(1)],'r-')
legend('blue x markers','y=e', 'location','southeast')
hold off
  1 Commento
Campbell Manzoni
Campbell Manzoni il 8 Apr 2018
Modificato: Walter Roberson il 8 Apr 2018
FULL PROGRAM:
clear % clear memory
sv=0; % initialize variable 'A'.
disp('The first 10 terms of the series are:'); %Display in command window *
% * the text in purple.
for i=1:10 %for loop with i=0 to i=9 which are the first 10 terms of series.
sv=1/factorial(i-1); %Eulers Formula calculated for each term 'i'
fprintf('Term %i is: %0.15f\n',i,sv) % display the result of each *
% * value of A seperately
end % close forloop pertaining to displaying values 0-9.
figure; hold on % 'hold on' for the for loop to make sure the plotable *
% *figure is not over written by each iteration.
for j=0:9 % For loop
sv=sv+1/factorial(j);
plot(j,sv,'b--x');
end
title('Graph Showing Eulers Formula and Convergance Line at y=e')
fprintf('The sum of the first 10 terms is:%0.15f \n',sv)
pd=((sv-exp(1))/((exp(1)+sv)/2))*100;
fprintf('The percentage difference between the above and MatLab value for')
fprintf(' e is:%0.15f%%',pd)
plot([0,9],[exp(1),exp(1)],'r-')
legend('blue x markers','y=e', 'location','southeast')
hold off

Accedi per commentare.

Risposta accettata

Star Strider
Star Strider il 8 Apr 2018
Modificato: Star Strider il 8 Apr 2018
The easiest way is to save the elements of ‘sv’ as elements of another vector, then plot them together:
sv = 0;
jv = 0:9; % Define ‘jv’ From ‘j’
for j = 1:numel(jv)
sv=sv+1/factorial(j);
svv(j) = sv; % Save ‘sv’ As Elements Of ‘svv’, Avoiding ‘0’ Index
end
plot(jv,svv,'b--x'); % Plot Vectors
hold on
title('Graph Showing Eulers Formula and Convergance Line at y=e')
fprintf('The sum of the first 10 terms is:%0.15f \n',sv)
pd=((svv-exp(1))./((exp(1)+svv)/2))*100; % Change To Use ‘svv’ & Use Vectorised Calculations
fprintf('The percentage difference between the above and MatLab value for')
fprintf(' e is:%0.15f%%',pd(end))
plot([0,9],[exp(1),exp(1)],'r-')
legend('blue x markers','y=e', 'location','southeast')
hold off
EDIT Corrected typographical error.
  2 Commenti
Campbell Manzoni
Campbell Manzoni il 8 Apr 2018
Thanks for that!! I didn't know that you could use the numel function to save the values in the Array, makes perfect sense now.
Thanks again
Star Strider
Star Strider il 8 Apr 2018
As always, my pleasure!
The numel function works here because ‘j’ is a vector. If ‘j’ was an array, it would return the number of elements in the entire array, not the desired outcome here. The length function will return the largest dimension of an array, so it also works for vectors in this context. The safest function is size, because it returns all the dimensions, and you can specify the specific dimension you want it to return with a second argument.

Accedi per commentare.

Più risposte (1)

Walter Roberson
Walter Roberson il 8 Apr 2018
You are plotting one point at a time. plot() only joins points created in the same argument pair in the same plot call. You need to store the values and plot afterwards. Or you could use animatedline()

Categorie

Scopri di più su Graphics Performance in Help Center e File Exchange

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by