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

why is my plot not showing the line? using semilogy

Luis Garcia
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; 
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');
    ylabel('error Delta');
CodeFukurou Reply to 2018-02-13 05:00:40Z

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<SAGROW>
    it(i) = i; %#ok<SAGROW>
    i = i + 1;

%% Plot data
ylabel('Error \delta');
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.545334 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO