Home why is my plot not showing the line? using semilogy

# why is my plot not showing the line? using semilogy

Luis Garcia
1#
Luis Garcia Published in 2018-02-13 03:53:41Z
 This question already has an answer here: Plotting graph using for loop in MatLab 2 answers Im trying to plot iterations vs error_delta but its not showing. but its not showing the line of plotting. We suppose to plot the iterations as the error_delta is in the y-axis, with the error_delta= abs(root_delta-x); % root for f(x) = cos(x) paso = 0.5e-10; N = 100; x= 1.0; %initial guess. i = 0; eps = 1; root_delta = 4.6; figure(1); hold on; while eps >= paso && i <= N %while loop to iterate y = x - (cos(x)/(-sin(x))); eps = abs(y-x); x= y; error_delta = abs(root_delta - x); i = i+1; disp(' root iteration'); disp([x',i',error_delta']); semilogy(i,error_delta); xlabel('iterations'); ylabel('error Delta'); end disp(y); disp(i); 
CodeFukurou
2#
 The problem you are having here is that semilogy works like plot does and requires arrays of data (one for each axis) to plot a line between them. In your situation it would be good to create these arrays inside of your while loop by indexing them and then using the semilogy function afterwards, this code works for me (NOTE: Defining it and error_delta like that is considered bad practice as the array changes size on every loop. If you have a way of knowing how big they will be before the loop runs, please initialize them with the zeros function): % root for f(x) = cos(x) %% Define parameters paso = 0.5e-10; N = 100; x = 1.0; i = 1; eps = 1; root_delta = 4.6; %% Allocate arrays it = []; error_data = []; %% Generate data while eps >= paso && i <= N y = x - (cos(x)/(-sin(x))); eps = abs(y-x); x = y; error_delta(i) = abs(root_delta - x); %#ok it(i) = i; %#ok i = i + 1; end %% Plot data figure(1); semilogy(it,error_delta); xlabel('Iterations'); ylabel('Error \delta');